Permalink
Browse files

json dumping was not working unless simplejson is installed

  • Loading branch information...
rckclmbr committed Aug 18, 2011
1 parent a597804 commit 1c188fe1cf58118561b18dc095297baab5f3e24f
Showing with 9 additions and 1 deletion.
  1. +9 −1 tornadio/proto.py
View
@@ -10,8 +10,16 @@
"""
try:
import simplejson as json
json_decimal_args = {"use_decimal":True}
except ImportError:
import json
import decimal
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
return super(DecimalEncoder, self).default(o)
json_decimal_args = {"cls":DecimalEncoder}
FRAME = '~m~'
HEARTBEAT = '~h~'
@@ -32,7 +40,7 @@ def encode(message):
elif (not isinstance(message, (unicode, str))
and isinstance(message, (object, dict))):
if message is not None:
encoded += encode('~j~' + json.dumps(message, use_decimal=True))
encoded += encode('~j~' + json.dumps(message, **json_decimal_args))
else:
msg = message.encode('utf-8')
encoded += "%s%d%s%s" % (FRAME, len(msg), FRAME, msg)

0 comments on commit 1c188fe

Please sign in to comment.