Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwriting String Mapper #211

Open
meierjan opened this issue May 28, 2017 · 2 comments
Open

Overwriting String Mapper #211

meierjan opened this issue May 28, 2017 · 2 comments

Comments

@meierjan
Copy link

meierjan commented May 28, 2017

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 to null.

Now the question:
What would be the cleanest way to achieve this?

Do I have to change the String Mapper?

@meierjan meierjan changed the title Overwriting String wrapper Overwriting String Mapper May 28, 2017
@mannodermaus
Copy link
Contributor

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:

  1. Attach your custom "null-to-empty-string" TypeConverter to each @JsonField
  2. Perform the conversion in the @OnJsonParseComplete method of each affected type, maybe even using reflective field iteration, to minimize tedious redundancy
  3. Expose the affected fields as String?

@meierjan
Copy link
Author

meierjan commented May 29, 2017

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants