How deserialize [JsonExtensionData] Dictionary<string, object> with casting
06:36 14 Nov 2018

[JsonExtensionData] allows you to do is to serialize elements of a JSON document which does not have matching properties on the destination object to the dictionary which is decorated with the [JsonExtensionData] attribute.

How to create a dictionary of objects cast into the appropriate type??

For example:

var json = "{\r\n \"sampleClass\":{ \"name\":\"Name\"} ,             
\"sampleOtherClass\":{ \"name\":\"OtherName\"} ,\r\n  \"X\": \"Jan\"\r\n}";

and

var result  =JsonConvert.DeserializeObject(json);

and

public class Test
{
    public string X { get; set; }

    [JsonExtensionData]
    public Dictionary Y { get; set; }
}

The dictionary should contain such elements:

Dictionary students = new Dictionary()
{
    { "sampleClass", new ClassName { Name="MyName" } },
    { "sampleOtherClass", new SampleOtherClass { Name="MyName" } }
};

Which means that for node sampleClass we want to create object SampleClass and next to add the dictionary Y.

Currently, the value in the DictionaryEntry is a string e.g. { name: "Name" }

The source: https://dotnetfiddle.net/mhU6ME

Update:

Now I used the below approach to deserialize, but when I want to have one [JsonExtensionData] Dictionary to deserialize/serialize it brings problems to have two collections.

public class Class1
{
    public string Code { get; set; }

    public string Name { get; set; }

    public bool IsActive { get; set; }

    [JsonExtensionData]
    public Dictionary _JTokenProperty { get; set; }

    public Dictionary Properties1 { get; set; } = new Dictionary();
}
c# json.net