Gson: Reading strings with white spaces - JsonReader

Created on 19 Dec 2016  路  5Comments  路  Source: google/gson

I'm using read method of TypeAdapter to read a Json message where some values consists of strings with white spaces.
It seems that readString, skipValue or any method to consume the json value (with white spaces) do not work correctly and drives the app to a MalFormedException trying to consume next characters.
Debugging the app I found that pointer position is in the white space when the exception occure, so it seems that for some reason the parser assumes that the end of the value is in the space and not in the the next comma. Requesting the data from the browser I see that all names and values are wrapped in double quotes, but on JsonReader there's no double quotes. There's some property that I must change? Lenient property is set to true, I cannot find any other property to change this behavior.

Can you please check it out? JsonReader is a great tool but this little issue is very prohibitive. Thanks

Most helpful comment

@rmpt @hgschwibbe
Try this. It's fix for me
gson.fromJson(gson.toJson(<your_data>), YourClassName::class.java)

All 5 comments

Same problem in our project. We use Gson 2.7.

@rmpt @hgschwibbe
Try this. It's fix for me
gson.fromJson(gson.toJson(<your_data>), YourClassName::class.java)

@rmpt @hgschwibbe
Try this. It's fix for me
gson.fromJson(gson.toJson(<your_data>), YourClassName::class.java)

Thanks @osip-000 ! it worked for me also

mark

Could you please provide an example JSON string and the code for reading it for which the issue occurs?
I assume you are talking about lenient unquoted strings / names, so something like this:

[
  unquote-stringSPACE,
  next-value
]

(though I don't understand why the workaround would then help in this situation since apparently you are getting the JSON from something other than Gson)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  路  15Comments

GoogleCodeExporter picture GoogleCodeExporter  路  25Comments

GoogleCodeExporter picture GoogleCodeExporter  路  20Comments

RobMans426 picture RobMans426  路  20Comments

GoogleCodeExporter picture GoogleCodeExporter  路  17Comments