Skip to content

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

@ZeleniJure

Description

@ZeleniJure

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions