Skip to content

Method fromJson throws JsonParseException instead of declared JsonSyntaxException #2816

Description

@DGorokhov123

Gson version

I found a bug in GSON library. It's actual fo version 2.12.1

Description

Bug description:

Method fromJson signature looks like that:

public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {

so, it's obvious to coders to catch JsonSyntaxException:

try {
    goodGson.fromJson(badJson, User.class);
} catch (JsonSyntaxException e) {
    System.out.println("Good deserializer caught JsonSyntaxException");
}

Such construction works until we add Deserializer:

Gson badGson = new GsonBuilder()
          .registerTypeAdapter(User.class, new UserBadDeserializer())
          .create();

class UserBadDeserializer implements JsonDeserializer<User> {
    @Override
    public User deserialize(JsonElement json, Type typeOfT, 
                         JsonDeserializationContext context) throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();
        if (!jsonObject.has("name")) 
                     throw new JsonParseException("json should contain the name field!");
        String name = jsonObject.get("name").getAsString();
        return new User(name);
    }
}

The signature of deserialize method we should override, throws another
exception - JsonParseException, which is parent to JsonSyntaxException
and isn't caught by try-catch construction above
It flies further and may cause unexpected behaviour.

        try {
            try {
                badGson.fromJson(badJson, User.class);
            } catch (JsonSyntaxException e) {
                System.out.println("Good deserializer caught JsonSyntaxException");
            }
        } catch (RuntimeException e) {
            System.out.println("Bad deserializer didn't catch JsonSyntaxException, and " 
                        + e.getClass().getSimpleName() + " flew further");
        }

Solution 1 : Change signature of deserialize method to throw JsonSyntaxException.

Solution 2 : Change signature of fromJson method (and other methods calling
deserialize) to throw JsonParseException.

I've made a simple example to show this bug:
https://github.com/DGorokhov123/gson-bug

(С) Dmitriy Gorokhov dgorokhov123@gmail.com, @DGorokhov123

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions