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()));
        }
示例#2
0
        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));
            }
        }