JsonApiFramework icon indicating copy to clipboard operation
JsonApiFramework copied to clipboard

JsonConverterAttribute

Open joshjeppson opened this issue 5 years ago • 2 comments

I see that I can pass in JsonSerializerSettings including a list of JsonConverters when serializing a Document. However these converters appear to ALWAYS be used. For example, if I pass in StringEnumConverter then all enums use that converter to serialize. Is it possible to get the framework to use the JsonConverterAttribute (example: [JsonConverter(typeof(StringEnumConverter))]) similar to how the default JsonMediaTypeFormatter works?

joshjeppson avatar Oct 16 '20 08:10 joshjeppson

So JsonApiFramework does use JsonConverter instances under the hood directly when serializing/deserializing JSON to/from a JSON API document and thus any JsonConverterAttribute I do think get ignored because of this approach.

I would try and use some kind of naming convention perhaps, let me know if this is still an issue and exactly what you are trying to do so I can take a stab at helping or implementing something that would help.

scott-mcdonald avatar Dec 23 '20 16:12 scott-mcdonald

We're trying to do several things. As noted in my initial comment, one use case is having some enums on a given model use the string representation and others use the integer value. Another use case is automatic cleanup of values. For example, we prefer to always save string values as null if the transmitted value was null, empty string or whitespace. We also prefer to save phone numbers, zip codes and ssns as an unformatted string of digits and thus we strip any transmitted formatting. Prior to using JsonApiFramework we used JsonConverters to accomplish this. In fact, the attributes are technically still in place because one of our Ember apps does not yet use JsonApi. Now, to compensate for the JsonConverters not being called, the conversion to the db model includes some fixup to handle this. It works... but isn't as elegant.

joshjeppson avatar Dec 29 '20 18:12 joshjeppson