public void ReadObjectWithDictionary() { JsonSerializerOptions options = new JsonSerializerOptions(); options.SetupExtensions(); const string json = @"{""Dictionary"":{""1"":{""Id"":1},""2"":{""Id"":2}}}"; ObjectWithDictionary actual = JsonSerializer.Deserialize <ObjectWithDictionary>(json, options); ObjectWithDictionary expected = new ObjectWithDictionary { Dictionary = new Dictionary <int, SimpleObject> { [1] = new SimpleObject { Id = 1 }, [2] = new SimpleObject { Id = 2 } } }; Assert.Equal(expected.Dictionary, actual.Dictionary); }
public void WriteObjectWithDictionary() { JsonSerializerOptions options = new JsonSerializerOptions(); options.SetupExtensions(); ObjectWithDictionary obj = new ObjectWithDictionary { Dictionary = new Dictionary <int, SimpleObject> { [1] = new SimpleObject { Id = 1 }, [2] = new SimpleObject { Id = 2 } } }; string actual = JsonSerializer.Serialize(obj, options); string expected = @"{""Dictionary"":{""1"":{""Id"":1},""2"":{""Id"":2}}}"; Assert.Equal(expected, actual); }