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.

3
  • Possible duplicate of Proper way of using Newtonsoft Json ItemConverterType Commented Sep 4, 2018 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. Commented Sep 4, 2018 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. Commented Sep 4, 2018 at 18:32

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.