I am trying to figure out how to use a custom JsonConverter as an Attribute. The problem is that I cannot figure out how to get the FOO object within the converter.
Example
[Newtonsoft.Json.JsonConverter(typeof(FOOConverter))] public interface IFOO { ... } public class FOOConverter : Newtonsoft.Json.JsonConverter { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jobj = serializer.Deserialize<JObject>(reader); ... var foo = jobj.ToObject<IFOO>() // Calls the converter again? } } The .ToObject() will run the converter again and cause a stack overflow, which makes sense since it looks at the attribute, but how can I get the IFOO object then?
Edit: The WriteJson will be simular, but with JObject.FromObject(value);
The usage needs to be flexible, for ex: some properties might be encrypted/encrypted during serialization, other times, there may be property values that needs to be cached. To think of a few use cases.