Commits

Anonymous committed e6b48ad

Always use unicode literals. Use slightly faster comma sequence

  • Participants
  • Parent commits ee7b45f

Comments (0)

Files changed (1)

File gnocchi/ajax/serialiser.py

 
 from django.core.serializers.python import Serializer
 from django.utils.encoding import smart_unicode
-from django.utils.simplejson.encoder import INFINITY, encode_basestring, JSONEncoder as BaseEncoder
+# Py 2.6 doesn't appear to have INFINITY
+from json.encoder import (
+    INFINITY,
+    ESCAPE_DCT,
+    ESCAPE,
+    JSONEncoder as BaseEncoder
+)
+from django.utils.simplejson import loads
+
 
 import datetime
 from itertools import chain, repeat
 from decimal import Decimal
 
-def digattr(obj, attr, default=None):
-    '''Perform template-style dotted lookup'''
-    steps = attr.split('.')
-    for step in steps:
-        try:    # dict lookup
-            obj = obj[step]
-        except (TypeError, AttributeError, KeyError):
-            try:    # attribute lookup
-                obj = getattr(obj, step)
-            except (TypeError, AttributeError):
-                try:    # list index lookup
-                    obj = obj[int(step)]
-                except (IndexError, ValueError, KeyError, TypeError):
-                    return default
-        if callable(obj):
-            obj = obj()
-    return obj
+from .utils import digattr
 
 class SimpleSerialiser(Serializer):
     '''A simple JSON serialiser, supporting recursive serialising'''
     def end_serialization(self):
         self.objects = self.objects[0]
 
-# From simplejson
-ESCAPE_DCT = {
-    '\\': '\\\\',
-    '"': '\\"',
-    '\b': '\\b',
-    '\f': '\\f',
-    '\n': '\\n',
-    '\r': '\\r',
-    '\t': '\\t',
-    u'\u2028': '\\u2028',
-    u'\u2029': '\\u2029',
-}
-for i in range(0x20):
-    ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % i)
-# end simplejson
-
 class JSONEncoder(BaseEncoder):
     '''JSON Encoder class with added type casting
 
     def encode(self, obj):
 
         def _encode(obj):
-            FIRST_SEP = ['']
-            NEXT_SEP = ','
+            SEP = repeat(u', ')
             if isinstance(obj, basestring):
-                yield '"' + ESCAPE.sub(lambda x: ESCAPE_DCT[x.group(0)], obj) + '"'
+                yield u'"' + ESCAPE.sub(lambda x: ESCAPE_DCT[x.group(0)], obj) + u'"'
             elif obj is None:
                 yield 'null'
             elif obj is True:
-                yield 'true'
+                yield u'true'
             elif obj is False:
-                yield 'false'
+                yield u'false'
             elif isinstance(obj, (int, long)):
                 yield unicode(obj)
             elif isinstance(obj, float):
                 if obj != obj:
-                    yield 'NaN'
+                    yield u'NaN'
                 elif obj == INFINITY:
-                    yield 'Infinity'
+                    yield u'Infinity'
                 elif obj == -INFINITY:
-                    yield '-Infinity'
+                    yield u'-Infinity'
                 else:
                     yield unicode(obj)
             elif isinstance(obj, Decimal):
                 yield unicode(Decimal)
             elif isinstance(obj, datetime.datetime):
-                yield '"'+obj.replace(microsecond=0).isoformat(' ')+'"'
+                yield u'"'+obj.replace(microsecond=0).isoformat(u' ')+u'"'
             elif isinstance(obj, datetime.date):
-                yield '"'+obj.isoformat()+'"'
+                yield u'"'+obj.isoformat()+u'"'
             elif isinstance(obj, dict):
-                yield '{'
-                sep = chain(FIRST_SEP, repeat(NEXT_SEP))
+                yield u'{'
+                sep = chain([''], SEP)
                 for key, val in obj.iteritems():
                     yield sep.next()
                     for chunk in chain( _encode(key), [': '], _encode(val)):
                         yield chunk
-                yield '}'
+                yield u'}'
             elif isinstance(obj, (list, tuple)) or hasattr(obj, '__iter__'):
-                yield '['
-                sep = chain(FIRST_SEP, repeat(NEXT_SEP))
+                sep = chain([''], SEP)
+                yield u'['
                 for item in obj:
                     yield sep.next()
                     for chunk in _encode(item):
                         yield chunk
-                yield ']'
+                yield u']'
             else:
                 obj = self.default(obj)
                 for chunk in _encode(obj):