Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSON deserializes int as Double... #5

Open
karol-brejna-i opened this issue Jan 3, 2022 · 4 comments
Open

GSON deserializes int as Double... #5

karol-brejna-i opened this issue Jan 3, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@karol-brejna-i
Copy link
Owner

It turns out it is a default behaviour of GSON.

For example, int the following response:

class QueryResponse {
version: class Version {
edition: enterprise
api: v2
schema: 0
}
error: false
message:
results: [{result=[{personId=2.199023255711E12, personFirstName=David, personLastName=Alonso, xCount=1.0, yCount=1.0, xyCount=2.0}]}]
requestId: null
}

personId, xCount are long numbers. They were deserialized to Double.

In one of the newer versions of GSON there is a switch that disables the "feature".

See: google/gson#1290

Use the switch to make integers great again...

@karol-brejna-i karol-brejna-i self-assigned this Jan 3, 2022
@karol-brejna-i karol-brejna-i added the enhancement New feature or request label Jan 3, 2022
@karol-brejna-i
Copy link
Owner Author

Try to add:

    public JSON() {
        gson = createGson()
            .registerTypeAdapter(Date.class, dateTypeAdapter)
            .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
            .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
            .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
            .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)   <------------------------- this one
            .create();
    }

in src/main/java/io/github/karol_brejna_i/tigergraph/restppclient/invoker/JSON.java

@bdoobli
Copy link

bdoobli commented Feb 5, 2022

    json = "{\"kind\": \"books#volumes\", \"totalItems\": 0, \"items\": []}";
    Gson gson = new GsonBuilder()
            .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
            .create();
    gson.fromJson(json, com.google.api.services.books.v1.model.Volumes.class);

==> java.lang.IllegalArgumentException: field com.google.api.services.books.v1.model.Volumes.totalItems has type java.lang.Integer, got java.lang.Long

    json = "{\"kind\": \"books#volumes\", \"totalItems\": 0, \"items\": []}";
    (new Gson()).fromJson(json, com.google.api.services.books.v1.model.Volumes.class);

==> java.lang.IllegalArgumentException: field com.google.api.services.books.v1.model.Volumes.totalItems has type java.lang.Integer, got java.lang.Double

What is a solution to deserialize json com.google.api.services.books.v1.model.Volumes.class ?

@karol-brejna-i
Copy link
Owner Author

@bdoobli For me, the following resolved previous issue:

    public static GsonBuilder createGson() {
        GsonFireBuilder fireBuilder = new GsonFireBuilder() ;
        return fireBuilder.createGsonBuilder();
    }

    public JSON() {
        gson = createGson()
            .registerTypeAdapter(Date.class, dateTypeAdapter)
            .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
            .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
            .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
            .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
            .create();
    }

I see GsonFireBuilder is used here (as opposed to your GsonBuilder. Take a look at the source code (https://github.com/karol-brejna-i/tigergraph-restpp-client/blob/develop/src/main/java/io/github/karol_brejna_i/tigergraph/restppclient/invoker/JSON.java), maybe there are more differences.
Please, note, that my code is generated by openapi-generator-cli, I didn't need to handcraft the code (beside setObjectToNumberStrategy)...

@bdoobli
Copy link

bdoobli commented Feb 7, 2022

I tested your GsonFireBuilder without success.

With another deserializer com.google.api.client.json.JsonSolution there is a solution

    String json = "{\"kind\": \"books#volumes\", \"totalItems\": 0, \"items\": []}";
    JsonFactory factory = getDefaultInstance();
    Volumes vs = factory.fromString(json, com.google.api.services.books.v1.model.Volumes.class);

Still wondering how to use Gson with google api services like com.google.api.services.books.v1.model.Volumes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants