Example #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);
        }
Example #2
0
 /// <summary>
 /// Supply conversion from <see cref="string"/> to <seealso cref="FilterClause"/> otherwise use default implementation
 /// </summary>
 /// <param name="context"></param>
 /// <param name="culture"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     if (value is string @string)
     {
         return(FilterClause.Parse(@string));
     }
     return(base.ConvertFrom(context, culture, value));
 }