[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<Test>(json);
and
public class Test
{
public string X { get; set; }
[JsonExtensionData]
public Dictionary<string, object> Y { get; set; }
}
The dictionary should contain such elements:
Dictionary<string, object> students = new Dictionary<string, object>()
{
{ "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<string, JToken> _JTokenProperty { get; set; }
public Dictionary<string, PropertiesClass> Properties1 { get; set; } = new Dictionary<string, PropertiesClass>();
}