2

I've successfully used a custom JsonConverter to handle items in a dictionary that may either be a string or an object:

[JsonProperty("result", ItemConverterType = typeof(TableFieldOrStringConverter), )]
public Dictionary<string, TableField> Records { get; set; }

But now I have a property that is an array of dictionaryies.

public Dictionary<string, TableField>[] Records { get; set; }

How do I set up the custom converter to apply it to the value of the dictionary?

This is different than most questions that apply a converter to the value of a dictionary item, because I'm trying to apply the converter to the value of a dictionary item in an array of dictionaries, and the JSonPropery attribute does not appear to allow that.

Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • Possible duplicate of [Proper way of using Newtonsoft Json ItemConverterType](https://stackoverflow.com/questions/24639750/proper-way-of-using-newtonsoft-json-itemconvertertype) – Peter B Sep 04 '18 at 18:27
  • @PeterB - I don't think it's the same. That question is trying to convert a string value into an array of values. I am trying to apply a converter to a dictionary value where the property is an array of dictionaries. – Jeremy Sep 04 '18 at 18:31
  • @PeterB - the difference is that in that question the converter is being applied to the property as a whole, I need it applied to the value of the dictionary item. – Jeremy Sep 04 '18 at 18:32

1 Answers1

2

I think I solved this by breaking the property out into another class.

[JsonObject]
public class TableUpdateResponse : IAPIResponse
{
    [JsonProperty("result")]
    public TableRow[] Records { get; set; }
}

[JsonDictionary(ItemConverterType = typeof(TableFieldOrStringConverter))]
public class TableRow : Dictionary<string, TableField>
{

}

[JsonObject]
public class TableField
{
    [JsonProperty("display_value")]
    public string DisplayValue { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }

    /// <summary>
    /// Applicable when the field references another record
    /// </summary>
    [JsonProperty("link")]
    public string Link { get; set; }
}
Jeremy
  • 44,950
  • 68
  • 206
  • 332