Skip to content
Browse files

Clean up

  • Loading branch information...
1 parent cc24ac2 commit afec7c6423c7efa7c0f53bb3744af5bed391daa4 @bruth committed Jan 1, 2013
Showing with 19 additions and 52 deletions.
  1. +9 −48 restlib2/serializers/_json.py
  2. +3 −3 restlib2/serializers/_xml.py
  3. +7 −1 run-tests.sh
View
57 restlib2/serializers/_json.py
@@ -1,70 +1,31 @@
-from datetime import datetime, date, time
-from decimal import Decimal
-from django.utils import simplejson
-from django.conf import settings
-
-DATE_FORMAT = '%Y-%m-%d'
-TIME_FORMAT = '%H:%M:%S'
-
-class JSONEncoder(simplejson.JSONEncoder):
- "Enhances the default JSONEncoder to handle other Python types."
- def default(self, obj):
-
- if isinstance(obj, set):
- return list(set)
-
- if isinstance(obj, Decimal):
- return float(str(obj))
-
- if isinstance(obj, datetime):
- return obj.strftime('%s %s' % (DATE_FORMAT, TIME_FORMAT))
-
- if isinstance(obj, date):
- return obj.strftime(DATE_FORMAT)
-
- if isinstance(obj, time):
- return obj.strftime(TIME_FORMAT)
-
- return super(JSONEncoder, self).default(obj)
-
-
-class JSONDecoder(simplejson.JSONDecoder):
- "Enhances the default JSONDecoder to handles other Python types."
- # TODO determine if date and times are worth supporting. my initial
- # gut is no since it is unknown whether the data is going to be handled
- # as strings (e.g. saved off in a database) or used as Python objects.
- # also, there will be a large performance hit for large objects since
- # *every* string would have to be tested to see if it matches a datetime
- # format.
- def default(self, obj):
- return super(JSONDecoder, self).default(obj)
+from django.core.serializers.json import simplejson, DjangoJSONEncoder
class JSON(object):
"""Very basic JSON representation encode/decoder. Additional Python types
are supported via a encoder subclass including: set, Decimal, datetime,
date and time objects.
"""
+ encoder_class = DjangoJSONEncoder
+ decoder_class = simplejson.JSONDecoder
+
encode_options = {}
decode_options = {}
- if settings.DEBUG:
- encode_options = {
- 'indent': 4,
- 'sort_keys': True,
- }
+ def __init__(self):
+ self.encode_options.setdefault('indent', 4)
+ self.encode_options.setdefault('sort_keys', True)
def encode(self, data, options=None, **kwargs):
if options is None:
options = self.encode_options
- encoder = JSONEncoder(**options)
+ encoder = self.encoder_class(**options)
return encoder.encode(data)
def decode(self, data, options=None, **kwargs):
if options is None:
options = self.decode_options
- decoder = JSONDecoder(**options)
+ decoder = self.decoder_class(**options)
return decoder.decode(data)
-
View
6 restlib2/serializers/_xml.py
@@ -27,10 +27,10 @@ def _decode(self, parent, converters, path=None):
# if this is true, there are multiple elements with this tag name.
# the value of this entry must now turn into a list
- if node.has_key(tag):
+ if tag in node:
# if this is true, we must take the existing value and insert it
# at the beginning of the list
- if type(node[tag]) is not list:
+ if isinstance(node[tag], list):
node[tag] = [node[tag]]
# recurse
node[tag].append(subnode)
@@ -50,7 +50,7 @@ def _decode(self, parent, converters, path=None):
else:
# check if a converter exist for this tag
_path = '/'.join(path)
- if converters.has_key(_path):
+ if _path in converters:
text = converters[_path](parent.text)
else:
text = parent.text
View
8 run-tests.sh
@@ -1,5 +1,11 @@
#!/bin/sh
-DJANGO_SETTINGS_MODULE='restlib2.tests.settings' PYTHONPATH=. coverage run `which django-admin.py` test restlib2
+ARGS="$@"
+
+if [ ! $ARGS ]; then
+ ARGS="restlib2"
+fi
+
+DJANGO_SETTINGS_MODULE='restlib2.tests.settings' PYTHONPATH=. coverage run `which django-admin.py` test "$ARGS"
rm -rf docs/coverage
coverage html

0 comments on commit afec7c6

Please sign in to comment.
Something went wrong with that request. Please try again.