have double quotes in string, gson won't escape it.

10,453 views
Skip to first unread message

stalker

unread,
Jul 11, 2009, 12:02:16 AM7/11/09
to google-gson
Hi guys gson is not escaping double quotes in string. Anybody have any
solution ?

Demo program

/******************/
public class Demo{
public static void main(String args[]){
String test = "Hey there. I am \"here\"";
JsonObject jsonObject = JsonParser.parse(new Gson().toJson(test));
System.out.println("JSON is " + jsonObject.toString());
}
}


GSON is not escaping the double quote inside string...

Please help..

Dennis Benzinger

unread,
Jul 11, 2009, 8:39:09 AM7/11/09
to googl...@googlegroups.com
> [...]

That's bug 107. See
http://code.google.com/p/google-gson/issues/detail?id=107


Regards,
Dennis Benzinger

inder

unread,
Jul 12, 2009, 12:54:35 AM7/12/09
to google-gson
Yes. JsonObject.toString() is not meant to output valid JSON. You
should instead use Gson.toJson()

stalker

unread,
Jul 12, 2009, 2:58:15 AM7/12/09
to google-gson
I've tried to use Gson.toJson(<passed string>). Still I'm getting
wrong output.
can you provide me a valid example ?

Thanks and Regards,
Dhaval Yoganandi.

inder

unread,
Jul 13, 2009, 2:29:45 PM7/13/09
to google-gson
See the method testEscapingQuotesInStringSerialization() in
http://code.google.com/p/google-gson/source/browse/trunk/gson/src/test/java/com/google/gson/functional/StringTest.java
for an example on how this is done.

That file also contains many other escaping samples that might be of
interest to you.

Inder
Reply all
Reply to author
Forward
0 new messages