コード例 #1
0
        public void WriteJson_IfTypeNamePropertyAlreadyExists_ReplacesExistingProperty()
        {
            // Arrange
            string expectedTypeName = "OfficialTypeName";
            string expectedFooValue = "TheFoo";
            IDictionary <string, Type> typeMapping = new Dictionary <string, Type>
            {
                { expectedTypeName, typeof(TypeWithFooAndType) }
            };
            JsonConverter product = CreateProductUnderTest("Type", typeMapping);
            JToken        token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                TypeWithFooAndType value = new TypeWithFooAndType
                {
                    Type = "OtherTypeName",
                    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("Type", new JValue(expectedTypeName));
            expectedObject.Add("Foo", expectedFooValue);
            string expectedJson = GetJson(expectedObject);

            Assert.Equal(expectedJson, json);
        }
コード例 #2
0
        public void WriteJson_IfTypeNamePropertyAlreadyExists_ReplacesExistingProperty()
        {
            // Arrange
            string expectedTypeName = "OfficialTypeName";
            string expectedFooValue = "TheFoo";
            IDictionary<string, Type> typeMapping = new Dictionary<string, Type>
            {
                { expectedTypeName, typeof(TypeWithFooAndType) }
            };
            JsonConverter product = CreateProductUnderTest("Type", typeMapping);
            JToken token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                TypeWithFooAndType value = new TypeWithFooAndType
                {
                    Type = "OtherTypeName",
                    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("Type", new JValue(expectedTypeName));
            expectedObject.Add("Foo", expectedFooValue);
            string expectedJson = GetJson(expectedObject);
            Assert.Equal(expectedJson, json);
        }