> try:
> import json
> assert hasattr(json, "loads") and hasattr(json, "dumps")
> _json_decode = lambda s: json.loads(s)
> _json_encode = lambda v: json.dumps(v)
This should probably be:
_json_decode = json.loads
_json_encode = json.dumps
The lambda amounts to an additional function call with no visible
benefit. Given the frequency of these calls it's probably a measurable
amount of overhead.
Regards,
Cliff
My hack is under the block you saw there. :)
import jspickle
_json_decode = jspickle.decode
_json_encode = jspickle.encode
D
You could also achieve this without hacking the Tornado source by using:
import jspickle
import tornado.escape
tornado.escape._json_decode = jspickle.decode
tornado.escape._json_encode = jspickle.encode
Modules are singletons, so this change is global.
Regards,
Cliff
Actually, it turns out that jspickle also escapes forward slashes:
>>> jspickle.encode("</")
'"<\\/"'
so there's no need for the provided tornado functions at all. That
makes this preferable:
tornado.escape.json_decode = jspickle.decode
tornado.escape.json_encode = jspickle.encode
This eliminates an extra function call for each.
Regards,
Cliff
I'll adjust our code. :)
This kind of subtle behavioral difference is exactly why
tornado.escape doesn't try to silently switch between different JSON
libraries depending on what third-party packages are installed. There
aren't many places where Tornado JSON-encodes things for you (it's
mostly used in the auth module which isn't performance-critical). If
you want to use a different JSON library for performance, I recommend
simply calling it instead of tornado.escape.json_{en,de}code (and
maybe override write() in your RequestHandler subclass to do the
dict-to-json conversion with this library).
-Ben