10

Now first off, I've read some other answers on this site and others about jackson serialisation but they all provide methods for ignoring null fields. In Java, however, int cannot be null.

I am trying to ObjectMap a java object to convert to Json but to ignore any null fields. This works for strings but ints end up taking on a value of 0 if uninitialised, and since 0 is not null the field is not ignored.

    private ObjectWriter mapper = new ObjectMapper().writer();
    private myClass data = new myClass(); //class contains a string and int variable
    data.setNumber(someInt); //set values
    data.setString(someString);

    String Json = mapper.writeValueAsString(data);

Can anyone shed some light on this please?

EDIT: To clarify, I have tried using the Integer class as the data type but causes the conversion to a Json string to throw a JsonProcessingException.

Shiri
  • 1,972
  • 7
  • 24
  • 46
  • What type is `data`? What error do you get? Show the class you're trying to serialize. – dguay Oct 27 '15 at 15:33
  • I've edited the question to provide more information. – Shiri Oct 27 '15 at 15:40
  • Please show `myClass` and the error stack. – dguay Oct 27 '15 at 15:43
  • myclass can consist of anything, and for this question's sake, only contains an int with it's getters and setters. And there is no error stack because I've caught the exception. – Shiri Oct 27 '15 at 15:45
  • @Shiri - Then print the exception, when you catch it and then copy it here. Changing "int" to "Integer" does not cause error itself, the error is somewhere deeper. Using Integer instead of int for json serialization is completely legit. – libik Oct 27 '15 at 15:51
  • It was a NullPointerException Error. `Failed.com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException)` – Shiri Oct 27 '15 at 16:00
  • @Shiri Look at my edited answer. – dguay Oct 27 '15 at 16:44

3 Answers3

26

Using the Jackson JsonInclude annotation:

@JsonInclude(Include.NON_DEFAULT)

works around the problem where unassigned primitive types always assumes a default value; in this case, unassigned ints become 0 and are subsequently ignored.

Shiri
  • 1,972
  • 7
  • 24
  • 46
7

Use int wrapper Integer. This way you'll be able to use null value.

Alternatively you can use Jackson's JsonInclude annotation to ignore null value when serializing.

@JsonInclude(Include.NON_NULL)  
public class MyClass{
    ...
}
dguay
  • 1,635
  • 15
  • 24
  • 1
    This is exactly what I'm talking about. The annotation only works for non_null but the int initialises as 0 by default because int cannot be null. I think my problem has something to do with Integer serialisation by Jackson like Libik was saying. – Shiri Oct 28 '15 at 09:24
  • Well it's hard to help without seeing the class you're trying to serialize. You shouldn't have problem with `Integer`. – dguay Oct 28 '15 at 12:02
5

Change int to Integer.

Otherwise no, it is not possible to have int variable with null in any way.

libik
  • 22,239
  • 9
  • 44
  • 87
  • I've tried this, the conversion, `String json = mapper.writeValueAsString(data)` just fails in this case. – Shiri Oct 27 '15 at 15:30
  • 2
    @Shiri The accessor methods should be updated as well to return/set `Integer` class. – Brian Jan 16 '19 at 19:23