When using Newtonsoft.Json
, I can make it do what I need by adding a converter to the top level SerializerSettings
or supplying it to the conversion invocation - all is working well.
I'm hoping to extract some of my global converters to instead be applied declaratively in the relevant place where the conversion is actually required.
I'm aware of the following techniques:-
- type level
[JsonConverter(typeof(Converters.StringEnumConverter))]
directly on typeX
- member level
[JsonConverter(typeof(Converters.StringEnumConverter))]
iff the member is of typeX
- item level
[JsonProperty(ItemConverterType=typeof(Converters.StringEnumConverter)]
if the member is actually an array etc. ofX
The problem I'm having is that some of the global converters I have in play operate on nested types, e.g. if I have member of type Tuple<X[],Nullable<X>>
, I can't express the "if you meet an X when processing this field or any child of it, do the conversion" semantic and instead get a Newtonsoft.Json.JsonSerializationException
.
Does such a "for this tree, also use this converter please" mechanism exist? I'd like to avoid having to define a top level type for anything I ever want to convert and then tagging that type with the JsonConverter
to work around this?
MyCustomConverter
to work on D without being passed in externally. I am ok with putting an Attribute on B or C. Not OK to have to pass in code or put on A. – Gewirtz