Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fixed problem: serializers datetime.datetime to JSON #12

Closed
wants to merge 5 commits into from

2 participants

@yurtaev

No description provided.

@mrjoes
Owner

Need support for json module as well, or it can serialize datetime without any additional modifications?

@yurtaev

I'll try to check now.

@yurtaev

Oh, is now the problems with datetime should not be

@yurtaev

Not actual fix after #50, thx

@yurtaev yurtaev closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 14, 2011
  1. @yurtaev
  2. @yurtaev
Commits on Dec 17, 2011
  1. @yurtaev
Commits on Jan 19, 2012
  1. @yurtaev
Commits on Feb 16, 2012
  1. @yurtaev

    fix: 'name' key in json args

    yurtaev authored
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 18 deletions.
  1. +9 −11 tornadio2/conn.py
  2. +15 −5 tornadio2/proto.py
  3. +2 −2 tornadio2/session.py
View
20 tornadio2/conn.py
@@ -151,7 +151,7 @@ def on_message(self, message):
"""Default on_message handler. Must be overridden in your application"""
raise NotImplementedError()
- def on_event(self, name, *args, **kwargs):
+ def on_event(self, name, data, *args, **kwargs):
"""Default on_event handler.
By default, it uses decorator-based approach to handle events,
@@ -159,10 +159,8 @@ def on_event(self, name, *args, **kwargs):
`name`
Event name
- `args`
- Event args
- `kwargs`
- Event kwargs
+ `data`
+ Event args or kwargs
There's small magic around event handling.
If you send exactly one parameter from the client side and it is dict,
@@ -194,19 +192,19 @@ def on_event(self, name, *args, **kwargs):
if handler:
try:
- if args:
- return handler(self, *args)
+ if isinstance(data, list):
+ return handler(self, *data)
else:
- return handler(self, **kwargs)
+ return handler(self, **data)
except TypeError:
- if args:
+ if isinstance(data, list):
logging.error(('Attempted to call event handler %s ' +
'with %s arguments.') % (handler,
- repr(args)))
+ repr(data)))
else:
logging.error(('Attempted to call event handler %s ' +
'with %s arguments.') % (handler,
- repr(kwargs)))
+ repr(data)))
raise
else:
logging.error('Invalid event name: %s' % name)
View
20 tornadio2/proto.py
@@ -21,10 +21,17 @@
Socket.IO protocol related functions
"""
import logging
+import datetime
+from bson import json_util
+
+dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
try:
import simplejson as json
- json_decimal_args = {"use_decimal": True}
+ json_decimal_args = {
+ "use_decimal": True,
+ "default": json_util.default,
+ }
except ImportError:
import json
import decimal
@@ -34,7 +41,10 @@ def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
return super(DecimalEncoder, self).default(o)
- json_decimal_args = {"cls": DecimalEncoder}
+ json_decimal_args = {
+ "cls": DecimalEncoder,
+ "default": json_util.default,
+ }
# Packet ids
DISCONNECT = '0'
@@ -140,7 +150,7 @@ def event(endpoint, name, message_id, *args, **kwargs):
return u'5:%s:%s:%s' % (
message_id or '',
endpoint or '',
- json.dumps(evt)
+ json.dumps(evt, **json_decimal_args)
)
@@ -194,7 +204,7 @@ def json_dumps(msg):
`msg`
Object to dump
"""
- return json.dumps(msg)
+ return json.dumps(msg, **json_decimal_args)
def json_load(msg):
@@ -203,7 +213,7 @@ def json_load(msg):
`msg`
json encoded object
"""
- return json.loads(msg)
+ return json.loads(msg, object_hook=json_util.object_hook)
def decode_frames(data):
View
4 tornadio2/session.py
@@ -401,9 +401,9 @@ def raw_message(self, msg):
# Fix for the http://bugs.python.org/issue4978 for older Python versions
str_args = dict((str(x), y) for x, y in args[0].iteritems())
- ack_response = conn.on_event(event['name'], **str_args)
+ ack_response = conn.on_event(event['name'], data=str_args)
else:
- ack_response = conn.on_event(event['name'], *args)
+ ack_response = conn.on_event(event['name'], data=args)
if msg_id:
if msg_id.endswith('+'):
Something went wrong with that request. Please try again.