I have an Order object in my application which takes the JSON format as follows.
{
"id" : "1001",
"amount" : 100.50
"customer" : {
"id" : "2001"
"name" : "Ned Stark"
}
}
This is represented as the following POJO.
public class Order
{
private long id;
private double amount;
private Customer customer;
}
I use GSON to serialize and deserialize this POJO. Everything works fine until the input values given in the JSON are valid.
But when I give an invalid data type value in JSON, GSON doesn't throw an exception specific to the error prone field but just a generic exception, which gives the app no idea which field value is invalid.
For instance, if I give amount as "asdf" (instead of a valid number) in JSON, during deserialization Gson just throws a generic NumberFormatException, but doesn't seem to provide any information that the exception has happened actually when the field "amount" is being trying to set with the input value.
This will be typically useful to the app when it has to communicate the apt error to the client. I assume there should be someway to achieve this in GSON.
Is there any format to find the specific field that has caused the deserialization failure?