Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

ToJSon does not generate valid JSON #4

Closed
GoogleCodeExporter opened this issue May 5, 2015 · 5 comments
Closed

ToJSon does not generate valid JSON #4

GoogleCodeExporter opened this issue May 5, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

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.

Original issue reported on code.google.com by m...@matt-good.net on 20 Jul 2009 at 10:05

@GoogleCodeExporter
Copy link
Author

I ran into this issue as I tried to use it with jQuery.getJSON. A bad 
combination since the function fails silently if the JSON isn't valid. Please 
fix this or at least rename the method.

Original comment by martin.thurau on 25 Sep 2010 at 11:21

@GoogleCodeExporter
Copy link
Author

I ran into this issue as well.  There is a stackoverflow question which would 
be useful if to keep up to date:

http://stackoverflow.com/questions/6007381/invalid-json-string-in-google-visuali
zation-api-example

Original comment by dgoris...@gmail.com on 19 May 2011 at 10:48

@GoogleCodeExporter
Copy link
Author

I am using gviz_api with jquery ajax and jquery 1.6 is using real ajax parser 
so the "json" produced by ToJSon() doesn't work.
Any plans to fix it?

Original comment by jacek.my...@gmail.com on 28 Aug 2011 at 5:57

@GoogleCodeExporter
Copy link
Author

This issue is now fixed as of r16. We should be preparing a new release shortly!

Original comment by jbas...@google.com on 10 Jan 2012 at 3:23

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

Thanks for fixing this.

Original comment by w...@whizzy.org on 4 Feb 2012 at 3:13

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant