Handling invalid JSON with gson.

818 views
Skip to first unread message

Max Kotasek

unread,
Sep 18, 2012, 4:08:25 PM9/18/12
to googl...@googlegroups.com
Hello - I'm looking for advice on how to handle a situation where the JSON may not be properly formatted for some of the records in a java application.   

In this case, I make a web services call to retrieve a block of JSON formatted data, but some of the records returned are containing characters such as an unescaped double quote ' " '.  This is due to some encoding issues which would ideally be fixed - but in the meantime, is there a way to bypass the record in an elegant way?  The only thing I can think of is to break apart the incoming stream by braces and attempt to use gson to parse the contents in between.  I don't like this method because I'm having to perform work that gson was already providing me - such as taking the entire JSON blob and giving me a java object instead of a series of java objects.

Any thoughts on how to use gson in a fault tolerant way?

Thanks,
Max

Maaartin

unread,
Sep 18, 2012, 6:51:12 PM9/18/12
to googl...@googlegroups.com
On Tuesday, September 18, 2012 10:08:25 PM UTC+2, Max Kotasek wrote:
Any thoughts on how to use gson in a fault tolerant way?

There's
but I doubt it helps with the quotes as they're changing the document structure. I could imagine to preprocess the json by enquoting such characters everywhere where they're surely illegal, but whatever you do you may seriously misinterpret the input.

Knowing if such chars comes in pairs could help. Running `JsonReader` manually and retrying when it throws an exception could help, too. However, whatever you do it's just guessing and it may be wrong. I'd go and fix the input provider if ever possible.

Max Kotasek

unread,
Sep 21, 2012, 2:34:34 PM9/21/12
to googl...@googlegroups.com
Thanks for the reply.  I'm contacting them and getting it fixed.  I'm not sure if setLenient would work in this scenario for the reasons you stated.  It may be useful if I find that it doesn't get fixed in the way I expect though. :)

I ended up trying the regex solution - breaking up the fields before calling gson handlers then trying individual records with gson.  Wasn't as bad as I'd imagine - I just worry about introducing an error or changing the data contents somehow when I run into an unexpected combination of symbols as part of the data, especially if the data format changes a bit.

Max


On Tuesday, September 18, 2012 5:51:12 PM UTC-5, Maaartin wrote:
On Tuesday, September 18, 2012 10:08:25 PM UTC+2, Max Kotasek wrote:
Any thoughts on how to use gson in a fault tolerant way?


Milan Gvero

unread,
Jan 30, 2020, 11:58:43 AM1/30/20
to google-gson
Wow a post 8 years ago :D I kind of a have same problem. Json that comes from server is missing } symbols. And now i am not sure if setLenient is going to help. If it is that is fantastic, but if not, how can i cough this malformed exception, and prevent app from crashing? At least until problems with server are fixed.
Thanks :)
Reply all
Reply to author
Forward
0 new messages