コード例 #1
0
        public void ObjectEventsDocumentationExample()
        {
            SerializationEventTestObject obj = new SerializationEventTestObject();

            Assert.AreEqual(11, obj.Member1);
            Assert.AreEqual("Hello World!", obj.Member2);
            Assert.AreEqual("This is a nonserialized value", obj.Member3);
            Assert.AreEqual(null, obj.Member4);
            Assert.AreEqual(null, obj.Member5);

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

            StringAssert.AreEqual(@"{
  ""Member1"": 11,
  ""Member2"": ""This value went into the data file during serialization."",
  ""Member4"": null
}", json);

            Assert.AreEqual(11, obj.Member1);
            Assert.AreEqual("This value was reset after serialization.", obj.Member2);
            Assert.AreEqual("This is a nonserialized value", obj.Member3);
            Assert.AreEqual(null, obj.Member4);
            Assert.AreEqual("Error message for member Member6 = Error getting value from 'Member6' on 'Newtonsoft.Json.Tests.TestObjects.SerializationEventTestObject'.", obj.Member5);

            obj = JsonConvert.DeserializeObject <SerializationEventTestObject>(json);

            Assert.AreEqual(11, obj.Member1);
            Assert.AreEqual("This value went into the data file during serialization.", obj.Member2);
            Assert.AreEqual("This value was set during deserialization", obj.Member3);
            Assert.AreEqual("This value was set after deserialization.", obj.Member4);
            Assert.AreEqual(null, obj.Member5);
        }
コード例 #2
0
        public void ObjectEvents()
        {
            SerializationEventTestObject[] objs = new[] { new SerializationEventTestObject(), new DerivedSerializationEventTestObject() };

            foreach (SerializationEventTestObject current in objs)
            {
                SerializationEventTestObject obj = current;

                Assert.AreEqual(11, obj.Member1);
                Assert.AreEqual("Hello World!", obj.Member2);
                Assert.AreEqual("This is a nonserialized value", obj.Member3);
                Assert.AreEqual(null, obj.Member4);
                Assert.AreEqual(null, obj.Member5);

                string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
                StringAssert.AreEqual(@"{
  ""Member1"": 11,
  ""Member2"": ""This value went into the data file during serialization."",
  ""Member4"": null
}", json);

                Assert.AreEqual(11, obj.Member1);
                Assert.AreEqual("This value was reset after serialization.", obj.Member2);
                Assert.AreEqual("This is a nonserialized value", obj.Member3);
                Assert.AreEqual(null, obj.Member4);

                string expectedError = String.Format("Error message for member Member6 = Error getting value from 'Member6' on '{0}'.", obj.GetType().FullName);
                Assert.AreEqual(expectedError, obj.Member5);

                JObject o = JObject.Parse(@"{
  ""Member1"": 11,
  ""Member2"": ""This value went into the data file during serialization."",
  ""Member4"": null
}");
                o["Member6"] = "Dummy text for error";

                obj = (SerializationEventTestObject)JsonConvert.DeserializeObject(o.ToString(), obj.GetType());

                Assert.AreEqual(11, obj.Member1);
                Assert.AreEqual("This value went into the data file during serialization.", obj.Member2);
                Assert.AreEqual("This value was set during deserialization", obj.Member3);
                Assert.AreEqual("This value was set after deserialization.", obj.Member4);

                expectedError = String.Format("Error message for member Member6 = Error setting value to 'Member6' on '{0}'.", obj.GetType());
                Assert.AreEqual(expectedError, obj.Member5);

                DerivedSerializationEventTestObject derivedObj = obj as DerivedSerializationEventTestObject;
                if (derivedObj != null)
                {
                    Assert.AreEqual("This value was set after deserialization.", derivedObj.Member7);
                }
            }
        }
コード例 #3
0
        public void ObjectEventsDocumentationExample()
        {
            SerializationEventTestObject obj = new SerializationEventTestObject();

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // Hello World!
            Console.WriteLine(obj.Member3);
            // This is a nonserialized value
            Console.WriteLine(obj.Member4);
            // null
            Console.WriteLine(obj.Member5);
            // null

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

            // {
            //   "Member1": 11,
            //   "Member2": "This value went into the data file during serialization.",
            //   "Member4": null
            // }

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // This value was reset after serialization.
            Console.WriteLine(obj.Member3);
            // This is a nonserialized value
            Console.WriteLine(obj.Member4);
            // null
            Console.WriteLine(obj.Member5);
            // Error message for member Member6 = Exception has been thrown by the target of an invocation.

            obj = JsonConvert.DeserializeObject <SerializationEventTestObject>(json);

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // This value went into the data file during serialization.
            Console.WriteLine(obj.Member3);
            // This value was set during deserialization
            Console.WriteLine(obj.Member4);
            // This value was set after deserialization.
        }
コード例 #4
0
        public void SerializationCallbacksExample()
        {
            #region SerializationCallbacksExample
            SerializationEventTestObject obj = new SerializationEventTestObject();

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // Hello World!
            Console.WriteLine(obj.Member3);
            // This is a nonserialized value
            Console.WriteLine(obj.Member4);
            // null

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
            // {
            //   "Member1": 11,
            //   "Member2": "This value went into the data file during serialization.",
            //   "Member4": null
            // }

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // This value was reset after serialization.
            Console.WriteLine(obj.Member3);
            // This is a nonserialized value
            Console.WriteLine(obj.Member4);
            // null

            obj = JsonConvert.DeserializeObject <SerializationEventTestObject>(json);

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // This value went into the data file during serialization.
            Console.WriteLine(obj.Member3);
            // This value was set during deserialization
            Console.WriteLine(obj.Member4);
            // This value was set after deserialization.
            #endregion

            Assert.AreEqual(11, obj.Member1);
        }
コード例 #5
0
        public void Example()
        {
            #region Usage
            SerializationEventTestObject obj = new SerializationEventTestObject();

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // Hello World!
            Console.WriteLine(obj.Member3);
            // This is a nonserialized value
            Console.WriteLine(obj.Member4);
            // null

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
            // {
            //   "Member1": 11,
            //   "Member2": "This value went into the data file during serialization.",
            //   "Member4": null
            // }

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // This value was reset after serialization.
            Console.WriteLine(obj.Member3);
            // This is a nonserialized value
            Console.WriteLine(obj.Member4);
            // null

            obj = JsonConvert.DeserializeObject<SerializationEventTestObject>(json);

            Console.WriteLine(obj.Member1);
            // 11
            Console.WriteLine(obj.Member2);
            // This value went into the data file during serialization.
            Console.WriteLine(obj.Member3);
            // This value was set during deserialization
            Console.WriteLine(obj.Member4);
            // This value was set after deserialization.
            #endregion
        }
コード例 #6
0
        public void ObjectEvents()
        {
            SerializationEventTestObject obj = new SerializationEventTestObject();

            Assert.AreEqual(11, obj.Member1);
            Assert.AreEqual("Hello World!", obj.Member2);
            Assert.AreEqual("This is a nonserialized value", obj.Member3);
            Assert.AreEqual(null, obj.Member4);
            Assert.AreEqual(null, obj.Member5);

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

            Assert.AreEqual(@"{
  ""Member1"": 11,
  ""Member2"": ""This value went into the data file during serialization."",
  ""Member4"": null
}", json);

            Assert.AreEqual(11, obj.Member1);
            Assert.AreEqual("This value was reset after serialization.", obj.Member2);
            Assert.AreEqual("This is a nonserialized value", obj.Member3);
            Assert.AreEqual(null, obj.Member4);
            Assert.AreEqual("Error message for member Member6 = Exception has been thrown by the target of an invocation.", obj.Member5);

            JObject o = JObject.Parse(@"{
  ""Member1"": 11,
  ""Member2"": ""This value went into the data file during serialization."",
  ""Member4"": null
}");

            o["Member6"] = "Dummy text for error";

            obj = JsonConvert.DeserializeObject <SerializationEventTestObject>(o.ToString());

            Assert.AreEqual(11, obj.Member1);
            Assert.AreEqual("This value went into the data file during serialization.", obj.Member2);
            Assert.AreEqual("This value was set during deserialization", obj.Member3);
            Assert.AreEqual("This value was set after deserialization.", obj.Member4);
            Assert.AreEqual("Error message for member Member6 = Exception has been thrown by the target of an invocation.", obj.Member5);
        }
コード例 #7
0
        public static void TestSerializationCallbacks()
        {
            SerializationEventTestObject obj = new SerializationEventTestObject();

            Assert.AreEqual(11, obj.Member1, "Int member initial value is the expected one.");
            Assert.AreEqual("Hello World!", obj.Member2, "String member initial value is the expected one.");
            Assert.AreEqual("This is a nonserialized value.", obj.Member3, "Json-ignored-string member initial value is the expected one.");
            Assert.Null(obj.Member4, "Null string member initial value is the expected one.");

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

            Assert.AreEqual(11, obj.Member1, "Member value is unchanged after serialization.");
            Assert.AreEqual("This value was reset after serialization.", obj.Member2, "Member changed on 'Serialized' is changed accordingly after serialization.");
            Assert.AreEqual("This is a nonserialized value.", obj.Member3, "Member with deserialization-changing event is unchanged after serialization.");
            Assert.Null(obj.Member4, "Member with deserialization-changing event is unchanged after serialization.");

            obj = JsonConvert.DeserializeObject <SerializationEventTestObject>(json);

            Assert.AreEqual(11, obj.Member1, "Member value is unchanged after deserialization.");
            Assert.AreEqual("This value went into the data file during serialization.", obj.Member2, "Member changed on 'Serializing' is changed accordingly after deserialization.");
            Assert.AreEqual("This value was set during deserialization.", obj.Member3, "Member with 'Deserializing' event is changed accordingly after deserialization.");
            Assert.AreEqual("This value was set after deserialization.", obj.Member4, "Member with 'Deserialized' event is changed accordingly after deserialization.");
        }