Now, a coding question. I'm wondering: For the all_experiments_json property of the main optimizely object, why is the value JSON-as-a-string, rather than just JSON? You're forcing yourself to parse it from a string to native JSON for no apparent reason. Just curious :)
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?
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 :)