4 ms·
Thanks for not giving it away. :) As for your question, I think you're right that we could just write this as straight JSON without wrapping it in quotes. What
by dsiroker 16y ago
Thanks for not giving it away. :)
As for your question, I think you're right that we could just write this as straight JSON without wrapping it in quotes. What are the benefits of doing this?
I tried just putting the all_experiments_json property in Firebug without wrapping it in quotes and I get a 'SyntaxError: invalid label' error. Looks like that is because the keys of the dicts are strings. Probably a way to generate this JSON server-side that makes Javascript happy.
One potential downside of making it just straight JSON is that this is generated by our Python template and if for whatever reason this isn't valid JSON it would throw a javascript exception when the file is loaded.
Right now our backend that generates this JSON does this:
dump = simplejson.dumps(experiments_dict)
dump = dump.replace("\\", "\\\\")
dump = dump.replace("'", "\\'")
The dump is the block of text that gets wrapped by '' in our template.
Thoughts on a better way to do this?
- photon_off 16y agoFirst off, there aren't really any benefits, other than it being simpler and more elegant. Right now you're going from Python Dictionary -> JSON-like-string -> JSON. There should be a way to go Python Dictionary -> JSON. It just seems like a code smell. Really though, if it ain't broke, don't worry about it. I was just curious as to why there was this roundabout. Removing the quotes won't fix the problem, because you've also altered the way characters are escaped via the .replace() calls. One potential downside of making it just straight JSON is that this is generated by our Python template and if for whatever reason this isn't valid JSON it would throw a javascript exception when the file is loaded. I'm not familiar with Python, but there shouldn't be any reason why simplejson would return invalid JSON. In the event that it does, your script would still break, anyway. Granted, it won't be a javascript exception, but it would certainly fail elsewhere. Like I said before, it really doesn't matter because it works :)