public void CompoundNullableTypesImportTest()
        {
            string     json   = @" {
                ""TestNested"": {
                    ""TestValue"": 42
                }
            }";
            JsonReader reader = new JsonReader(json);

            reader.SkipNonMembers = false;
            CompoundNullableTypesTest value = JsonMapper.ToObject <CompoundNullableTypesTest>(reader);

            Assert.AreNotEqual(value.TestNested, null);
            CompoundNullableType <int> innerValue = (CompoundNullableType <int>)value.TestNested;

            Assert.AreEqual(innerValue.TestValue, 42);

            json  = @" {
                ""TestNested"": null
            }";
            value = JsonMapper.ToObject <CompoundNullableTypesTest>(json);
            Assert.AreEqual(value.TestNested, null);

            json  = @" {
                ""TestNullableTypeArray"": [
                    { ""TestValue"": 42 },
                    { ""TestValue"": 43 },
                    { ""TestValue"": 44 }
                ]
            }";
            value = JsonMapper.ToObject <CompoundNullableTypesTest>(json);
            Assert.AreNotEqual(value.TestNullableTypeArray, null);
            Assert.AreEqual(value.TestNullableTypeArray.Length, 3);
            Assert.AreNotEqual(value.TestNullableTypeArray[0], null);
            innerValue = (CompoundNullableType <int>)value.TestNullableTypeArray[0];
            Assert.AreEqual(innerValue.TestValue, 42);
            Assert.AreNotEqual(value.TestNullableTypeArray[1], null);
            innerValue = (CompoundNullableType <int>)value.TestNullableTypeArray[1];
            Assert.AreEqual(innerValue.TestValue, 43);
            Assert.AreNotEqual(value.TestNullableTypeArray[2], null);
            innerValue = (CompoundNullableType <int>)value.TestNullableTypeArray[2];
            Assert.AreEqual(innerValue.TestValue, 44);
        }
Beispiel #2
0
        public void CompoundNullableTypesExportTest()
        {
            CompoundNullableTypesTest value = new CompoundNullableTypesTest()
            {
                TestNested = new CompoundNullableType <int>()
                {
                    TestValue = 42
                }
            };
            var expectedJson =
                "{\"TestNested\":{\"TestValue\":42}," +
                "\"TestNullableTypeArray\":null}";

            Assert.AreEqual(expectedJson, JsonMapper.ToJson(value));

            value = new CompoundNullableTypesTest()
            {
                TestNullableTypeArray = new[] {
                    new Nullable <CompoundNullableType <int> >(
                        new CompoundNullableType <int>()
                    {
                        TestValue = 42
                    }),
                    new Nullable <CompoundNullableType <int> >(
                        new CompoundNullableType <int>()
                    {
                        TestValue = 43
                    }),
                    new Nullable <CompoundNullableType <int> >(
                        new CompoundNullableType <int>()
                    {
                        TestValue = 44
                    })
                }
            };
            expectedJson =
                "{\"TestNested\":null,\"TestNullableTypeArray\":" +
                "[{\"TestValue\":42},{\"TestValue\":43},{\"TestValue\":44}]}";
            Assert.AreEqual(expectedJson, JsonMapper.ToJson(value));
        }