Skip to content

Commit 48e9b40

Browse files
committed
json: support frozendict
1 parent 33c8c55 commit 48e9b40

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

Lib/json/encoder.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class JSONEncoder(object):
8282
+-------------------+---------------+
8383
| Python | JSON |
8484
+===================+===============+
85-
| dict | object |
85+
| dict, frozendict | object |
8686
+-------------------+---------------+
8787
| list, tuple | array |
8888
+-------------------+---------------+
@@ -268,6 +268,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
268268
## HACK: hand-optimized bytecode; turn globals into locals
269269
ValueError=ValueError,
270270
dict=dict,
271+
frozendict=frozendict,
271272
float=float,
272273
id=id,
273274
int=int,
@@ -320,7 +321,7 @@ def _iterencode_list(lst, _current_indent_level):
320321
yield buf
321322
if isinstance(value, (list, tuple)):
322323
chunks = _iterencode_list(value, _current_indent_level)
323-
elif isinstance(value, dict):
324+
elif isinstance(value, (dict, frozendict)):
324325
chunks = _iterencode_dict(value, _current_indent_level)
325326
else:
326327
chunks = _iterencode(value, _current_indent_level)
@@ -407,7 +408,7 @@ def _iterencode_dict(dct, _current_indent_level):
407408
else:
408409
if isinstance(value, (list, tuple)):
409410
chunks = _iterencode_list(value, _current_indent_level)
410-
elif isinstance(value, dict):
411+
elif isinstance(value, (dict, frozendict)):
411412
chunks = _iterencode_dict(value, _current_indent_level)
412413
else:
413414
chunks = _iterencode(value, _current_indent_level)
@@ -441,7 +442,7 @@ def _iterencode(o, _current_indent_level):
441442
yield _floatstr(o)
442443
elif isinstance(o, (list, tuple)):
443444
yield from _iterencode_list(o, _current_indent_level)
444-
elif isinstance(o, dict):
445+
elif isinstance(o, (dict, frozendict)):
445446
yield from _iterencode_dict(o, _current_indent_level)
446447
else:
447448
if markers is not None:

Modules/_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
15971597
_Py_LeaveRecursiveCall();
15981598
return rv;
15991599
}
1600-
else if (PyDict_Check(obj)) {
1600+
else if (PyDict_Check(obj) || PyFrozenDict_Check(obj)) {
16011601
if (_Py_EnterRecursiveCall(" while encoding a JSON object"))
16021602
return -1;
16031603
rv = encoder_listencode_dict(s, writer, obj, indent_level, indent_cache);

0 commit comments

Comments
 (0)