Status: New
Owner: ----
Labels: Type-Defect Priority-Medium
New issue 4 by [email protected]: ToJSon does not generate valid JSON
http://code.google.com/p/google-visualization-python/issues/detail?id=4
ToJSon returns valid syntax for a JavaScript object, but it is not
parseable with many JSON implementations
because it uses syntax that is not within the subset defined by the JSON
standard.
The simplest solution is probably to rename this function or at least
document that it doesn't really return
JSON, but expects to be executed by a full JS interpreter.
For example, the result will not parse with Python 2.6's json package:
>>> d = gviz_api.DataTable(('a', 'string'), ['foo'])
>>> d.ToJSon()
"{cols: [{id:'a',label:'a',type:'string'}],rows: [{c:[{v:'foo'}]}]}"
>>> json.loads(d.ToJSon())
Traceback (most recent call last):
...
ValueError: Expecting property name: line 1 column 1 (char 1)
Some more basic issues are:
- JSON strings must use double-quotes
- JSON keys must be quoted following the string syntax
e.g. {"key": "value"}, not {key: 'value'}
Python's default unicode escaping is also not compatible with JSON
strings. For example, it will use \x hex
escape codes which do happen to work in JavaScript, but JSON only allows \u
4-digit hex escapes, e.g.
Wrong:
>>> json.loads('"\\xfc"')
Traceback (most recent call last):
...
ValueError: Invalid \escape: line 1 column 1 (char 1)
Right:
>>> json.loads('"\\u00fc"')
u'\xfc'
Also, JSON does not define any syntax for Date types, so the "new Date()"
syntax is not supported by most
non-JS-native JSON parsers.
Since this data structure is primarily designed for consumption by the JS
viz API, I don't have a problem with
the syntax remaining as it is, just please don't misrepresent it as "JSON",
which could cause confusion if
people try to parse it with a JSON library.
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-visualization-api?hl=en
-~----------~----~----~----~------~----~------~--~---