Gabriel Ittner
Gabriel Ittner

Reputation: 1162

IllegalArgumentException: Deserializing generated objects with @JsonString annotation

Long values in Objects generated by Cloud Endpoints are annotated with @JsonString. This causes a IllegalArgumentException when deserializing those Objects using a GsonFactory.

This is the stacktrace:

Caused by: java.lang.IllegalArgumentException: number type formatted as a JSON number cannot use @JsonString annotation [key updated, field private java.lang.Long com.google.api.services.timetable.model.Lesson.updated]
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)
    at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:599)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:350)
    at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:586)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:289)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:266)
    at com.google.api.client.json.JsonFactory.fromString(JsonFactory.java:207)

Example code to produce the Exception:

GsonFactory gsonFactory = new GsonFactory();
Lesson lesson = new Lesson();
lesson.setUpdated(2);
String json = gsonFactory.toString(lesson);
gsonFactory.fromString(json, Lesson.class);

Original discusssion https://groups.google.com/d/msg/endpoints-trusted-testers/-/_TKGoruZVt0J

Upvotes: 2

Views: 898

Answers (1)

saiyr
saiyr

Reputation: 2595

The reason why this exception occurs is because the Java client library expects all long integers to be quoted (aka strings), because JavaScript can't handle 64-bit integer precision correctly. There's a known issue where the Python SDK won't correctly serialize 64-bit integers as strings. I'm not sure where you're getting the JSON from, exactly, but if it's in user code, you need to make sure you also have 64-bit integers quoted properly.

Upvotes: 1

Related Questions