Example #1
0
        public void SerializeNestedObject_WhenAnObjectIsMissing_SupportsDefaultValueFactoryAtAllDepthLevels()
        {
            var house = new House
            {
                RoofColor       = "someColor",
                NumberOfWindows = 5,
                Room            = null,
            };

            var jsonString   = JsonConvert.SerializeObject(house);
            var expectedJson = new JObject
            {
                { "NumberOfWindows", 5 },
                { "RoofColor", "someColor" },
                {
                    "Room", new JObject
                    {
                        { "CarpetColor", "" },
                        {
                            "Bed", new JObject
                            {
                                { "NumberOfLegs", 0 },
                                { "HasHeadboard", false },
                                { "CoverColor", "" },
                            }
                        },
                    }
                },
            };

            var json = JToken.Parse(jsonString);

            Test.AssertSameJson(expectedJson, json);
        }
        public void SerializationAndDeserialization_RespectsNewtonsoftAttributes()
        {
            var obj = new ExampleObject()
            {
                ShouldNotSerialize           = "value should not get serialized",
                ShouldIncludeWhenSerializing = "value should get serialized",
            };

            var jsonString   = JsonConvert.SerializeObject(obj);
            var expectedJson = new JObject
            {
                { "ShouldIncludeWhenSerializing", "value should get serialized" },
            };

            var json = JToken.Parse(jsonString);

            Test.AssertSameJson(expectedJson, json);
        }