sharplib/ser/XmlSer_Tests.cs

134 lines
3.3 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
//using Org.BouncyCastle.Crypto.IO;
namespace ser;
public record class SimpleImmutable( string Name, int Age ) : imm.Timed<SimpleImmutable>;
static public class Test
{
public class ExternalClass
{
public string ActualProperty { get; set; } = "test_ActualProperty_set_inline";
//public string ActualProperty_NotSerialized { get; set; } = "ActualProperty_NotSerialized";
}
[ser.Ser( Types = ser.Types.Implied )]
public partial class LeaveWithExternalClasss
{
//[ser.Do]
//public bool _cccwp_doBool = true;
[ser.ChildPropsAttribute( "ActualProperty" )]
public ExternalClass _leaf_external = new();
//public string _cccwp_doNotSerialize = "test_do_not_serialize";
}
[ser.Ser]
public class TrunkClass
{
public LeaveWithExternalClasss _trunk_leaf = new();
//public int _chf_test = 10;
//private string _chf_priv_string = "test_priv_string";
};
public static void Serialization()
{
ser.XmlCfg cfg = new()
{
Verbose = true,
};
ser.TypeMetaCache metaCache = new( cfg );
//metaCache.AddType( typeof( ClassContainsClassWithProp ), "ActualProperty" );
TrunkClass trunk = new()
{
_trunk_leaf = new()
{
_leaf_external = new()
{
ActualProperty = "ActualProperty_set_in_cons"
}
}
};
Debug.Assert( trunk._trunk_leaf._leaf_external.ActualProperty == "ActualProperty_set_in_cons" );
var memStream = new MemoryStream();
{
var xml = new ser.XmlSer( cfg, metaCache );
xml.Serialize( memStream, trunk );
}
memStream.Position = 0;
var strXml = System.Text.Encoding.UTF8.GetString( memStream.ToArray() );
var badXml = "<root>\n <prop doBool=\"True\" />\n</root>";
///*
var badXml_02 = @"""<root>
<prop doBool=""True"">
<propHolder>
<ActualProperty ActualProperty=""ActualProperty_set_in_cons"" />
<ActualProperty_NotSerialized ActualProperty_NotSerialized=""ActualProperty_NotSerialized"" />
</propHolder>
<doNotSerialize doNotSerialize=""test_do_not_serialize"" />
</prop>
</root>""";
//*/
Debug.Assert( strXml != badXml );
memStream.Position = 0;
var classHasFields2 = new TrunkClass();
//classHasFields2._chf_prop._cccwp_propHolder.ActualProperty_NotSerialized = "ActualProperty_NotSerialized_set_in_test_01";
Debug.Assert( trunk._trunk_leaf._leaf_external.ActualProperty == "test_ActualProperty_set_inline" );
//Debug.Assert( classHasFields2._chf_prop._cccwp_propHolder.ActualProperty_NotSerialized == "ActualProperty_NotSerialized" );
{
var xml = new ser.XmlSer(cfg, metaCache);
classHasFields2 = xml.Deserialize<TrunkClass>( memStream );
}
Debug.Assert( trunk._trunk_leaf._leaf_external.ActualProperty == "test_ActualProperty_set_inline" );
//Debug.Assert( classHasFields2._chf_prop._cccwp_propHolder.ActualProperty_NotSerialized == "ActualProperty_NotSerialized_set_in_test_01" );
memStream.Position = 0;
var classHasFields3 = new TrunkClass();
{
var xml = new ser.XmlSer(cfg, metaCache);
xml.DeserializeInto( memStream, classHasFields3 );
}
Debug.Assert( trunk._trunk_leaf._leaf_external.ActualProperty == "test_ActualProperty_set_inline" );
//Debug.Assert( classHasFields3._chf_prop._cccwp_propHolder.ActualProperty_NotSerialized == "ActualProperty_NotSerialized_set_in_test_01" );
}
}