Skip to content

Several regressions caused by the switch to Gson #1779

@hiranya911

Description

@hiranya911

In #1661 this library moved from the old Jackson-based JSON parser to a Gson-based JSON parser. This change seems to be causing some regressions in the downstream codebases like Firebase.

1. JSON-parser leniency

Gson is a lot more lenient in the inputs it accept as valid JSON. As a result the following test case would pass in the older versions of the API client, but fails in more recent versions.

@Test
public void testLenience() {
    JsonParser parser = Utils.getDefaultJsonFactory().createJsonParser("not json");
    Map<String, Object> destination = new HashMap<>();
    try {
      parser.parseAndClose(destination);
      fail("No error thrown");
    } catch (IOException ex) {
      // expected
    }
}

It seems when using the Gson API directly, there's a way to set parser to a "strict" mode by turning off leniency, but this option is not exposed when parsing using the Google API client.

2. Malformed JSON exceptions

This is more of a consequence of the above issue. Trying to parse certain malformed JSON strings into objects used to result in an IOException. But now it can result in other runtime exceptions like IllegalArgumentException.

@Test
public void testExceptionType()
    JsonParser parser = Utils.getDefaultJsonFactory().createJsonParser("not json");
    try {
      parser.parseAndClose(Foo.class); 
    } catch (IOException e) {
      e.printStackTrace();
    }
}

public static class Foo {
  @Key("name")
  private String name;
}

3. Broken float serialization

This seems to be due google/gson#1127. Serializing floating point values is not working correctly.

System.out.println(Utils.getDefaultJsonFactory().toString(0.2f));
// Output: 0.20000000298023224

Note that there is again a workaround when using Gson directly. But that's not possible when using the Google API client.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions