I'm using the Google HTTP Library to parse JSON data received from a get request, but the strange thing that's happening now is that for one of my @Keys, the value that is coming across as JSON is null (hasShipmentBeenPulled):
{
"orderNo": 19.000,
...
"customFields": {
"hasShipmentBeenPulled": null,
"service": null
}
}
But when the library is parsing out the value, it's defaulting to 'true'. Here's the class:
public class CustomFields {
@Key
private Boolean hasShipmentBeenPulled;
@Key
private String service;
public Boolean getHasShipmentBeenPulled() {
return hasShipmentBeenPulled;
}
public void setHasShipmentBeenPulled(Boolean hasShipmentBeenPulled) {
this.hasShipmentBeenPulled = hasShipmentBeenPulled;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
}
Here's the output:
OrderNo: 19
UseOrderNoRoot: 0
hasShipmentBeenPulled: true
Why is this happening? Shouldn't this be defaulting to false? Everything I have been reading over the past 2 hours has pointed to the fact the null values should be defaulting to false, or ideally, I would prefer for it to just remain a null value.
P.S. Below is the code that is making the magic happen:
HttpRequest request = requestFactory.buildGetRequest(url).setHeaders(getHeaders());
Type type = new TypeToken<List<Order>>() {}.getType();
List<Order> orders = (List<Order>) request.execute().parseAs(type);
for(Order order : orders) {
System.out.println("OrderNo: " + order.getOrderNo());
System.out.println("UseOrderNoRoot: " + order.getUseOrderNoRoot());
System.out.println("hasShipmentBeenPulled: " + order.getCustomFields().getHasShipmentBeenPulled());
Ultimately what I'm trying to accomplish is parse out the JSON, and if a value is set to null in the JSON, I want the Java object also be set to null. I have some required fields that will always be passed in, but there are other fields that are optional. If the field is optional, I want to still gather that information if it's passed over, but if not it can just be left as null or be ignored entirely. I'm not sure the best way to handle that functionality using this library. So far I've been unsuccessful.
Thanks.