Exemplo n.º 1
0
        public void TestComplexSerialization()
        {
            var fixture = new ConfigurationFixture();

            var rootElement = fixture.RootElement;

            var serializer = JsonSerializer.Create(_settings);

            using (var textWriter = new StringWriter())
            {
                using (var jsonWriter = new JsonTextWriter(textWriter))
                {
                    serializer.Serialize(jsonWriter, rootElement);

                    var jsonString = textWriter.ToString();

                    const string x = "{\"booleanPropertyA\":true,\"booleanPropertyB\":false,\"bytePropertyA\":1,\"bytePropertyB\":123,\"charPropertyA\":\"a\",\"charPropertyB\":\"B\",\"childCollection\":[{\"name\":\"Item_0\",\"value\":0},{\"name\":\"Item_1\",\"value\":1},{\"name\":\"Item_2\",\"value\":2}],\"childDictionary\":{\"Item1\":{\"name\":\"Item_1\",\"value\":1},\"Item2\":{\"name\":\"Item_2\",\"value\":2},\"Item3\":{\"name\":\"Item_3\",\"value\":3}},\"childElementProperty\":{\"name\":\"NAME-1\",\"value\":1},\"customProperty\":\"XX_XX\",\"customValueA\":\"CUSTOM-VALUE-A\",\"customValueB\":\"DEFAULT_VALUE\",\"customValueC\":\"DEFAULT_VALUE\",\"dateTimeOffsetPropertyA\":\"2020-01-10T18:00:30+03:00\",\"dateTimeOffsetPropertyB\":\"2019-10-01T14:30:15+03:00\",\"dateTimePropertyA\":\"2020-01-10T18:00:30\",\"dateTimePropertyB\":\"2019-10-01T14:30:15\",\"decimalPropertyA\":555.666,\"decimalPropertyB\":-666.777,\"doublePropertyA\":555.666,\"doublePropertyB\":-666.777,\"enumPropertyA\":16,\"enumPropertyB\":4,\"int16PropertyA\":333,\"int16PropertyB\":-444,\"int32PropertyA\":333,\"int32PropertyB\":-444,\"int32PropertyC\":999,\"int32PropertyD\":555,\"int64PropertyA\":333,\"int64PropertyB\":-444,\"nonFlagsEnumPropertyA\":1,\"nonFlagsEnumPropertyB\":3,\"readOnlyChildCollection\":[],\"readOnlyChildDictionary\":{},\"readOnlyCollection\":[],\"readOnlyDictionary\":[],\"sBytePropertyA\":99,\"sBytePropertyB\":-100,\"singlePropertyA\":555.666,\"singlePropertyB\":-666.777,\"singlePropertyNoDefault\":-666.777,\"singlePropertyWithDefault\":555.666,\"stringPropertyA\":\"111\",\"stringPropertyB\":\"222\",\"stringPropertyC\":\"222\",\"timeSpanPropertyA\":\"0.04:00:10:\",\"timeSpanPropertyB\":\"0.00:30:30:\",\"testDefaults\":false}";

                    var objX = JToken.Parse(x);
                    var objY = JToken.Parse(jsonString);

                    var bagX = JsonDocumentToPropertyBag(objX);
                    var bagY = JsonDocumentToPropertyBag(objY);

                    // We can't just compare strings because the ordering of properties is not guaranteed, so instead we
                    // must deserialize the strings and use a deep equals that does not pay attention to property ordering.
                    Assert.True(EqualityHelper.Equals(bagX, bagY));
                }
            }
        }
        public void TestComplexSerialization()
        {
            // BUG: Until this is fixed in corefx we must manually add the converter: https://github.com/dotnet/runtime/issues/31007
            var serializerOptions = new JsonSerializerOptions
            {
                Converters =
                {
                    new OpenCollar.Extensions.Configuration.Converters.Text.Json.ConfigurationDictionaryConverterFactory()
                }
            };

            var fixture = new ConfigurationFixture();

            var rootElement = fixture.RootElement;

            var jsonString = JsonSerializer.Serialize(rootElement, serializerOptions);

            const string x = "{\"BooleanPropertyA\":true,\"BooleanPropertyB\":false,\"BytePropertyA\":1,\"BytePropertyB\":123,\"CharPropertyA\":\"a\",\"CharPropertyB\":\"B\",\"ChildCollection\":[{\"Name\":\"Item_0\",\"Value\":0},{\"Name\":\"Item_1\",\"Value\":1},{\"Name\":\"Item_2\",\"Value\":2}],\"ChildDictionary\":{\"Item1\":{\"Name\":\"Item_1\",\"Value\":1},\"Item2\":{\"Name\":\"Item_2\",\"Value\":2},\"Item3\":{\"Name\":\"Item_3\",\"Value\":3}},\"ChildElementProperty\":{\"Name\":\"NAME-1\",\"Value\":1},\"CustomProperty\":\"XX_XX\",\"CustomValueA\":\"CUSTOM-VALUE-A\",\"CustomValueB\":\"DEFAULT_VALUE\",\"CustomValueC\":\"DEFAULT_VALUE\",\"DateTimeOffsetPropertyA\":\"2020-01-10T18:00:30+03:00\",\"DateTimeOffsetPropertyB\":\"2019-10-01T14:30:15+03:00\",\"DateTimePropertyA\":\"2020-01-10T18:00:30\",\"DateTimePropertyB\":\"2019-10-01T14:30:15\",\"DecimalPropertyA\":555.666,\"DecimalPropertyB\":-666.777,\"DoublePropertyA\":555.666,\"DoublePropertyB\":-666.777,\"EnumPropertyA\":16,\"EnumPropertyB\":4,\"Int16PropertyA\":333,\"Int16PropertyB\":-444,\"Int32PropertyA\":333,\"Int32PropertyB\":-444,\"Int32PropertyC\":999,\"Int32PropertyD\":555,\"Int64PropertyA\":333,\"Int64PropertyB\":-444,\"NonFlagsEnumPropertyA\":1,\"NonFlagsEnumPropertyB\":3,\"ReadOnlyChildCollection\":[],\"ReadOnlyChildDictionary\":[],\"ReadOnlyCollection\":[],\"ReadOnlyDictionary\":[],\"SBytePropertyA\":99,\"SBytePropertyB\":-100,\"SinglePropertyA\":555.666,\"SinglePropertyB\":-666.777,\"SinglePropertyNoDefault\":-666.777,\"SinglePropertyWithDefault\":555.666,\"StringPropertyA\":\"111\",\"StringPropertyB\":\"222\",\"StringPropertyC\":\"222\",\"TimeSpanPropertyA\":{\"Ticks\":144100000000,\"Days\":0,\"Hours\":4,\"Milliseconds\":0,\"Minutes\":0,\"Seconds\":10,\"TotalDays\":0.1667824074074074,\"TotalHours\":4.002777777777778,\"TotalMilliseconds\":14410000,\"TotalMinutes\":240.16666666666666,\"TotalSeconds\":14410},\"TimeSpanPropertyB\":{\"Ticks\":18300000000,\"Days\":0,\"Hours\":0,\"Milliseconds\":0,\"Minutes\":30,\"Seconds\":30,\"TotalDays\":0.021180555555555557,\"TotalHours\":0.5083333333333333,\"TotalMilliseconds\":1830000,\"TotalMinutes\":30.5,\"TotalSeconds\":1830},\"TestDefaults\":false}";

            var objX = JsonDocument.Parse(x);
            var objY = JsonDocument.Parse(jsonString);

            var bagX = JsonDocumentToPropertyBag(objX);
            var bagY = JsonDocumentToPropertyBag(objY);

            // We can't just compare strings because the ordering of properties is not guaranteed, so instead we must
            // deserialize the strings and use a deep equals that does not pay attention to property ordering.
            Assert.True(EqualityHelper.Equals(bagX, bagY));
        }