Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json doesn't deserialize IDictionary<string, string> if get only property #35388

Closed
componentspace opened this issue Apr 24, 2020 · 3 comments

Comments

@componentspace
Copy link

In the following code Newtonsoft.Json correctly deserializes but System.Text.Json doesn't with the dictionary being empty. If the IDictionary<string, string> property has a setter, the deserialization works. However, I don't control the class that requires serialization/deserialization and it includes IDictionary<string, string> properties with getters only.

`
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json;

namespace TestConsoleApp
{
class Test
{
public IDictionary<string, string> Items { get; } = new Dictionary<string, string>();
}

class Program
{
    static void Main(string[] args)
    {
        var test = new Test();
        test.Items["a"] = "1";
        test.Items["b"] = "2";

        var json = System.Text.Json.JsonSerializer.Serialize(test);

        // Dictionary is empty.
        var test2 = System.Text.Json.JsonSerializer.Deserialize<Test>(json);

        // Correctly deserialized.
        var test3 = JsonConvert.DeserializeObject<Test>(json);
    }
}

}

`

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Apr 24, 2020
@ghost
Copy link

ghost commented Apr 24, 2020

Tagging subscribers to this area: @jozkee
Notify danmosemsft if you want to be subscribed.

@Symbai
Copy link

Symbai commented Apr 24, 2020

#30258
#30688

@layomia
Copy link
Contributor

layomia commented Apr 28, 2020

Closing as dup of #30258

@layomia layomia closed this as completed Apr 28, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Apr 28, 2020
@layomia layomia added this to the 5.0 milestone Apr 28, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants