Irshad Ali
Irshad Ali

Reputation: 1213

JSON data is not bound with Dictionary property during deserialization

I am using Nest for querying data that binds with a corresponding property using Newtonsoft.Json.

Below is a property of a class that is not being populated with JSON data.

[JsonExtensionData]
IDictionary<long, ICollection<Tuple<string, byte[ ]>>> ImageMap { get; set; }

However, others are correctly bound. I am using Newtonsoft.Json 7.0.1.

enter image description here

Upvotes: 3

Views: 1281

Answers (1)

Brian Rogers
Brian Rogers

Reputation: 129717

The problem is that you are using the [JsonExtensionData] attribute inappropriately. [JsonExtensionData] is intended to be used to capture extra data from the JSON for which you did not explicitly define properties in your class. To use it properly, the dictionary in your class must be declared as a Dictionary<string, object> or Dictionary<string, JToken>. (See How to serialize a Dictionary as part of its parent object using Json.Net for a simple example.)

However, in your case, you have a very specific ImageMap property in your class which is intended to capture the data from the corresponding imageMap property in the JSON. This doesn't fit the use case for extension data at all. Remove the [JsonExtensionData] attribute and replace it with [JsonProperty("imageMap")]; then it should deserialize correctly.

[JsonProperty("imageMap")]
public IDictionary<long, ICollection<Tuple<string, byte[]>>> ImageMap { get; set; }

Fiddle: https://dotnetfiddle.net/05J7Wo

Upvotes: 2

Related Questions