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 int
s 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.