public void TestNormal() { ExampleOuter o1 = CreateExample(); Assert.AreEqual(2, o1.ValueObject.Value); Assert.AreEqual(4, o1.ValueObject.OtherValue); string s1 = new JsonSerializer().Serialize(o1); var d = new JsonDeserializer(); ExampleOuter o2 = d.Deserialize <ExampleOuter>(s1); CompareSomeFields(o1, o2); Assert.AreEqual(2.345f, o1.RetrievePrivateValue()); Assert.AreEqual(0, o2.RetrievePrivateValue()); Assert.AreEqual(0, o2.ValueObject.OtherValue); string s2 = new JsonSerializer().Serialize(o2); Assert.AreEqual(s1, s2); JsonNode n1 = JsonParser.Parse(s1); JsonNode n2 = n1.Clone(); JsonNode n3 = JsonParser.Parse(n2.AsJson); JsonNode n4 = JsonParser.Parse(n3.AsJsonCompact); Assert.IsTrue(n1 != n2); CompareNodes(n1, n1); CompareNodes(n1, n2); CompareNodes(n1, n3); CompareNodes(n1, n4); }
public void TestWithConverter1() { var ser = new JsonSerializer(); const string prefix = "prefix: "; ser.RegisterConverter <DateTime>(x => prefix + x.ToString(@"yyyy\-MM\-dd HH\:mm\:ss", CultureInfo.InvariantCulture)); var o1 = new ExampleOuter(); o1.PropertyDateTime = new DateTime(1950, 7, 20, 12, 34, 56); string s = ser.Serialize(o1); Assert.IsTrue(s.Contains(prefix + o1.PropertyDateTime.ToString(@"yyyy\-MM\-dd HH\:mm\:ss", CultureInfo.InvariantCulture))); var des = new JsonDeserializer(); des.RegisterConverter <DateTime>(x => { string z = x.Substring(x.IndexOf(prefix) + prefix.Length); return(DateTime.Parse(z, CultureInfo.InvariantCulture)); }); var o2 = des.Deserialize <ExampleOuter>(s); Assert.AreEqual(o1.PropertyDateTime, o2.PropertyDateTime); }
static void TestStringConversion(JsonSerializer serializer, string expectedValue) { var o1 = new ExampleOuter(); string s = serializer.Serialize(o1); var o2 = new JsonDeserializer().Deserialize <ExampleOuter>(s); Assert.AreEqual(expectedValue, o2.ValueString); }
static void CompareSomeFields(ExampleOuter o1, ExampleOuter o2) { Assert.AreEqual(o1.ValueBoolean1, o2.ValueBoolean1); Assert.AreEqual(o1.ValueBoolean2, o2.ValueBoolean2); Assert.AreEqual(o1.ValueBoolean3, o2.ValueBoolean3); Assert.AreEqual(o1.ValueString, o2.ValueString); Assert.AreEqual(o1.PropertyInteger, o2.PropertyInteger); Assert.AreEqual(o1.PropertyDateTime, o2.PropertyDateTime); }
static ExampleOuter CreateExample() { var res = new ExampleOuter() { ValueString = "Test", ValueStringArray = new string[] { "A", "B,", "C" }, ValueDoubleArray = new double[] { 2, 3.14, 10000 }, PropertyInteger = 27, PropertyDateTime = new DateTime(2017, 12, 27, 14, 30, 0), }; res.ValueObject = new ExampleInner(2); res.ValueObjectArray = new ExampleInner[] { new ExampleInner(4), new ExampleInner(6) }; res.ChangePrivateValue(2.345f); return(res); }
public void TestObsoleteSerializeObject2() { ExampleOuter o1 = CreateExample(); var ser = new SLJsonSerializer(); #pragma warning disable 618 // Disable obsolete warning SLJsonWriter wr = ser.SerializeObject(o1); string s1 = ser.ToString(); #pragma warning restore 618 string s2 = wr.ToString(); Assert.AreEqual(s1, s2); var o2 = new SLJsonDeserializer().Deserialize <ExampleOuter>(s1); CompareSomeFields(o1, o2); }
public void Run() { Test = new UnitTestHelper(); Test.PrintHeadline("UnitTest3 - Reflection-based Serialization"); var o1 = new ExampleOuter() { ValueString = "Test", ValueStringArray = new string[] { "A", "B,", "C" }, ValueDoubleArray = new double[] { 2, 3.14, 10000 }, PropertyInteger = 27, PropertyDateTime = new DateTime(2017, 12, 27, 14, 30, 0), }; o1.ValueObject = new ExampleInner(2); o1.ValueObjectArray = new ExampleInner[] { new ExampleInner(4), new ExampleInner(6) }; o1.ChangePrivateValue(2.345f); string s1 = Serialize(o1); //Console.WriteLine(s1); var d = new SLJsonDeserializer(); ExampleOuter o2 = d.Deserialize <ExampleOuter>(s1); string s2 = Serialize(o1); Test.Assert(() => s1 == s2); SLJsonNode n1 = SLJsonParser.Parse(s1); SLJsonNode n2 = n1.Clone(); SLJsonNode n3 = SLJsonParser.Parse(n2.AsJson); SLJsonNode n4 = SLJsonParser.Parse(n3.AsJsonCompact); Test.Assert(() => n1 != n2); CompareNodes(n1, n1); CompareNodes(n1, n2); CompareNodes(n1, n3); CompareNodes(n1, n4); //Console.WriteLine(); Test.PrintSummary(); Test = null; }