question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`null` String for `Exception`s deserialized as String "null" instead of `null`

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Nov 30, 2017

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).

0reactions
cowtowncodercommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

null String for Exception s deserialized as String "null" instead ...
The problem is that while serialization of string object properties with null value is fine, the deserialization is not. What I want is,...
Read more >
Jackson: deserializing null Strings as empty Strings
A simple solution using no Jackson specialities: Write a Getter for name which returns an empty String instead of null as Jackson uses...
Read more >
Handling JSON null and empty arrays and objects - IBM
Handling null and empty arrays and objects used in JSON data is described. JSON data has the concept of null and empty arrays...
Read more >
The Null and Drop Values — colander 1.8.3 documentation
The type object's deserialize method will be called with the null value to allow the type to convert the null value to a...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Deserialize null to non-nullable type. Newtonsoft.Json doesn't throw an exception in the following scenario: NullValueHandling is set to Ignore ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found