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

null String for Exceptions deserialized as String "null" instead of null #1842

Closed
ZeleniJure opened this issue Nov 28, 2017 · 5 comments
Closed
Milestone

Comments

@ZeleniJure
Copy link

Hi!
I have been trying to solve this problem for over a day now, and what research I did showed that this should work, but it does not in my case, e.g. default behaviour should deserialize null objects to null objects, right?

The problem is that while serialization of string object properties with null value is fine, the deserialization is not.

What I want is, if string property value

  • is "null" -> deserialize to string "null"
  • is null -> deserialize to null object
  • is "" -> deserialize to empty string ("")
  • is "ladida" -> deserialize to string "ladida"

Example code to reproduce the error:

        final ObjectMapper mapper = new ObjectMapper();
        // so that the error is visible at the beginning of the output :)
        mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
        final NullPointerException ex = new NullPointerException();
        String json;
        try {
            json = mapper.writeValueAsString(ex);
            System.out.println("OK\t-> " + json);
            final NullPointerException parsed = mapper.readValue(json, NullPointerException.class);
            json = mapper.writeValueAsString(parsed);
            System.out.println("Not OK\t<- " + json);
        } catch (final IOException e1) {
            e1.printStackTrace();
        }

Output:
See 'localizedMessage' and 'message' strings in serialized and deserialized output. Serialization is OK (null), deserialized is not ok (string containing "null")

OK	-> {"cause":null,"localizedMessage":null,"message":null,"stackTrace":[{"className":"com.cosylab.tcs.commons.communication.model.Test","fileName":"Test.java","lineNumber":87,"methodName":"main","nativeMethod":false}],"suppressed":[]}
Not OK	<- {"cause":null,"localizedMessage":"null","message":"null","stackTrace":[{"className":"com.cosylab.tcs.commons.communication.model.Test","fileName":"Test.java","lineNumber":87,"methodName":"main","nativeMethod":false}],"suppressed":[]}

Version: 2.9.1

@cowtowncoder
Copy link
Member

Jackson json module does not coerce JSON nulls into String "null" so I am bit surprised by this.

But I think it may be possible that specific deserializer for Exception types might have something, so there may be this one special case. I'll have a look if that explains what you observe.

@cowtowncoder cowtowncoder added this to the 2.9.3 milestone Nov 30, 2017
@cowtowncoder cowtowncoder changed the title null String deserialized as "null" instead of null null String for Exceptions deserialized as String "null" instead of null Nov 30, 2017
@cowtowncoder
Copy link
Member

Ok yes. Use of parser.getText() unfortunately did include "null" as value. So better choice is parser.getValueAsString() which only coerces scalars, but returns null for JSON null as well as for structured tokens (START_ARRAY, START_OBJECT).

dmichel1 added a commit to dmichel1/ffwd that referenced this issue Nov 25, 2018
A JSON null value is being translated to a “null” string.

Was supposed to be fixed in FasterXML/jackson-databind#1842 but it doesn’t appear to be.
dmichel1 added a commit to dmichel1/ffwd that referenced this issue Nov 25, 2018
A JSON null value is being translated to a “null” string.

Was supposed to be fixed in FasterXML/jackson-databind#1842 but it doesn’t appear to be.
dmichel1 added a commit to dmichel1/ffwd that referenced this issue Nov 25, 2018
A JSON null value is being translated to a “null” string.

Was supposed to be fixed in FasterXML/jackson-databind#1842 but it doesn’t appear to be.
@sanjaybsm
Copy link

is this issue fixed, if yes then could you please let us know the version?

@kupci
Copy link
Member

kupci commented Nov 5, 2019

According to the doc it was fixed in 2.9.3: https://github.com/FasterXML/jackson-databind/blob/master/release-notes/VERSION-2.x

@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 5, 2019

@sanjaybsm see the "Milestone" on right side of this page. That is where version information is usually included. Fix is also listed under release-notes/VERSION (or depending on branch, release-notes/VERSION-2.x) as @kupci noted.

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

4 participants