Jeremy
Jeremy

Reputation: 46322

Json.net custom JsonConvert for Dictionary<string,object>[]

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.

Upvotes: 2

Views: 368

Answers (1)

Jeremy
Jeremy

Reputation: 46322

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; }
}

Upvotes: 2

Related Questions