Open meierjan opened 7 years ago
Just off the top of my head, and depending on how many responses and/or fields this would actually affect, I personally would take either of the following approaches:
TypeConverter
to each @JsonField
@OnJsonParseComplete
method of each affected type, maybe even using reflective field iteration, to minimize tedious redundancyString?
@aurae Thanks for your time and for the info
Attach your custom "null-to-empty-string" TypeConverter to each @JsonField
That's the first thing that came to my mind as well. What I don't like is that this is pretty error prone as missing one single String
TypeConverter in the future might make the whole model unparsable. The worst part is that it might work on my tests but failes for others (depending on the api response)
On the other hand it is a good solution if you don't miss anything. Thanks!
Perform the conversion in the @OnJsonParseComplete method of each affected type, maybe even using reflective field iteration, to minimize tedious redundancy
I can't find any documentation on this other then the java-doc stating:
/**
* Declare that a method should be called once a class has been parsed from JSON.
* <pre><code>
* {@literal @}OnJsonParseComplete
* public void postParseMethod() {
* ...
* }
* </code></pre>
*/
When I understand this correctly, it is called AFTEr the whole class was parsed. I think this is not working because setting null explicitly to a non-optional field raises an exception.
Expose the affected fields as String?
This is really what I am trying to avoid in the first place
I currently try to map JSON responses to Kotlin data classes.
Now I am trying to parse some JSON responses with explicit
null
values for strings that are empty (rather then providing them as""
).I wonder how to get around this? Keeping in mind how String is implemented internally I would consider
""
as the default String value even if it is explicitly set tonull
.Now the question: What would be the cleanest way to achieve this?
Do I have to change the String Mapper?