Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a NumberFormatException with Gson in deserialization a JSON response

I'm reading a JSON response with Gson, which returns somtimes a NumberFormatException because an expected int value is set to an empty string. Now I'm wondering what's the best way to handle this kind of exception. If the value is an empty string, the deserialization should be 0.

Expected JSON response:

{    "name" : "Test1",    "runtime" : 90 } 

But sometimes the runtime is an empty string:

{    "name" : "Test2",    "runtime" : "" } 

The java class looks like this:

public class Foo {     private String name;     private int runtime; } 

And the deserialization is this:

String input = "{\n" +                "   \"name\" : \"Test\",\n" +                "   \"runtime\" : \"\"\n" +                "}";  Gson gson = new Gson(); Foo foo = gson.fromJson(input, Foo.class); 

Which throws a com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String because an empty String is returned instead of an int value.

Is there a way to tell Gson, "if you deserialize the field runtime of the Type Foo and there is a NumberFormatException, just return the default value 0"?

My workaround is to use a String as the Type of the runtime field instead of int, but maybe there is a better way to handle such errors.

like image 465
Soundlink Avatar asked Jan 14 '12 16:01

Soundlink


People also ask

What is GSON Deserialization?

Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished.

What is GSON serialization and Deserialization?

Gson can serialize a collection of arbitrary objects but can't deserialize the data without additional information. That's because there's no way for the user to indicate the type of the resulting object. Instead, while deserializing, the Collection must be of a specific, generic type.

What does GSON to JSON do?

Gson (by Google) is a Java library that can be used to convert a Java object into JSON string. Also, it can used to convert the JSON string into equivalent java object.


1 Answers

Here is an example that I made for Long type. This is a better option:

public class LongTypeAdapter extends TypeAdapter<Long> {      @Override     public Long read(JsonReader reader) throws IOException {         if (reader.peek() == JsonToken.NULL) {             reader.nextNull();             return null;         }         String stringValue = reader.nextString();         try {             Long value = Long.valueOf(stringValue);             return value;         } catch (NumberFormatException e) {             return null;         }     }      @Override     public void write(JsonWriter writer, Long value) throws IOException {         if (value == null) {             writer.nullValue();             return;         }         writer.value(value);     } } 

Register an adapter using Gson util:

Gson gson = new GsonBuilder().registerTypeAdapter(Long.class, new LongTypeAdapter()).create(); 

You can refer to this link for more.

like image 190
Manish Khandelwal Avatar answered Sep 19 '22 01:09

Manish Khandelwal