discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

toString on JsonPrimitve does not produce valid Json #107

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Compile attached file GsonTest.java
2. Run it

What is the expected output? What do you see instead?
The line break and tab should be escaped. Currently they are not escaped and
therefore the output is not valid Json. The documentation for the toString
method claims that it is.

What version of the product are you using? On what operating system?
1.3b2 on Linux

Original issue reported on code.google.com by dennis.b...@gmx.net on 11 Mar 2009 at 5:46

Attachments:

GoogleCodeExporter commented 9 years ago
This is a valid bug, but relatively hard to fix since we would need to allow a 
way
for the user to specify whether to escape Html characters or not. 

Original comment by inder123 on 11 Mar 2009 at 9:06

GoogleCodeExporter commented 9 years ago
What do you mean with HTML character?

The JSON specification <http://www.ietf.org/rfc/rfc4627.txt> clearly specifies 
in the 
last sentence of the first paragraph of section 2.5. Strings which characters 
must be
escaped.

Original comment by dennis.b...@gmx.net on 11 Mar 2009 at 9:34

GoogleCodeExporter commented 9 years ago
The contents of the string may contain HTML characters and Gson provides a 
GsonBuilder setting to escape them. We would have to provide a similar setting 
to 
the Constructor (or some other way) in the toString() method to make it work 
correctly. For now, I have updated the Javadoc to remove this claim. But I will 
leave this bug open because it is worth fixing in future.

Original comment by inder123 on 27 Mar 2009 at 7:31

GoogleCodeExporter commented 9 years ago
I'm not sure what this issue has to do with HTML characters.  To produce valid 
JSON,
you just need to escape "s and the control characters listed in the JSON spec:
http://www.json.org/

If the string being escaped contains HTML, then that's fine, but escaping for 
HTML
should be outside of the purview of this library since this library deals with 
JSON
not HTML.

Unless there is something I am missing?

Original comment by z13g...@gmail.com on 14 Apr 2009 at 8:31

GoogleCodeExporter commented 9 years ago
I think I understand the issue.  You are depending on the toString(Appender) 
method
in JsonPrimitive to generate unescaped JSON so that the Serializer can do the 
escaping.

I am running into the issue because I'm not using the Gson class at all because 
I
don't need the Json <--> Object stuff.  I am just parsing JSON via JsonParser to
generate the JsonElements then modifying it and outputting it.

IMHO, the toString method on the JsonElements should return properly escaped 
JSON. 
If it doesn't, then the method isn't of much use except by the internal methods 
since
the JSON it is generating is invalid.

Is my interpretation correct?  If it is, I'm still not clear how the HTML 
escaping
fits into the problem.

Original comment by z13g...@gmail.com on 14 Apr 2009 at 9:29

GoogleCodeExporter commented 9 years ago
I agree that this is a valid problem. The toString() method should just do JSON
escaping and not HTML escaping (setting through GsonBuilder). 

Original comment by inder123 on 14 Apr 2009 at 9:34

GoogleCodeExporter commented 9 years ago
FYI... In the meantime, here is a quicky patch which adds the functionality via 
a
second toEscapedString method.

Original comment by z13g...@gmail.com on 14 Apr 2009 at 9:48

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Original comment by joel.leitch@gmail.com on 23 Sep 2009 at 6:49