`null` String for `Exception`s deserialized as String "null" instead of `null`
See original GitHub issueHi! 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:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok yes. Use of
parser.getText()
unfortunately did include"null"
as value. So better choice isparser.getValueAsString()
which only coerces scalars, but returnsnull
for JSONnull
as well as for structured tokens (START_ARRAY, START_OBJECT).@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.