2

I have an issue with JSON serialization into a class with a dictionary property.
The whole process is a bit more complex, as an input i have a YAML file that i convert into a json using YamlDotNet and NewtonSoft like so

This is an example of the Yaml and the JSON output

some_element: '1'
should_be_dic_element:
 - a: '1'
 - b: '2'

{
 "some_element": "1",
 "should_be_dic_element": [
  {
   "a": "1"
  },
  {
   "b": "2"
  }
 ]
}

And the class

 public class SomeClass
 {
    [JsonProperty(PropertyName = "some_element")]
    public string SomeProperty { get; set; }

    [JsonProperty(PropertyName = "should_be_dic_element")]
    public Dictionary<string, string> Dictionary { get; set; }
 }

I know the issue with Array dictionary so this are all the things I've tried.

Using Dictionary i get the following

error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array

Using a new class from Dictionary like so

[JsonArray]
class X : Dictionary<string, string> { }

error Value cannot be null. Parameter name: key

Using KeyValuePair<string, string>[] / List<KeyValuePair<string, string>> the outcome is the amount of elements but with null values.

Any suggestions please ?

guyl
  • 2,158
  • 4
  • 32
  • 58

1 Answers1

2

I think your JSON which you are attempting to de-serialize is not conforming to your class definition. Try changing the way you are generating the JSON.

For a dictionary-type structure, I would expect to see curly braces { instead of [ to begin the structure, e.g.:

{
 "some_element": "1",
 "should_be_dic_element": {
  {
   "a": "1"
  },
  {
   "b": "2"
  }
 }
}

If you want to deserialize the existing JSON unchanged, try using your class definition to specify a list of dictionaries as such:

 public class SomeClass
 {
    [JsonProperty(PropertyName = "some_element")]
    public string SomeProperty { get; set; }

    [JsonProperty(PropertyName = "should_be_dic_element")]
    public List<Dictionary<string, string>> Dictionary { get; set; }
 }
user700390
  • 2,287
  • 1
  • 19
  • 26
  • You are correct, this is not the ideal dictionary json representation yet it is possible to do. many articles are talking about it this is why i used JsonArrayAttribute on the derived class. https://stackoverflow.com/questions/43676125/json-array-to-c-sharp-dictionary/43676283 – guyl Apr 11 '18 at 19:42
  • please see the updated answer, you should be able to de-serialize this way but your code to process the data may be a little more cludgy as a result. – user700390 Apr 11 '18 at 20:41
  • Thank you :) but this is the solution i am trying to avoid. – guyl Apr 12 '18 at 05:31