Skip to content

Commit

Permalink
Merge pull request mrjoes#30 from rckclmbr/master
Browse files Browse the repository at this point in the history
Error dumping json when simplejson is not installed
  • Loading branch information
mrjoes committed Sep 1, 2011
2 parents a597804 + 1c188fe commit 7453d84
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tornadio/proto.py
Expand Up @@ -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~'
Expand All @@ -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)
Expand Down

0 comments on commit 7453d84

Please sign in to comment.