public void TestActionName() { string[] s1 = new string[2] { TestHelper.RandomString(10, false, true, true, false, string.Empty), TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string[] s2 = new string[2] { s1[0], TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string jsonContext = DivideJson(s1.GetType(), s1, s2, new JsonSerializeSetting { ActionName = "ACT" }); string xmlContext = DivideXml(s1.GetType(), s1, s2, new JsonSerializeSetting { ActionName = "ACT" }); AssertJsonString(xmlContext, jsonContext); string[] s3 = CombineJson <string[]>(jsonContext, s1, new JsonSerializeSetting { ActionName = "ACT" }); Assert.AreEqual(s3.Length, s2.Length); for (int i = 0; i < s3.Length; i++) { Assert.AreEqual(s3[i], s2[i]); } }
public void TestAddressInfoEdit() { AddressInfo s1 = new AddressInfo { AddressId = Guid.NewGuid(), City = TestHelper.RandomString(10, false, true, true, false, string.Empty), Country = TestHelper.RandomString(10, false, true, true, false, string.Empty), Phone = TestHelper.RandomString(10, false, true, true, false, string.Empty), State = TestHelper.RandomString(10, false, true, true, false, string.Empty), Zip = TestHelper.RandomString(10, false, true, true, false, string.Empty) }; AddressInfo s2 = new AddressInfo { AddressId = s1.AddressId, City = TestHelper.RandomString(10, false, true, true, false, string.Empty), Country = s1.Country, Phone = TestHelper.RandomString(10, false, true, true, false, string.Empty), State = s1.State, Zip = TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string jsonContext = DivideJson(typeof(AddressInfo), s1, s2); string xmlContext = DivideXml(typeof(AddressInfo), s1, s2); AssertJsonString(xmlContext, jsonContext); AddressInfo s3 = CombineJson <AddressInfo>(jsonContext, s1); Assert.AreEqual(s2, s3); }
public void TestSAIN() { string[] s1 = new string[2] { TestHelper.RandomString(10, false, true, true, false, string.Empty), TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string[] s2 = new string[2] { s1[0], TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string jsonContext = DivideJson(s1.GetType(), s1, s2, new JsonSerializeSetting { SAIN = "VALUE" }); string xmlContext = DivideXml(s1.GetType(), s1, s2, new JsonSerializeSetting { SAIN = "VALUE" }); xmlContext = xmlContext.Substring(40); string jsonXmlContext = Convert(xmlContext); Assert.AreEqual(jsonXmlContext.Replace("#text", "VALUE"), jsonContext); string[] s3 = CombineJson <string[]>(jsonContext, s1, new JsonSerializeSetting { SAIN = "VALUE" }); Assert.AreEqual(s3.Length, s2.Length); for (int i = 0; i < s3.Length; i++) { Assert.AreEqual(s3[i], s2[i]); } }
private void TestJsonTextWriterProperties(TestClass instance, string context, JsonSerializeSetting setting = null) { string xmlContext = DivideXml(instance.GetType(), null, instance, setting); string convertContext = Convert(xmlContext); StringBuilder sb = new StringBuilder(); Serializer serializer = new Serializer(typeof(TestClass)); using (ExtentedStringWriter strWriter = new ExtentedStringWriter(sb, new UTF8Encoding(false))) { using (JsonTextWriter writer = new JsonTextWriter(strWriter)) { if (setting != null) { writer.Setting = setting; } serializer.Divide(writer, null, instance); } } DebugWriteLine(sb.ToString()); Assert.AreEqual(context, sb.ToString()); TestClass newInstance = CombineJson <TestClass>(sb.ToString(), null, setting); Assert.IsTrue(string.Equals(instance.Str, newInstance.Str, StringComparison.Ordinal)); Assert.IsTrue(decimal.Equals(instance.Number, newInstance.Number)); Assert.IsTrue(DateTime.Equals(instance.DT.ToUniversalTime(), newInstance.DT.ToUniversalTime())); }
public void TestSimpleObjectArrayAdd() { SimpleClass[] s = { new SimpleClass { S = TestHelper.RandomString(10, false, true, true, false, string.Empty) }, new SimpleClass { S = TestHelper.RandomString(10, false, true, true, false, string.Empty) } }; string jsonContext = DivideJson(s.GetType(), null, s); string xmlContext = DivideXml(s.GetType(), null, s); AssertJsonString(xmlContext, jsonContext); SimpleClass[] n = CombineJson <SimpleClass[]>(jsonContext, null); Assert.AreEqual(s.Length, n.Length); for (int i = 0; i < s.Length; i++) { Assert.AreEqual(n[i].S, s[i].S); } }
public void TestSimpleObjectArray() { SimpleClass[] s1 = { new SimpleClass { S = TestHelper.RandomString(10, false, true, true, false, string.Empty) }, new SimpleClass { S = TestHelper.RandomString(10, false, true, true, false, string.Empty) } }; SimpleClass[] s2 = { s1[0], new SimpleClass { S = TestHelper.RandomString(10,false, true, true, false, string.Empty) } }; string jsonContext = DivideJson(s1.GetType(), s1, s2); string xmlContext = DivideXml(s1.GetType(), s1, s2); AssertJsonString(xmlContext, jsonContext); SimpleClass[] s3 = CombineJson <SimpleClass[]>(jsonContext, s1); Assert.AreEqual(s3.Length, s2.Length); for (int i = 0; i < s3.Length; i++) { Assert.AreEqual(s3[i].S, s2[i].S); } }
protected void AssertJsonString(string xml, string json) { xml = xml.Substring(xml.IndexOf("<", 1)); string jsonXmlContext = Convert(xml); Assert.AreEqual(jsonXmlContext, json); }
public void TestAddressInfoArray() { List <AddressInfo> s1 = new List <AddressInfo>(); s1.Add( new AddressInfo { AddressId = Guid.NewGuid(), City = TestHelper.RandomString(10, false, true, true, false, string.Empty), Country = TestHelper.RandomString(10, false, true, true, false, string.Empty), Phone = TestHelper.RandomString(10, false, true, true, false, string.Empty), State = TestHelper.RandomString(10, false, true, true, false, string.Empty), Zip = TestHelper.RandomString(10, false, true, true, false, string.Empty) }); s1.Add( new AddressInfo { AddressId = Guid.NewGuid(), City = TestHelper.RandomString(10, false, true, true, false, string.Empty), Country = TestHelper.RandomString(10, false, true, true, false, string.Empty), Phone = TestHelper.RandomString(10, false, true, true, false, string.Empty), State = TestHelper.RandomString(10, false, true, true, false, string.Empty), Zip = TestHelper.RandomString(10, false, true, true, false, string.Empty) }); List <AddressInfo> s2 = new List <AddressInfo>(); s2.Add(s1[0].Clone() as AddressInfo); s2.Add(s1[1].Clone() as AddressInfo); s2[0].City = string.Concat(s2[0].City, TestHelper.RandomString(10, false, true, true, false, string.Empty)); s2.RemoveAt(1); s2.Add( new AddressInfo { AddressId = Guid.NewGuid(), City = TestHelper.RandomString(10, false, true, true, false, string.Empty), Country = TestHelper.RandomString(10, false, true, true, false, string.Empty), Phone = TestHelper.RandomString(10, false, true, true, false, string.Empty), State = TestHelper.RandomString(10, false, true, true, false, string.Empty), Zip = TestHelper.RandomString(10, false, true, true, false, string.Empty) }); string jsonContext = DivideJson(s1.GetType(), s1, s2); string xmlContext = DivideXml(s1.GetType(), s1, s2); AssertJsonString(xmlContext, jsonContext); List <AddressInfo> s3 = CombineJson <List <AddressInfo> >(jsonContext, s1); Assert.AreEqual(s3.Count, s2.Count); for (int i = 0; i < s3.Count; i++) { Assert.AreEqual(s3[i], s2[i]); } }
public void TestNotIndent() { string[] s1 = new string[2] { TestHelper.RandomString(10, false, true, true, false, string.Empty), TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string[] s2 = new string[2] { s1[0], TestHelper.RandomString(10, false, true, true, false, string.Empty) }; Serializer serializer = new Serializer(typeof(string[])); StringBuilder jsonResult = new StringBuilder(); using (ExtentedStringWriter sw = new ExtentedStringWriter(jsonResult, new UTF8Encoding(false))) { using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.None; serializer.Divide(jsonWriter, s1, s2); } } DebugWriteLine(jsonResult.ToString()); string jsonContext = jsonResult.ToString(); StringBuilder xmlResult = new StringBuilder(); using (ExtentedStringWriter sw = new ExtentedStringWriter(xmlResult, new UTF8Encoding(false))) { using (XmlTextWriter xmlWriter = new XmlTextWriter(sw)) { xmlWriter.Formatting = Formatting.None; serializer.Divide(xmlWriter, s1, s2); } } DebugWriteLine(xmlResult.ToString()); string xmlContext = xmlResult.ToString(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlContext.Substring(xmlContext.IndexOf("<", 1))); string jsonXmlContext = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None); Assert.AreEqual(jsonXmlContext, jsonContext); string[] s3 = CombineJson <string[]>(jsonContext, s1); Assert.AreEqual(s3.Length, s2.Length); for (int i = 0; i < s3.Length; i++) { Assert.AreEqual(s3[i], s2[i]); } }
public void TestBasic() { string s = TestHelper.RandomString(10, false, true, true, false, string.Empty); string jsonContext = DivideJson(s.GetType(), string.Empty, s); string xmlContext = DivideXml(s.GetType(), string.Empty, s); AssertJsonString(xmlContext, jsonContext); string n = CombineJson <string>(jsonContext, string.Empty); Assert.AreEqual(s, n); }
public void TestClone() { JsonSerializeSetting c = new JsonSerializeSetting(); c.IgnoreAttributeType = typeof(string); c.SAIN = new Random().Next(Int32.MinValue, Int32.MaxValue).ToString(); c.ActionName = new Random().Next(Int32.MinValue, Int32.MaxValue).ToString(); #if NET_40_UP || NETSTANDARD_2_0_UP c.AssemblyQualifiedName = new Random().Next(Int32.MinValue, Int32.MaxValue).ToString(); #endif #if NET || NETSTANDARD_2_0_UP c.EnableOnDeserializedAttribute = !c.EnableOnDeserializedAttribute; c.EnableOnDeserializingAttribute = !c.EnableOnDeserializingAttribute; c.EnableOnSerializedAttribute = !c.EnableOnSerializedAttribute; c.EnableOnSerializingAttribute = !c.EnableOnSerializingAttribute; #endif foreach (string name in Enum.GetNames(typeof(SerializeMemberType))) { SerializeMemberType t = (SerializeMemberType)Enum.Parse(typeof(SerializeMemberType), name); if (c.MemberType != t) { c.MemberType = t; } } foreach (string name in Enum.GetNames(typeof(DateTimeSerializationMode))) { DateTimeSerializationMode t = (DateTimeSerializationMode)Enum.Parse(typeof(DateTimeSerializationMode), name); if (c.Mode != t) { c.Mode = t; } } foreach (string name in Enum.GetNames(typeof(SerializeMemberModifier))) { SerializeMemberModifier t = (SerializeMemberModifier)Enum.Parse(typeof(SerializeMemberModifier), name); if (c.Modifier != t) { c.Modifier = t; } } c.SerializeDefalutValue = !c.SerializeDefalutValue; c.ActionName = new Random().Next(Int32.MinValue, Int32.MaxValue).ToString(); JsonSerializeSetting c_new = c.Clone() as JsonSerializeSetting; PropertyInfo[] pis = typeof(JsonSerializeSetting).GetProperties(); foreach (PropertyInfo pi in pis) { Assert.AreEqual(pi.GetValue(c, null), pi.GetValue(c_new, null)); } }
public void TestSimpleObject() { SimpleClass s = new SimpleClass { S = TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string jsonContext = DivideJson(s.GetType(), null, s); string xmlContext = DivideXml(s.GetType(), null, s); AssertJsonString(xmlContext, jsonContext); SimpleClass n = CombineJson <SimpleClass>(jsonContext, null); Assert.AreEqual(s.S, n.S); }
public void TestAddressInfoEditFormNull() { AddressInfo s = new AddressInfo { AddressId = Guid.NewGuid(), City = TestHelper.RandomString(10, false, true, true, false, string.Empty), Country = TestHelper.RandomString(10, false, true, true, false, string.Empty), Phone = TestHelper.RandomString(10, false, true, true, false, string.Empty), State = TestHelper.RandomString(10, false, true, true, false, string.Empty), Zip = TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string jsonContext = DivideJson(s.GetType(), null, s); string xmlContext = DivideXml(s.GetType(), null, s); AssertJsonString(xmlContext, jsonContext); AddressInfo n = CombineJson <AddressInfo>(jsonContext, null); Assert.AreEqual(s, n); }
public void TestStringArrayAdd() { string[] s = new string[2] { TestHelper.RandomString(10, false, true, true, false, string.Empty), TestHelper.RandomString(10, false, true, true, false, string.Empty) }; string jsonContext = DivideJson(s.GetType(), null, s); string xmlContext = DivideXml(s.GetType(), null, s); AssertJsonString(xmlContext, jsonContext); string[] n = CombineJson <string[]>(jsonContext, null); Assert.AreEqual(s.Length, n.Length); for (int i = 0; i < s.Length; i++) { Assert.AreEqual(s[i], n[i]); } }
public void TestJsonSerializeSetting() { JsonSerializeSetting setting = new JsonSerializeSetting(); setting.SAIN = "SAIN"; setting.ActionName = "ACTIONNAME"; setting.Mode = DateTimeSerializationMode.Local; setting.SerializeDefalutValue = true; Assert.AreEqual("SAIN", setting.SAIN); Assert.AreEqual("ACTIONNAME", setting.ActionName); Assert.IsTrue(setting.SerializeDefalutValue); Assert.AreEqual(DateTimeSerializationMode.Local, setting.Mode); JsonSerializeSetting newSetting = setting.Clone() as JsonSerializeSetting; Assert.IsNotNull(newSetting); PropertyInfo[] pi = typeof(JsonSerializeSetting).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); foreach (PropertyInfo info in pi) { Assert.AreEqual(info.GetValue(setting, null), info.GetValue(newSetting, null)); } }