2

[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>();
}
PiotrN
  • 21
  • 1
  • 3
  • Why do you have a data-structure storing instances that seem to have **nothing** in common in a single **common** map? But anyway: what is your problem exactly? What results do you get? – MakePeaceGreatAgain Nov 14 '18 at 09:47
  • The reason is that the rest api returns user-defined objects. – PiotrN Nov 14 '18 at 09:50
  • Currently, the key and the string as value are created { "key", "{name: "Name"} } – PiotrN Nov 14 '18 at 09:55
  • I think you have to write your custom serializer and within that you hjave to tell Json.net of what type those properties are. I have made a quick search and it seems that here you find a simple example on how this may work. I have not testet this yet. https://stackoverflow.com/a/40439958/9809950 – Josef Biehler Nov 14 '18 at 09:59
  • Possible duplicate of [How to parse not fully corresponding json](https://stackoverflow.com/questions/53279441/how-to-parse-not-fully-corresponding-json) – Adrian Nov 14 '18 at 10:02
  • The problem is that I need one [JsonExtensionData] Dictionary to (de)serialize. The solution with [JsonExtensionData] public Dictionary _JTokenProperty { get; set; } is not accurate because it does not give possibilty to serialize ?? – PiotrN Nov 15 '18 at 10:19
  • Related or duplicate: [How to deserialize a child object with dynamic (numeric) key names?](https://stackoverflow.com/q/40088941/3744182). – dbc Mar 07 '19 at 17:52

2 Answers2

0

Json.Net has the ability to (de)serialize objects specifying the actual type of the object.

In the serialization process a property "$type" for storing the Type is added to the resulting JSON. Then in deserialization this property is checked to obtain the actual Type. Of course it must be a Type which inherits/implements the declared Type in you class. Since you're using object, you shouldn't have problems.

You can activate this feature by setting the TypeNameHandling property of the JsonSerializerSettings to Auto, which activates this behaviour when the object type is different from the declared type.

https://www.newtonsoft.com/json/help/html/SerializationSettings.htm#TypeNameHandling

  • However this is not my restful service that's why it does not return $type and I do not have the option of adding $type – PiotrN Nov 14 '18 at 11:52
  • 1
    How, then, the program is supposed to decide which is the correct type? If you need to make assumptions based on things such as the name of the property (the key of the dictionary) or the content of the object, I think the only way is to write your own `JsonConverter` and place your custom logic into the `ReadJson` method. – Matteo Melli Nov 14 '18 at 15:05
  • Now I used [OnDeserialized] private void OnDeserialized(StreamingContext context) – PiotrN Nov 14 '18 at 15:38
-1

how about if you try the dynamic instance instead of object.

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

    [JsonExtensionData]
    public Dictionary<string, dynamic> Y { get; set; }
}
zetawars
  • 1,023
  • 1
  • 12
  • 27