Apply a converter to all elements in Json Array
c#, json.net
Solution
While L.B's answer works, there are cases where a converter is indeed necessary - for instance, when you don't ever get to touch the raw JSON and only get to attribute ASP.NET methods that will be accessible to HTTP requests.
In that case, you can use the `JsonPropertyAttribute` class and assign a value to its `ItemConverterType` property:
[JsonProperty(ItemConverterType = typeof(MyCustomConverter))]
public List<MyCustomType> Items { get; set; }
Problem
One can use custom converters by defining a class like this: ``` public class MyCustomConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(MyCustomType); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var ret = new MyCustomType(); return ret; } } ``` And then using it like this: ``` MyCustomType item = JsonConvert.DeserializeObject<MyCustomType>(jsonString, new MyCustomTypeConverter()); ``` My question is, how can I apply this deserializer when dealing with a list of `MyCustomType`? Basically I have a Json array (`[{ ... }, { ... }]`) and I would like to use the converter above on each item of the array to get a `List<MyCustomType>`. I know I can do it by hand using the `JArray` object and its methods but I was wondering if there was an easier and cleaner way to do it. Here's a simplified context. C# (I want to deserialize a `List` of those): ``` class MyCustomType { public Dictionary<string, string> Data { get; set; } public int Id { get; set; } } ``` JSON (one item in the array sample): ``` { "Id": 50, "Data": [ "Hello", "World" ] } ``` C# Deserialization I want to apply: ``` public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var ret = new MyCustomType(); ret.Data = new Dictionary<string, string>(); while (reader.Read()) { if (reader.TokenType == JsonToken.EndObject) { continue; } var value = reader.Value.ToString(); switch(value) { case "Id": ret.Id = reader.ReadAsInt32().Value; break; case "Data": ret.Data.Add(MySingleton.Instance.CurrentLanguage, reader.ReadAsString()); break; } } return ret; } ```