示例#1
0
        public void ReadJson_IfTypeNameIsPresent_ReturnsObjectOfTypeNameType()
        {
            // Arrange
            string typePropertyName = "TheTypeProperty";
            string typeName         = "TheTypeName";
            IDictionary <string, Type> typeMapping = new Dictionary <string, Type>
            {
                { typeName, typeof(TypeWithFoo) }
            };
            JsonConverter product          = CreateProductUnderTest(typePropertyName, typeMapping);
            string        expectedFooValue = "IgnoreFoo";
            JObject       json             = new JObject();

            json.Add(typePropertyName, new JValue(typeName));
            json.Add("Foo", new JValue(expectedFooValue));

            using (JsonReader reader = CreateReader(json))
            {
                Type           objectType    = typeof(object);
                object         existingValue = null;
                JsonSerializer serializer    = new JsonSerializer();

                // Act
                object value = product.ReadJson(reader, objectType, existingValue, serializer);

                // Assert
                Assert.IsType <TypeWithFoo>(value);
                TypeWithFoo valueWithFoo = (TypeWithFoo)value;
                Assert.Equal(expectedFooValue, valueWithFoo.Foo);
            }
        }
示例#2
0
        public void WriteJson_IfTypeIsNotInMapping_DoesNotAddTypeName()
        {
            // Arrange
            string        expectedFooValue = "TheFoo";
            JsonConverter product          = CreateProductUnderTest();
            JToken        token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                TypeWithFoo value = new TypeWithFoo
                {
                    Foo = expectedFooValue
                };
                JsonSerializer serializer = new JsonSerializer();

                // Act
                product.WriteJson(writer, value, serializer);

                token = writer.Token;
            }

            // Assert
            string  json           = GetJson(token);
            JObject expectedObject = new JObject();

            expectedObject.Add("Foo", expectedFooValue);
            string expectedJson = GetJson(expectedObject);

            Assert.Equal(expectedJson, json);
        }
示例#3
0
        public void WriteJson_AddsTypeNameToObject()
        {
            // Arrange
            string typePropertyName = "TypeProperty";
            string expectedTypeName = "TheTypeName";
            string expectedFooValue = "TheFoo";
            IDictionary <string, Type> typeMapping = new Dictionary <string, Type>
            {
                { expectedTypeName, typeof(TypeWithFoo) }
            };
            JsonConverter product = CreateProductUnderTest(typePropertyName, typeMapping);
            JToken        token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                TypeWithFoo value = new TypeWithFoo
                {
                    Foo = expectedFooValue
                };
                JsonSerializer serializer = new JsonSerializer();

                // Act
                product.WriteJson(writer, value, serializer);

                token = writer.Token;
            }

            // Assert
            string  json           = GetJson(token);
            JObject expectedObject = new JObject();

            expectedObject.Add(typePropertyName, new JValue(expectedTypeName));
            expectedObject.Add("Foo", expectedFooValue);
            string expectedJson = GetJson(expectedObject);

            Assert.Equal(expectedJson, json);
        }
示例#4
0
        public void ReadJson_IfTypeNameIsMissing_ReturnsObjectOfObjectType()
        {
            // Arrange
            JsonConverter product          = CreateProductUnderTest();
            string        expectedFooValue = "IgnoreFoo";
            JObject       json             = new JObject();

            json.Add("Foo", new JValue(expectedFooValue));

            using (JsonReader reader = CreateReader(json))
            {
                Type           objectType    = typeof(TypeWithFoo);
                object         existingValue = null;
                JsonSerializer serializer    = new JsonSerializer();

                // Act
                object value = product.ReadJson(reader, objectType, existingValue, serializer);

                // Assert
                Assert.IsType <TypeWithFoo>(value);
                TypeWithFoo valueWithFoo = (TypeWithFoo)value;
                Assert.Equal(expectedFooValue, valueWithFoo.Foo);
            }
        }
        public void WriteJson_IfTypeIsNotInMapping_DoesNotAddTypeName()
        {
            // Arrange
            string expectedFooValue = "TheFoo";
            JsonConverter product = CreateProductUnderTest();
            JToken token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                TypeWithFoo value = new TypeWithFoo
                {
                    Foo = expectedFooValue
                };
                JsonSerializer serializer = new JsonSerializer();

                // Act
                product.WriteJson(writer, value, serializer);

                token = writer.Token;
            }

            // Assert
            string json = GetJson(token);
            JObject expectedObject = new JObject();
            expectedObject.Add("Foo", expectedFooValue);
            string expectedJson = GetJson(expectedObject);
            Assert.Equal(expectedJson, json);
        }
        public void WriteJson_AddsTypeNameToObject()
        {
            // Arrange
            string typePropertyName = "TypeProperty";
            string expectedTypeName = "TheTypeName";
            string expectedFooValue = "TheFoo";
            IDictionary<string, Type> typeMapping = new Dictionary<string, Type>
            {
                { expectedTypeName, typeof(TypeWithFoo) }
            };
            JsonConverter product = CreateProductUnderTest(typePropertyName, typeMapping);
            JToken token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                TypeWithFoo value = new TypeWithFoo
                {
                    Foo = expectedFooValue
                };
                JsonSerializer serializer = new JsonSerializer();

                // Act
                product.WriteJson(writer, value, serializer);

                token = writer.Token;
            }

            // Assert
            string json = GetJson(token);
            JObject expectedObject = new JObject();
            expectedObject.Add(typePropertyName, new JValue(expectedTypeName));
            expectedObject.Add("Foo", expectedFooValue);
            string expectedJson = GetJson(expectedObject);
            Assert.Equal(expectedJson, json);
        }