A brief tutorial on using Guava's Optional with Jackson JSON library
One serious limitation of using Jackson to deserialize the custom defined bean classe is that you cannot differentiate between a value missing in the input JSON or it is present but having null value. Consider the exampel below:
You cannot differentiate between the JSON inputs {"firstName": "John"} and {"firstName": "John", "lastName": null}. I.e. the lastName property being present in the input or not.
Guava library has an excellent tool to express missing values versus null values: Optional. Jackson supports integration with Guava library. The integration library can be accessed from jackson-datatype-guava package. To clearly express the null versus absent values, we can rewrite the bean class above as follows:
So before accessing the value, you will be just checking for its presence. I.e. myBean.getFirstName().isPresent().
If you don't care if the value is present or not, but just want to retrieve the value as null if absent, you can do myBean.fetFirstName().orNull().
BTW, if you are not making use of Guava libraries in your project, you are truly missing something great and makes you productive. Please give it a try.
class MyBean { private String firstName; private String lastName; // Getters and setters here. }
You cannot differentiate between the JSON inputs {"firstName": "John"} and {"firstName": "John", "lastName": null}. I.e. the lastName property being present in the input or not.
Guava library has an excellent tool to express missing values versus null values: Optional. Jackson supports integration with Guava library. The integration library can be accessed from jackson-datatype-guava package. To clearly express the null versus absent values, we can rewrite the bean class above as follows:
class MyBean { private Optional<String> firstName; private Optional<String> lastName; // Getters and setters here. }
So before accessing the value, you will be just checking for its presence. I.e. myBean.getFirstName().isPresent().
If you don't care if the value is present or not, but just want to retrieve the value as null if absent, you can do myBean.fetFirstName().orNull().
BTW, if you are not making use of Guava libraries in your project, you are truly missing something great and makes you productive. Please give it a try.
Comments
Why not set the bean property to Optional.absent() when the JSON field is absent? Then set the bean property to the value or null when the JSON property is present (thus, isPresent())?
Sure would make coding easier. For me, null is sent by REST client wanting to specifically null out the field. It's the same as sending a specific value, except it's null(aka. empty). If they don't have a field in the JSON, it's not because they want it nullified, but because it's not in *their* model and so not sent.
Meh. Easier enough to create my own deserializer, I supppose - just wish I didn't have to.