Deserialization with two different types

Oct 22, 2011 at 2:58 PM

Hello,

I have an hybrid JSON result which can have different forms:

         "values": [
            {
               "value": 0,
            }
         ],

or

         "values": [
            {
               "value": {
                  "anything": 0
               },
            }
         ],

 

The value can be a simple integer or also a Dictionary<string, int> so I created two different types with the same property name:

        [JsonProperty(PropertyName = "value")]        public Int32? Value { get; set; }
        [JsonProperty(PropertyName = "value")]        public Dictionary<string, int> Values { get; set; }

It is the good type for each example but when I let the two properties I have an exception with a dictionary example because it is impossible to deserialize a dictionary to int.

When I have a dictionary value could I create a rule in order to ignore the deserialization to integer ?

Thanks