public static void SetCustomConverterForDictionaryProperty() { DefaultJsonTypeInfoResolver resolver = new(); resolver.Modifiers.Add((ti) => { if (ti.Type == typeof(TestClassWithDictionaries)) { Assert.Equal(JsonTypeInfoKind.Object, ti.Kind); foreach (var prop in ti.Properties) { if (prop.Name == nameof(TestClassWithDictionaries.DictionaryProperty1)) { prop.CustomConverter = new AddDictionaryEntryConverter(); } } } }); JsonSerializerOptions options = new JsonSerializerOptions(); options.IncludeFields = true; options.TypeInfoResolver = resolver; TestClassWithDictionaries originalObj = new TestClassWithDictionaries() { DictionaryProperty1 = new Dictionary <string, int> { ["test1"] = 4, ["test2"] = 5, }, DictionaryProperty2 = new Dictionary <string, int> { ["foo"] = 1, ["bar"] = 8, }, }; string json = JsonSerializer.Serialize(originalObj, options); Assert.Equal("" "{" DictionaryProperty1 ":{" test1 ":4," test2 ":5," * test * ":-1}," DictionaryProperty2 ":{" foo ":1," bar ":8}}" "", json); TestClassWithDictionaries deserialized = JsonSerializer.Deserialize <TestClassWithDictionaries>(json, options); Assert.Equal(originalObj.DictionaryProperty1, deserialized.DictionaryProperty1); Assert.Equal(originalObj.DictionaryProperty2, deserialized.DictionaryProperty2); }
public static void CreateObjectForDictionaryWithDefaults(bool useTypedCreateObject) { DefaultJsonTypeInfoResolver resolver = new(); resolver.Modifiers.Add((ti) => { if (ti.Type == typeof(Dictionary <string, int>)) { Func <Dictionary <string, int> > createObj = () => new Dictionary <string, int> { ["*test*"] = -1 }; if (useTypedCreateObject) { JsonTypeInfo <Dictionary <string, int> > typedTi = ti as JsonTypeInfo <Dictionary <string, int> >; Assert.NotNull(typedTi); typedTi.CreateObject = createObj; } else { // we want to make sure Func is not a cast to the untyped one ti.CreateObject = () => createObj(); } } }); JsonSerializerOptions options = new JsonSerializerOptions(); options.IncludeFields = true; options.TypeInfoResolver = resolver; TestClassWithDictionaries originalObj = new() { DictionaryProperty1 = new Dictionary <string, int> { ["test1"] = 2, ["test2"] = 3 }, DictionaryProperty2 = new Dictionary <string, int>(), }; string json = JsonSerializer.Serialize(originalObj, options); Assert.Equal("" "{" DictionaryProperty1 ":{" test1 ":2," test2 ":3}," DictionaryProperty2 ":{}}" "", json); TestClassWithDictionaries deserialized = JsonSerializer.Deserialize <TestClassWithDictionaries>(json, options); Assert.Equal(new Dictionary <string, int> { ["*test*"] = -1, ["test1"] = 2, ["test2"] = 3 }, deserialized.DictionaryProperty1); Assert.Equal(new Dictionary <string, int> { ["*test*"] = -1 }, deserialized.DictionaryProperty2); json = @"{}"; deserialized = JsonSerializer.Deserialize <TestClassWithDictionaries>(json, options); Assert.Null(deserialized.DictionaryProperty1); Assert.Null(deserialized.DictionaryProperty2); json = "" "{" DictionaryProperty2 ":{" foo ":123}}" ""; deserialized = JsonSerializer.Deserialize <TestClassWithDictionaries>(json, options); Assert.Null(deserialized.DictionaryProperty1); Assert.Equal(new Dictionary <string, int> { ["*test*"] = -1, ["foo"] = 123 }, deserialized.DictionaryProperty2); }