I'm trying to serialize and deserialize a complex object tree, with some objects requiring a custom JsonConverter. It's easy to apply such a converter to object properties where the property is a simple object, but I don't know how to apply one in cases where the property is a collection.
For example:
[JsonObject(MemberSerialization.OptIn)]
public class Settings
{
[JsonProperty] public Dictionary<string, Color> ColorSet;
[JsonProperty, JsonConverter(typeof(ColorConverter))] public Color CurrentColor;
}
Color is a type requiring its own JsonConverter (fyi: this is Xamarin.Forms.Color, which does not deserialize from Json out of the box).
Using
Settings newSettings = JsonConvert.DeserializeObject<Settings>(jsondata);
results in a new Settings object with the CurrentColor object correctly initialized. How can I apply the same ColorConverter JsonConverter to the Dictionary of Colors?
The same question applies to a recursive object tree:
[JsonObject(MemberSerialization.OptIn)]
public class DisplayPanel
{
[JsonProperty] public List<DisplayPanel> Children;
}
where DisplayPanel requires its own JsonConverter.
The issue is not that each object is an (unknown) derived class of some base class; each Color / DisplayPanel object in the Dictionary / List has the exact same class.