Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON: JsonMappingException while try to deserialize object with null values

I try to deserialize object that contains null-properties and have the JsonMappingException.

What I do:

String actual = "{\"@class\" : \"PersonResponse\"," +                 "  \"id\" : \"PersonResponse\"," +                 "  \"result\" : \"Ok\"," +                 "  \"message\" : \"Send new person object to the client\"," +                 "  \"person\" : {" +                 "    \"id\" : 51," +                 "    \"firstName\" : null}}"; ObjectMapper mapper = new ObjectMapper(); mapper.readValue(new StringReader(json), PersonResponse.class); //EXCEPTION! 

BUT: if to throw away "firstName = null" property - all works fine! I mean pass the next string:

String test = "{\"@class\" : \"PersonResponse\"," +                 "  \"id\" : \"PersonResponse\"," +                 "  \"result\" : \"Ok\"," +                 "  \"message\" : \"Send new person object to the client\"," +                 "  \"person\" : {" +                 "    \"id\" : 51}}"; ObjectMapper mapper = new ObjectMapper(); mapper.readValue(new StringReader(json), PersonResponse.class); //ALL WORKS FINE! 

Question: How to avoid this exception or to pledge Jackson ignore null-values during serialization?

Throws:

Message:

com.fasterxml.jackson.databind.MessageJsonException:  com.fasterxml.jackson.databind.JsonMappingException:   N/A (through reference chain: person.Create["person"]->Person["firstName"]) 

cause:

com.fasterxml.jackson.databind.MessageJsonException:  com.fasterxml.jackson.databind.JsonMappingException:   N/A (through reference chain: prson.Create["person"]->Person["firstName"]) 

cause: java.lang.NullPointerException

like image 949
VB_ Avatar asked Aug 07 '13 06:08

VB_


People also ask

How do I ignore null values in JSON Deserializing?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

What causes JsonMappingException?

Now let's take a look at JsonMappingException: No Serializer Found for Class. This exception is thrown if we try to serialize an instance while its properties and their getters are private.

What is JsonMappingException?

public class JsonMappingException extends JsonProcessingException. Checked exception used to signal fatal problems with mapping of content, distinct from low-level I/O problems (signaled using simple IOException s) or data encoding/decoding problems (signaled with JsonParseException , JsonGenerationException ).

What is JSON Deserialization in Java?

Deserialization is transforming the data from a file or stream back into an object to be used in your application. This can be binary data or structured data like JSON and XML. Deserialization is the opposite of serialization, which transforms objects into byte streams or structured text.


2 Answers

Sometimes this problem occurs when accidentally using a primitive type as return type of the getter of a non-primitive field:

public class Item {     private Float value;      public float getValue()     {         return value;     }      public void setValue(Float value)     {         this.value = value;     }    } 

Notice the "float" instead of "Float" for the getValue()-method, this can lead to a Null Pointer Exception, even when you have added

objectMapper.setSerializationInclusion(Include.NON_NULL); 
like image 75
Neman Avatar answered Sep 21 '22 11:09

Neman


If you don't want to serialize null values, you can use the following setting (during serialization):

objectMapper.setSerializationInclusion(Include.NON_NULL); 

Hope this solves your problem.

But the NullPointerException you get during deserialization seems suspicious to me (Jackson should ideally be able to handle null values in the serialized output). Could you post the code corresponding to the PersonResponse class?

like image 44
Jackall Avatar answered Sep 21 '22 11:09

Jackall