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

JavaJsonDeserializationGson

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

At first, I tried to write a general custom type adaptor for Integer values, to catch the NumberFormatException and return 0, but Gson doesn't allow TypeAdaptors for primitive Types:

java.lang.IllegalArgumentException: Cannot register type adapters for class java.lang.Integer

After that I introduced a new Type FooRuntime for the runtime field, so the Foo class now looks like this:

public class Foo
{
    private String name;
    private FooRuntime runtime;
    
    public int getRuntime()
    {
        return runtime.getValue();
    }
}

public class FooRuntime
{
    private int value;

    public FooRuntime(int runtime)
    {
        this.value = runtime;
    }

    public int getValue()
    {
        return value;
    }
}

A type adaptor handles the custom deserialization process:

public class FooRuntimeTypeAdapter implements JsonDeserializer<FooRuntime>, JsonSerializer<FooRuntime>
{
    public FooRuntime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        int runtime;
        try
        {
            runtime = json.getAsInt();
        }
        catch (NumberFormatException e)
        {
            runtime = 0;
        }
        return new FooRuntime(runtime);
    }

    public JsonElement serialize(FooRuntime src, Type typeOfSrc, JsonSerializationContext context)
    {
        return new JsonPrimitive(src.getValue());
    }
}

Now it's necessary to use GsonBuilder to register the type adapter, so an empty string is interpreted as 0 instead of throwing a NumberFormatException.

String input = "{\n" +
               "   \"name\" : \"Test\",\n" +
               "   \"runtime\" : \"\"\n" +
               "}";

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(FooRuntime.class, new FooRuntimeTypeAdapter());
Gson gson = builder.create();
Foo foo = gson.fromJson(input, Foo.class);

Solution 3 - Java

Quick and easy workaround - Just change your member type field of runtime to String and access it via getter that returns runtime as an int:

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

    public int getRuntime(){
        if(runtime == null || runtime.equals("")){
            return 0;
        }
        return Integer.valueOf(trackId);
    }
}

=> no json deserialization neccessary

Solution 4 - Java

I've made this TypeAdapter which check for empty strings and return 0

public class IntegerTypeAdapter extends TypeAdapter<Number> {
@Override
public void write(JsonWriter jsonWriter, Number number) throws IOException {
	if (number == null) {
		jsonWriter.nullValue();
		return;
	}
	jsonWriter.value(number);
}

@Override
public Number read(JsonReader jsonReader) throws IOException {
	if (jsonReader.peek() == JsonToken.NULL) {
		jsonReader.nextNull();
		return null;
	}

	try {
		String value = jsonReader.nextString();
		if ("".equals(value)) {
			return 0;
		}
		return Integer.parseInt(value);
	} catch (NumberFormatException e) {
		throw new JsonSyntaxException(e);
	}
}

}

Solution 5 - Java

As stated in another comment, as of GSON 2.3.1 you can register type adapters for primitive types, here is a type adapter that handles int and Integer types, and gracefully defaults to 0 (or null) for strings, booleans and nulls. This will continue to parse strings that have numbers in them like "runtime" : "5".

public static final TypeAdapter<Number> UNRELIABLE_INTEGER = new TypeAdapter<Number>() {
    @Override
    public Number read(JsonReader in) throws IOException {
        JsonToken jsonToken = in.peek();
        switch (jsonToken) {
            case NUMBER:
            case STRING:
                String s = in.nextString();
                try {
                    return Integer.parseInt(s);
                } catch (NumberFormatException ignored) {
                }
                try {
                    return (int)Double.parseDouble(s);
                } catch (NumberFormatException ignored) {
                }
                return null;
            case NULL:
                in.nextNull();
                return null;
            case BOOLEAN:
                in.nextBoolean();
                return null;
            default:
                throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
        }
    }
    @Override
    public void write(JsonWriter out, Number value) throws IOException {
        out.value(value);
    }
};
public static final TypeAdapterFactory UNRELIABLE_INTEGER_FACTORY = TypeAdapters.newFactory(int.class, Integer.class, UNRELIABLE_INTEGER);

You can register it with the following code

Gson gson = new GsonBuilder()
            .registerTypeAdapterFactory(UNRELIABLE_INTEGER_FACTORY)
            .create();

Note that the normal JsonReader.nextInt() that this replaces attempts to call parseInt and parseDouble on the token, so this will replicate internal logic for parsing integers.

Solution 6 - Java

It might help you to always assume a default value of 0 for the field runtime in case of a NumberFormatException, since it can be the only source of error.

Solution 7 - Java

This solution works for Double types. This will only work for non-primitive types:

public class DoubleGsonTypeAdapter implements JsonDeserializer<Double> {

    @Override
    public Double deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        Double result = null;
        try {
            result = jsonElement.getAsDouble();
        } catch (NumberFormatException e) {
            return result;
        }
        return result;
    }
}

Model:

@SerializedName("rateOfInterest")
public Double rateOfInterest;
@SerializedName("repaymentTenure")
public Double repaymentTenure;
@SerializedName("emiAmount")
public Double emiAmount;

Retrofit client:

Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new DoubleGsonTypeAdapter()) .create();

Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

Solution 8 - Java

I like NO ADAPTERS

forgive me if that looks offensive, but, i had to change my Model Class types all to String to overcome this issue

for example i had

data class Info(
    @SerializedName("name") val name : String?,
	@SerializedName("cover") val cover : String?,
	@SerializedName("releaseDate") val releaseDate : Int?,
	@SerializedName("last_modified") val last_modified : Int?,
	@SerializedName("rating") val rating : Int?)

i was facing NumberFormatException so i changed it to

data class Info(
    @SerializedName("name") val name : String?,
	@SerializedName("cover") val cover : String?,
	@SerializedName("releaseDate") val releaseDate : String?,
	@SerializedName("last_modified") val last_modified : String?,
	@SerializedName("rating") val rating : String?)

Now, i check them as

if(!TextUtils.isEmpty(releaseDate){
//go ahead to use it
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoundlinkView Question on Stackoverflow
Solution 1 - JavaManish KhandelwalView Answer on Stackoverflow
Solution 2 - JavaSoundlinkView Answer on Stackoverflow
Solution 3 - JavaLukas LechnerView Answer on Stackoverflow
Solution 4 - JavaValdo RayaView Answer on Stackoverflow
Solution 5 - JavaseanalltogetherView Answer on Stackoverflow
Solution 6 - JavaMilad NaseriView Answer on Stackoverflow
Solution 7 - JavaPrateek BhuwaniaView Answer on Stackoverflow
Solution 8 - JavaNasibView Answer on Stackoverflow