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