Opened 12 years ago
Last modified 12 years ago
#10771 closed defect
`to_json` generates malformed json on Python 2.4-2.5 if contained single qoutes — at Initial Version
Reported by: | Jun Omae | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | 1.0 |
Component: | general | Version: | 0.12 |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: | |||
API Changes: | |||
Internal Changes: |
Description
to_json
generates malformed json on Python 2.4-2.5 if the argument is contained single qoutes. to_json
uses javascript_quote
because Python 2.4-2.5 don't have json library.
Python 2.4.3 (#1, Jun 18 2012, 08:55:31) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from trac.util.presentation import to_json >>> print to_json({'json': "a ' single quote"}) {"json":"a \' single quote"}
However, single quote cannot be encoded to \'
, according to http://www.json.org/.
And, jQuery.parseJSON
raises SyntaxError
for such a json string. This brings a problem when Trac and Trac plugins response json as contents.
[12:46:48.548] jQuery.parseJSON("{\"json\": \"a \\' single quote\"}") [12:46:48.553] SyntaxError: JSON.parse: bad escaped character [12:46:51.864] jQuery.parseJSON("{\"json\": \"a ' single quote\"}") [12:46:51.868] ({json:"a ' single quote"}) [12:52:32.423] jQuery.parseJSON("{\"json\": \"a \\u0027 single quote\"}") [12:52:32.427] ({json:"a ' single quote"})
I propose that javascript_quote
encodes a single quote to \u0027
(neither '
nor \'
) in order to avoid \'
in json text.