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 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]); } }
protected string SerializeJson(object value) { JsonSerializer serializer = new JsonSerializer(); StringBuilder result = new StringBuilder(); using (ExtentedStringWriter sw = new ExtentedStringWriter(result, new UTF8Encoding(false))) { serializer.Serialize(sw, value); } DebugWriteLine(result.ToString()); return(result.ToString()); }
protected string DivideXml(Type t, object oriValue, object revValue, JsonSerializeSetting setting = null) { Serializer serializer = new Serializer(t); StringBuilder result = new StringBuilder(); using (ExtentedStringWriter sw = new ExtentedStringWriter(result, new UTF8Encoding(false))) { using (XmlTextWriter xmlWriter = new XmlTextWriter(sw)) { if (setting != null) { xmlWriter.Setting = setting; } serializer.Divide(xmlWriter, oriValue, revValue); } } DebugWriteLine(result.ToString()); return(result.ToString()); }