author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Sun, 25 Jan 2009 11:31:15 +0000 | |
changeset 975 | 295d67509412 |
permissions | -rw-r--r-- |
975
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1 |
"""Implementation of JSONEncoder |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
2 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
3 |
import re |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
4 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
5 |
try: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
6 |
from simplejson._speedups import encode_basestring_ascii as c_encode_basestring_ascii |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
7 |
except ImportError: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
8 |
c_encode_basestring_ascii = None |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
9 |
try: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
10 |
from simplejson._speedups import make_encoder as c_make_encoder |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
11 |
except ImportError: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
12 |
c_make_encoder = None |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
13 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
14 |
ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]') |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
15 |
ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
16 |
HAS_UTF8 = re.compile(r'[\x80-\xff]') |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
17 |
ESCAPE_DCT = { |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
18 |
'\\': '\\\\', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
19 |
'"': '\\"', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
20 |
'\b': '\\b', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
21 |
'\f': '\\f', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
22 |
'\n': '\\n', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
23 |
'\r': '\\r', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
24 |
'\t': '\\t', |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
25 |
} |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
26 |
for i in range(0x20): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
27 |
ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
28 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
29 |
# Assume this produces an infinity on all machines (probably not guaranteed) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
30 |
INFINITY = float('1e66666') |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
31 |
FLOAT_REPR = repr |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
32 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
33 |
def encode_basestring(s): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
34 |
"""Return a JSON representation of a Python string |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
35 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
36 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
37 |
def replace(match): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
38 |
return ESCAPE_DCT[match.group(0)] |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
39 |
return '"' + ESCAPE.sub(replace, s) + '"' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
40 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
41 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
42 |
def py_encode_basestring_ascii(s): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
43 |
"""Return an ASCII-only JSON representation of a Python string |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
44 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
45 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
46 |
if isinstance(s, str) and HAS_UTF8.search(s) is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
47 |
s = s.decode('utf-8') |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
48 |
def replace(match): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
49 |
s = match.group(0) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
50 |
try: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
51 |
return ESCAPE_DCT[s] |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
52 |
except KeyError: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
53 |
n = ord(s) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
54 |
if n < 0x10000: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
55 |
return '\\u%04x' % (n,) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
56 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
57 |
# surrogate pair |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
58 |
n -= 0x10000 |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
59 |
s1 = 0xd800 | ((n >> 10) & 0x3ff) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
60 |
s2 = 0xdc00 | (n & 0x3ff) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
61 |
return '\\u%04x\\u%04x' % (s1, s2) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
62 |
return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
63 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
64 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
65 |
encode_basestring_ascii = c_encode_basestring_ascii or py_encode_basestring_ascii |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
66 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
67 |
class JSONEncoder(object): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
68 |
"""Extensible JSON <http://json.org> encoder for Python data structures. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
69 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
70 |
Supports the following objects and types by default: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
71 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
72 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
73 |
| Python | JSON | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
74 |
+===================+===============+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
75 |
| dict | object | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
76 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
77 |
| list, tuple | array | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
78 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
79 |
| str, unicode | string | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
80 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
81 |
| int, long, float | number | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
82 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
83 |
| True | true | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
84 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
85 |
| False | false | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
86 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
87 |
| None | null | |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
88 |
+-------------------+---------------+ |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
89 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
90 |
To extend this to recognize other objects, subclass and implement a |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
91 |
``.default()`` method with another method that returns a serializable |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
92 |
object for ``o`` if possible, otherwise it should call the superclass |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
93 |
implementation (to raise ``TypeError``). |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
94 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
95 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
96 |
item_separator = ', ' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
97 |
key_separator = ': ' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
98 |
def __init__(self, skipkeys=False, ensure_ascii=True, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
99 |
check_circular=True, allow_nan=True, sort_keys=False, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
100 |
indent=None, separators=None, encoding='utf-8', default=None): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
101 |
"""Constructor for JSONEncoder, with sensible defaults. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
102 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
103 |
If skipkeys is False, then it is a TypeError to attempt |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
104 |
encoding of keys that are not str, int, long, float or None. If |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
105 |
skipkeys is True, such items are simply skipped. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
106 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
107 |
If ensure_ascii is True, the output is guaranteed to be str |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
108 |
objects with all incoming unicode characters escaped. If |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
109 |
ensure_ascii is false, the output will be unicode object. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
110 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
111 |
If check_circular is True, then lists, dicts, and custom encoded |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
112 |
objects will be checked for circular references during encoding to |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
113 |
prevent an infinite recursion (which would cause an OverflowError). |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
114 |
Otherwise, no such check takes place. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
115 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
116 |
If allow_nan is True, then NaN, Infinity, and -Infinity will be |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
117 |
encoded as such. This behavior is not JSON specification compliant, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
118 |
but is consistent with most JavaScript based encoders and decoders. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
119 |
Otherwise, it will be a ValueError to encode such floats. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
120 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
121 |
If sort_keys is True, then the output of dictionaries will be |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
122 |
sorted by key; this is useful for regression tests to ensure |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
123 |
that JSON serializations can be compared on a day-to-day basis. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
124 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
125 |
If indent is a non-negative integer, then JSON array |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
126 |
elements and object members will be pretty-printed with that |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
127 |
indent level. An indent level of 0 will only insert newlines. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
128 |
None is the most compact representation. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
129 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
130 |
If specified, separators should be a (item_separator, key_separator) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
131 |
tuple. The default is (', ', ': '). To get the most compact JSON |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
132 |
representation you should specify (',', ':') to eliminate whitespace. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
133 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
134 |
If specified, default is a function that gets called for objects |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
135 |
that can't otherwise be serialized. It should return a JSON encodable |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
136 |
version of the object or raise a ``TypeError``. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
137 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
138 |
If encoding is not None, then all input strings will be |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
139 |
transformed into unicode using that encoding prior to JSON-encoding. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
140 |
The default is UTF-8. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
141 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
142 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
143 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
144 |
self.skipkeys = skipkeys |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
145 |
self.ensure_ascii = ensure_ascii |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
146 |
self.check_circular = check_circular |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
147 |
self.allow_nan = allow_nan |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
148 |
self.sort_keys = sort_keys |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
149 |
self.indent = indent |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
150 |
if separators is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
151 |
self.item_separator, self.key_separator = separators |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
152 |
if default is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
153 |
self.default = default |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
154 |
self.encoding = encoding |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
155 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
156 |
def default(self, o): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
157 |
"""Implement this method in a subclass such that it returns |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
158 |
a serializable object for ``o``, or calls the base implementation |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
159 |
(to raise a ``TypeError``). |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
160 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
161 |
For example, to support arbitrary iterators, you could |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
162 |
implement default like this:: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
163 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
164 |
def default(self, o): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
165 |
try: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
166 |
iterable = iter(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
167 |
except TypeError: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
168 |
pass |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
169 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
170 |
return list(iterable) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
171 |
return JSONEncoder.default(self, o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
172 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
173 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
174 |
raise TypeError("%r is not JSON serializable" % (o,)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
175 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
176 |
def encode(self, o): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
177 |
"""Return a JSON string representation of a Python data structure. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
178 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
179 |
>>> JSONEncoder().encode({"foo": ["bar", "baz"]}) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
180 |
'{"foo": ["bar", "baz"]}' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
181 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
182 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
183 |
# This is for extremely simple cases and benchmarks. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
184 |
if isinstance(o, basestring): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
185 |
if isinstance(o, str): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
186 |
_encoding = self.encoding |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
187 |
if (_encoding is not None |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
188 |
and not (_encoding == 'utf-8')): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
189 |
o = o.decode(_encoding) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
190 |
if self.ensure_ascii: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
191 |
return encode_basestring_ascii(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
192 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
193 |
return encode_basestring(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
194 |
# This doesn't pass the iterator directly to ''.join() because the |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
195 |
# exceptions aren't as detailed. The list call should be roughly |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
196 |
# equivalent to the PySequence_Fast that ''.join() would do. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
197 |
chunks = self.iterencode(o, _one_shot=True) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
198 |
if not isinstance(chunks, (list, tuple)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
199 |
chunks = list(chunks) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
200 |
return ''.join(chunks) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
201 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
202 |
def iterencode(self, o, _one_shot=False): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
203 |
"""Encode the given object and yield each string |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
204 |
representation as available. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
205 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
206 |
For example:: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
207 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
208 |
for chunk in JSONEncoder().iterencode(bigobject): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
209 |
mysocket.write(chunk) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
210 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
211 |
""" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
212 |
if self.check_circular: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
213 |
markers = {} |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
214 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
215 |
markers = None |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
216 |
if self.ensure_ascii: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
217 |
_encoder = encode_basestring_ascii |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
218 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
219 |
_encoder = encode_basestring |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
220 |
if self.encoding != 'utf-8': |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
221 |
def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
222 |
if isinstance(o, str): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
223 |
o = o.decode(_encoding) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
224 |
return _orig_encoder(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
225 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
226 |
def floatstr(o, allow_nan=self.allow_nan, _repr=FLOAT_REPR, _inf=INFINITY, _neginf=-INFINITY): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
227 |
# Check for specials. Note that this type of test is processor- and/or |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
228 |
# platform-specific, so do tests which don't depend on the internals. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
229 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
230 |
if o != o: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
231 |
text = 'NaN' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
232 |
elif o == _inf: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
233 |
text = 'Infinity' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
234 |
elif o == _neginf: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
235 |
text = '-Infinity' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
236 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
237 |
return _repr(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
238 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
239 |
if not allow_nan: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
240 |
raise ValueError("Out of range float values are not JSON compliant: %r" |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
241 |
% (o,)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
242 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
243 |
return text |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
244 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
245 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
246 |
if _one_shot and c_make_encoder is not None and not self.indent and not self.sort_keys: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
247 |
_iterencode = c_make_encoder( |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
248 |
markers, self.default, _encoder, self.indent, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
249 |
self.key_separator, self.item_separator, self.sort_keys, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
250 |
self.skipkeys, self.allow_nan) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
251 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
252 |
_iterencode = _make_iterencode( |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
253 |
markers, self.default, _encoder, self.indent, floatstr, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
254 |
self.key_separator, self.item_separator, self.sort_keys, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
255 |
self.skipkeys, _one_shot) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
256 |
return _iterencode(o, 0) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
257 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
258 |
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
259 |
## HACK: hand-optimized bytecode; turn globals into locals |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
260 |
False=False, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
261 |
True=True, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
262 |
ValueError=ValueError, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
263 |
basestring=basestring, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
264 |
dict=dict, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
265 |
float=float, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
266 |
id=id, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
267 |
int=int, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
268 |
isinstance=isinstance, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
269 |
list=list, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
270 |
long=long, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
271 |
str=str, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
272 |
tuple=tuple, |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
273 |
): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
274 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
275 |
def _iterencode_list(lst, _current_indent_level): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
276 |
if not lst: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
277 |
yield '[]' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
278 |
return |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
279 |
if markers is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
280 |
markerid = id(lst) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
281 |
if markerid in markers: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
282 |
raise ValueError("Circular reference detected") |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
283 |
markers[markerid] = lst |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
284 |
buf = '[' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
285 |
if _indent is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
286 |
_current_indent_level += 1 |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
287 |
newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
288 |
separator = _item_separator + newline_indent |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
289 |
buf += newline_indent |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
290 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
291 |
newline_indent = None |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
292 |
separator = _item_separator |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
293 |
first = True |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
294 |
for value in lst: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
295 |
if first: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
296 |
first = False |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
297 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
298 |
buf = separator |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
299 |
if isinstance(value, basestring): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
300 |
yield buf + _encoder(value) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
301 |
elif value is None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
302 |
yield buf + 'null' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
303 |
elif value is True: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
304 |
yield buf + 'true' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
305 |
elif value is False: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
306 |
yield buf + 'false' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
307 |
elif isinstance(value, (int, long)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
308 |
yield buf + str(value) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
309 |
elif isinstance(value, float): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
310 |
yield buf + _floatstr(value) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
311 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
312 |
yield buf |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
313 |
if isinstance(value, (list, tuple)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
314 |
chunks = _iterencode_list(value, _current_indent_level) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
315 |
elif isinstance(value, dict): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
316 |
chunks = _iterencode_dict(value, _current_indent_level) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
317 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
318 |
chunks = _iterencode(value, _current_indent_level) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
319 |
for chunk in chunks: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
320 |
yield chunk |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
321 |
if newline_indent is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
322 |
_current_indent_level -= 1 |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
323 |
yield '\n' + (' ' * (_indent * _current_indent_level)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
324 |
yield ']' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
325 |
if markers is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
326 |
del markers[markerid] |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
327 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
328 |
def _iterencode_dict(dct, _current_indent_level): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
329 |
if not dct: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
330 |
yield '{}' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
331 |
return |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
332 |
if markers is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
333 |
markerid = id(dct) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
334 |
if markerid in markers: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
335 |
raise ValueError("Circular reference detected") |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
336 |
markers[markerid] = dct |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
337 |
yield '{' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
338 |
if _indent is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
339 |
_current_indent_level += 1 |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
340 |
newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
341 |
item_separator = _item_separator + newline_indent |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
342 |
yield newline_indent |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
343 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
344 |
newline_indent = None |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
345 |
item_separator = _item_separator |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
346 |
first = True |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
347 |
if _sort_keys: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
348 |
items = dct.items() |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
349 |
items.sort(key=lambda kv: kv[0]) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
350 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
351 |
items = dct.iteritems() |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
352 |
for key, value in items: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
353 |
if isinstance(key, basestring): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
354 |
pass |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
355 |
# JavaScript is weakly typed for these, so it makes sense to |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
356 |
# also allow them. Many encoders seem to do something like this. |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
357 |
elif isinstance(key, float): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
358 |
key = _floatstr(key) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
359 |
elif isinstance(key, (int, long)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
360 |
key = str(key) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
361 |
elif key is True: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
362 |
key = 'true' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
363 |
elif key is False: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
364 |
key = 'false' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
365 |
elif key is None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
366 |
key = 'null' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
367 |
elif _skipkeys: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
368 |
continue |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
369 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
370 |
raise TypeError("key %r is not a string" % (key,)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
371 |
if first: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
372 |
first = False |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
373 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
374 |
yield item_separator |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
375 |
yield _encoder(key) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
376 |
yield _key_separator |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
377 |
if isinstance(value, basestring): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
378 |
yield _encoder(value) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
379 |
elif value is None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
380 |
yield 'null' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
381 |
elif value is True: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
382 |
yield 'true' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
383 |
elif value is False: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
384 |
yield 'false' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
385 |
elif isinstance(value, (int, long)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
386 |
yield str(value) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
387 |
elif isinstance(value, float): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
388 |
yield _floatstr(value) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
389 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
390 |
if isinstance(value, (list, tuple)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
391 |
chunks = _iterencode_list(value, _current_indent_level) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
392 |
elif isinstance(value, dict): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
393 |
chunks = _iterencode_dict(value, _current_indent_level) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
394 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
395 |
chunks = _iterencode(value, _current_indent_level) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
396 |
for chunk in chunks: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
397 |
yield chunk |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
398 |
if newline_indent is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
399 |
_current_indent_level -= 1 |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
400 |
yield '\n' + (' ' * (_indent * _current_indent_level)) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
401 |
yield '}' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
402 |
if markers is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
403 |
del markers[markerid] |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
404 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
405 |
def _iterencode(o, _current_indent_level): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
406 |
if isinstance(o, basestring): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
407 |
yield _encoder(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
408 |
elif o is None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
409 |
yield 'null' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
410 |
elif o is True: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
411 |
yield 'true' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
412 |
elif o is False: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
413 |
yield 'false' |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
414 |
elif isinstance(o, (int, long)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
415 |
yield str(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
416 |
elif isinstance(o, float): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
417 |
yield _floatstr(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
418 |
elif isinstance(o, (list, tuple)): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
419 |
for chunk in _iterencode_list(o, _current_indent_level): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
420 |
yield chunk |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
421 |
elif isinstance(o, dict): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
422 |
for chunk in _iterencode_dict(o, _current_indent_level): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
423 |
yield chunk |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
424 |
else: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
425 |
if markers is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
426 |
markerid = id(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
427 |
if markerid in markers: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
428 |
raise ValueError("Circular reference detected") |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
429 |
markers[markerid] = o |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
430 |
o = _default(o) |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
431 |
for chunk in _iterencode(o, _current_indent_level): |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
432 |
yield chunk |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
433 |
if markers is not None: |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
434 |
del markers[markerid] |
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
435 |
|
295d67509412
Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
436 |
return _iterencode |