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 converter is broken when converting string response body #1465

Closed
NeoLSN opened this issue Jan 13, 2016 · 9 comments
Closed

Gson converter is broken when converting string response body #1465

NeoLSN opened this issue Jan 13, 2016 · 9 comments
Milestone

Comments

@NeoLSN
Copy link

NeoLSN commented Jan 13, 2016

See the log.

01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: <-- 200 OK api_web (7364ms)
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: Content-Type: application/json; charset=UTF-8
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: Date: Thu, 14 Jan 2016 03:36:37 GMT
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: Server: Apache
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: Status: 200 OK
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: Content-Length: 94
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: Connection: keep-alive
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: OkHttp-Sent-Millis: 1452742596982
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: OkHttp-Received-Millis: 1452742602997
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: "0qQ9imBV5zYCZi5NRIZCl9WCivkSjj5SR5Ya8Diw0Urw6RV0hoef_DqaNgcZyPif4C89eAFZGn80p8xWroRnEn3AJhPL"
01-14 11:36:42.987 11795-13040/com.example.app D/OkHttp: <-- END HTTP (94-byte body)
01-14 11:36:42.997 11795-13040/com.example.app D/ExoLibrary: error occur
                                                             com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2 path $
                                                                 at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1573)
                                                                 at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1423)
                                                                 at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:575)
                                                                 at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
                                                                 at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:351)
                                                                 at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:348)
                                                                 at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:260)
                                                                 at retrofit2.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:33)
                                                                 at retrofit2.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:23)
                                                                 at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:154)
                                                                 at retrofit2.OkHttpCall.execute(OkHttpCall.java:118)

The library based on

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
    compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.0.0-RC1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
@NeoLSN
Copy link
Author

NeoLSN commented Jan 14, 2016

I do the simple test like below

    Gson gson = new Gson();
    TypeAdapter<String> adapter = gson.getAdapter(TypeToken.get(String.class));
    Reader reader = new StringReader("0qQ9imBV5zYCZi5NRIZCl9WCivkSjj5SR5Ya8Diw0Urw6RV0hoef_DqaNgcZyPif4C89eAFZGn80p8xWroRnEn3AJhPL");
    String str = adapter.fromJson(reader);
    Log.d("Debug", "str = "+str);

The same problem happened again.
But when I changed to

    Gson gson = new Gson();
    Reader reader = new StringReader("0qQ9imBV5zYCZi5NRIZCl9WCivkSjj5SR5Ya8Diw0Urw6RV0hoef_DqaNgcZyPif4C89eAFZGn80p8xWroRnEn3AJhPL");
    String str = gson.fromJson(reader, String.class);
    Log.d("Debug", "str = "+str);

It goes safe.

@JakeWharton
Copy link
Collaborator

This is actually a Gson bug in not supporting this from RFC 7159 in its strict mode. However, supporting a way to enable lenient mode in Gson is the Retrofit bug here.

@JakeWharton
Copy link
Collaborator

First step: google/gson#771. Once Gson has a release we can consume the newJsonReader API just like we do newJsonWriter to provide lenient parsing.

@JakeWharton JakeWharton added this to the 2.0 milestone Jan 17, 2016
@JakeWharton
Copy link
Collaborator

And here's the fix in Gson (google/gson#773) and Moshi (square/moshi#121) for supporting top-level value types.

@andreaturli
Copy link

is there a way to test it using a new moshi release?
or we need to wait for new Gson release to have a retrofit beta4 ?
Thanks!

@JakeWharton
Copy link
Collaborator

For Gson it still requires an update to Retrofit to use its new API. For Moshi, once its v1.1 released (hopefully today, maybe tomorrow), all that is needed is ensuring you are using its latest version with any version of Retrofit.

@andreaturli
Copy link

thanks for the update!

@JakeWharton
Copy link
Collaborator

FYI Gson 2.6.1 was released with RFC 7159 support which should allow this to work with any version of Retrofit without needing Gson to be set to lenient.

That said, I'm still adding support for honoring leniency in Retrofit's Gson converter (and using this issue to track).

@NeoLSN
Copy link
Author

NeoLSN commented Feb 13, 2016

Great news about this. Thanks.

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

No branches or pull requests

3 participants