示例#1
0
        public void RoundtripTypeConverterAdapterWithCollections()
        {
            var options = new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            options.Converters.Add(new JsonStringEnumConverter());
            options.Converters.Add(new TypeConverterJsonAdapterFactory());
            options.Converters.Add(new JsonObjectToInferredTypeConverter());
            options.IgnoreNullValues = true;
            var model = new TestTypeConverters {
                Point   = GeoPoint.Parse("37.9888529,23.7037796"),
                Id      = new Base64Id(Guid.Parse("04F39650-F015-4831-8592-D2D82E70589F")),
                Mystery = new TheMystery {
                    FirstName = "Constantinos",
                    LastName  = "Leftheris"
                },
                Filters = new[] {
                    FilterClause.Parse("name::In::(String)Constantinos, George")
                }
            };
            var jsonExpected = "{\"point\":\"37.9888529,23.7037796\",\"id\":\"UJbzBBXwMUiFktLYLnBYnw\",\"filters\":[\"name::In::(String)Constantinos, George\"],\"mystery\":{\"firstName\":\"Constantinos\",\"lastName\":\"Leftheris\"}}";
            //var jsonExpected = "{\"point\":\"37.9888529,23.7037796\",\"id\":\"UJbzBBXwMUiFktLYLnBYnw\",\"mystery\":{\"firstName\":\"Constantinos\",\"lastName\":\"Leftheris\"}}";
            var json = JsonSerializer.Serialize(model, options);

            Assert.Equal(jsonExpected, json);
            var output = JsonSerializer.Deserialize <TestTypeConverters>(json, options);

            Assert.Equal(model.Point.Latitude, output.Point.Latitude);
            Assert.Equal(model.Id, output.Id);
            var json2 = JsonSerializer.Serialize(output, options);

            Assert.Equal(json, json2);
        }
示例#2
0
        public void RoundtripTypeConverterAdapter()
        {
            var options = new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            options.Converters.Add(new JsonStringEnumConverter());
            options.Converters.Add(new TypeConverterJsonAdapter());
            options.IgnoreNullValues = true;
            var model = new TestTypeConverters {
                Point = GeoPoint.Parse("37.9888529,23.7037796"), Id = new Base64Id(Guid.Parse("04F39650-F015-4831-8592-D2D82E70589F"))
            };
            var jsonExpected = "{\"point\":\"37.9888529,23.7037796\",\"id\":\"UJbzBBXwMUiFktLYLnBYnw\"}";
            var json         = JsonSerializer.Serialize(model, options);

            Assert.Equal(jsonExpected, json);
            var output = JsonSerializer.Deserialize <TestTypeConverters>(json, options);

            Assert.Equal(model.Point.Latitude, output.Point.Latitude);
            Assert.Equal(model.Id, output.Id);
        }