annotate Lib/json/encoder.py @ 79675:c0a2294de787 2.7

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