I am implementing a detector - deserializer for JSON files using Jackson.
Those JSON files can contain null values. However, for the detection phase, I'd like jackson to differentiate whether the field is present but null, or not present at all.
Example: I have this Vehicle Java class:
class Vehicle {
// mandatory fields
private String id;
private String brand;
private String model;
// optional fields
private String color;
private double length;
private double width;
}
I want these JSONs to be successfully parsed
{
"id": 123456,
"brand": "Ford",
"model": null, // field is present (null, but present)
"color": "white",
"length": 410,
"width": 230,
}
{
"id": 123456,
"brand": "Ford",
"model": null, // field is present (null, but present)
"color": "white", // missing optional fields, but they are optional
}
{
"id": 123456,
"brand": "Ford",
"model": "FordModel",
"color": "white",
"length": 410,
"width": 230,
"height": 210 // additional field not in Java class
}
However, I want the following JSON not to be parsed at all (jackson throwing an exception is fine):
{
"id": 123456,
"brand": "Ford", // missing the model field here
"color": "white",
"length": 410,
"width": 230,
}
I found a way to ignore null values (either by using Optional or with Jackson annotations), but the problem is that I can't find a way to differentiate between "the vehicle field being present but null" and "the vehicle field not being present at all".
What's the best way to achieve that?