Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4998a7

Browse files
committed
Close #18264: int- and float-derived enums now converted to int or float.
1 parent fbcf4d7 commit a4998a7

4 files changed

Lines changed: 178 additions & 32 deletions

File tree

Doc/library/json.rst

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -349,23 +349,26 @@ Encoders and Decoders
349349

350350
.. _py-to-json-table:
351351

352-
+-------------------+---------------+
353-
| Python | JSON |
354-
+===================+===============+
355-
| dict | object |
356-
+-------------------+---------------+
357-
| list, tuple | array |
358-
+-------------------+---------------+
359-
| str | string |
360-
+-------------------+---------------+
361-
| int, float | number |
362-
+-------------------+---------------+
363-
| True | true |
364-
+-------------------+---------------+
365-
| False | false |
366-
+-------------------+---------------+
367-
| None | null |
368-
+-------------------+---------------+
352+
+----------------------------------------+---------------+
353+
| Python | JSON |
354+
+========================================+===============+
355+
| dict | object |
356+
+----------------------------------------+---------------+
357+
| list, tuple | array |
358+
+----------------------------------------+---------------+
359+
| str | string |
360+
+----------------------------------------+---------------+
361+
| int, float, int- & float-derived Enums | number |
362+
+----------------------------------------+---------------+
363+
| True | true |
364+
+----------------------------------------+---------------+
365+
| False | false |
366+
+----------------------------------------+---------------+
367+
| None | null |
368+
+----------------------------------------+---------------+
369+
370+
.. versionchanged:: 3.4
371+
Added support for int- and float-derived Enum classes.
369372

370373
To extend this to recognize other objects, subclass and implement a
371374
:meth:`default` method with another method that returns a serializable object

Lib/json/encoder.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def default(self, o):
175175
def encode(self, o):
176176
"""Return a JSON string representation of a Python data structure.
177177
178+
>>> from json.encoder import JSONEncoder
178179
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
179180
'{"foo": ["bar", "baz"]}'
180181
@@ -298,9 +299,13 @@ def _iterencode_list(lst, _current_indent_level):
298299
elif value is False:
299300
yield buf + 'false'
300301
elif isinstance(value, int):
301-
yield buf + str(value)
302+
# Subclasses of int/float may override __str__, but we still
303+
# want to encode them as integers/floats in JSON. One example
304+
# within the standard library is IntEnum.
305+
yield buf + str(int(value))
302306
elif isinstance(value, float):
303-
yield buf + _floatstr(value)
307+
# see comment above for int
308+
yield buf + _floatstr(float(value))
304309
else:
305310
yield buf
306311
if isinstance(value, (list, tuple)):
@@ -346,15 +351,17 @@ def _iterencode_dict(dct, _current_indent_level):
346351
# JavaScript is weakly typed for these, so it makes sense to
347352
# also allow them. Many encoders seem to do something like this.
348353
elif isinstance(key, float):
349-
key = _floatstr(key)
354+
# see comment for int/float in _make_iterencode
355+
key = _floatstr(float(key))
350356
elif key is True:
351357
key = 'true'
352358
elif key is False:
353359
key = 'false'
354360
elif key is None:
355361
key = 'null'
356362
elif isinstance(key, int):
357-
key = str(key)
363+
# see comment for int/float in _make_iterencode
364+
key = str(int(key))
358365
elif _skipkeys:
359366
continue
360367
else:
@@ -374,9 +381,11 @@ def _iterencode_dict(dct, _current_indent_level):
374381
elif value is False:
375382
yield 'false'
376383
elif isinstance(value, int):
377-
yield str(value)
384+
# see comment for int/float in _make_iterencode
385+
yield str(int(value))
378386
elif isinstance(value, float):
379-
yield _floatstr(value)
387+
# see comment for int/float in _make_iterencode
388+
yield _floatstr(float(value))
380389
else:
381390
if isinstance(value, (list, tuple)):
382391
chunks = _iterencode_list(value, _current_indent_level)
@@ -402,9 +411,11 @@ def _iterencode(o, _current_indent_level):
402411
elif o is False:
403412
yield 'false'
404413
elif isinstance(o, int):
405-
yield str(o)
414+
# see comment for int/float in _make_iterencode
415+
yield str(int(o))
406416
elif isinstance(o, float):
407-
yield _floatstr(o)
417+
# see comment for int/float in _make_iterencode
418+
yield _floatstr(float(o))
408419
elif isinstance(o, (list, tuple)):
409420
yield from _iterencode_list(o, _current_indent_level)
410421
elif isinstance(o, dict):

Lib/test/test_json/test_enum.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from enum import Enum, IntEnum
2+
from test.test_json import PyTest, CTest
3+
4+
SMALL = 1
5+
BIG = 1<<32
6+
HUGE = 1<<64
7+
REALLY_HUGE = 1<<96
8+
9+
class BigNum(IntEnum):
10+
small = SMALL
11+
big = BIG
12+
huge = HUGE
13+
really_huge = REALLY_HUGE
14+
15+
E = 2.718281
16+
PI = 3.141593
17+
TAU = 2 * PI
18+
19+
class FloatNum(float, Enum):
20+
e = E
21+
pi = PI
22+
tau = TAU
23+
24+
class TestEnum:
25+
26+
def test_floats(self):
27+
for enum in FloatNum:
28+
self.assertEqual(self.dumps(enum), repr(enum.value))
29+
self.assertEqual(float(self.dumps(enum)), enum)
30+
self.assertEqual(self.loads(self.dumps(enum)), enum)
31+
32+
def test_ints(self):
33+
for enum in BigNum:
34+
self.assertEqual(self.dumps(enum), str(enum.value))
35+
self.assertEqual(int(self.dumps(enum)), enum)
36+
self.assertEqual(self.loads(self.dumps(enum)), enum)
37+
38+
def test_list(self):
39+
self.assertEqual(
40+
self.dumps(list(BigNum)),
41+
str([SMALL, BIG, HUGE, REALLY_HUGE]),
42+
)
43+
self.assertEqual(self.dumps(list(FloatNum)), str([E, PI, TAU]))
44+
45+
def test_dict_keys(self):
46+
s, b, h, r = BigNum
47+
e, p, t = FloatNum
48+
d = {
49+
s:'tiny', b:'large', h:'larger', r:'largest',
50+
e:"Euler's number", p:'pi', t:'tau',
51+
}
52+
nd = self.loads(self.dumps(d))
53+
self.assertEqual(nd[str(SMALL)], 'tiny')
54+
self.assertEqual(nd[str(BIG)], 'large')
55+
self.assertEqual(nd[str(HUGE)], 'larger')
56+
self.assertEqual(nd[str(REALLY_HUGE)], 'largest')
57+
self.assertEqual(nd[repr(E)], "Euler's number")
58+
self.assertEqual(nd[repr(PI)], 'pi')
59+
self.assertEqual(nd[repr(TAU)], 'tau')
60+
61+
def test_dict_values(self):
62+
d = dict(
63+
tiny=BigNum.small,
64+
large=BigNum.big,
65+
larger=BigNum.huge,
66+
largest=BigNum.really_huge,
67+
e=FloatNum.e,
68+
pi=FloatNum.pi,
69+
tau=FloatNum.tau,
70+
)
71+
nd = self.loads(self.dumps(d))
72+
self.assertEqual(nd['tiny'], SMALL)
73+
self.assertEqual(nd['large'], BIG)
74+
self.assertEqual(nd['larger'], HUGE)
75+
self.assertEqual(nd['largest'], REALLY_HUGE)
76+
self.assertEqual(nd['e'], E)
77+
self.assertEqual(nd['pi'], PI)
78+
self.assertEqual(nd['tau'], TAU)
79+
80+
class TestPyEnum(TestEnum, PyTest): pass
81+
class TestCEnum(TestEnum, CTest): pass

Modules/_json.c

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
116116
static PyObject *
117117
encoder_encode_string(PyEncoderObject *s, PyObject *obj);
118118
static PyObject *
119+
encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj);
120+
static PyObject *
119121
encoder_encode_float(PyEncoderObject *s, PyObject *obj);
120122

121123
#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
@@ -1301,14 +1303,46 @@ _encoded_const(PyObject *obj)
13011303
}
13021304
}
13031305

1306+
static PyObject *
1307+
encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj)
1308+
{
1309+
/* Return the JSON representation of a PyLong and PyLong subclasses.
1310+
Calls int() on PyLong subclasses in case the str() was changed.
1311+
Added specifically to deal with IntEnum. See Issue18264. */
1312+
PyObject *encoded, *longobj;
1313+
if (PyLong_CheckExact(obj)) {
1314+
encoded = PyObject_Str(obj);
1315+
}
1316+
else {
1317+
longobj = PyNumber_Long(obj);
1318+
if (longobj == NULL) {
1319+
PyErr_SetString(
1320+
PyExc_ValueError,
1321+
"Unable to coerce int subclass to int"
1322+
);
1323+
return NULL;
1324+
}
1325+
encoded = PyObject_Str(longobj);
1326+
Py_DECREF(longobj);
1327+
}
1328+
return encoded;
1329+
}
1330+
1331+
13041332
static PyObject *
13051333
encoder_encode_float(PyEncoderObject *s, PyObject *obj)
13061334
{
1307-
/* Return the JSON representation of a PyFloat */
1335+
/* Return the JSON representation of a PyFloat.
1336+
Modified to call float() on float subclasses in case the subclass
1337+
changes the repr. See Issue18264. */
1338+
PyObject *encoded, *floatobj;
13081339
double i = PyFloat_AS_DOUBLE(obj);
13091340
if (!Py_IS_FINITE(i)) {
13101341
if (!s->allow_nan) {
1311-
PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1342+
PyErr_SetString(
1343+
PyExc_ValueError,
1344+
"Out of range float values are not JSON compliant"
1345+
);
13121346
return NULL;
13131347
}
13141348
if (i > 0) {
@@ -1321,8 +1355,24 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
13211355
return PyUnicode_FromString("NaN");
13221356
}
13231357
}
1324-
/* Use a better float format here? */
1325-
return PyObject_Repr(obj);
1358+
/* coerce float subclasses to float (primarily for Enum) */
1359+
if (PyFloat_CheckExact(obj)) {
1360+
/* Use a better float format here? */
1361+
encoded = PyObject_Repr(obj);
1362+
}
1363+
else {
1364+
floatobj = PyNumber_Float(obj);
1365+
if (floatobj == NULL) {
1366+
PyErr_SetString(
1367+
PyExc_ValueError,
1368+
"Unable to coerce float subclass to float"
1369+
);
1370+
return NULL;
1371+
}
1372+
encoded = PyObject_Repr(floatobj);
1373+
Py_DECREF(floatobj);
1374+
}
1375+
return encoded;
13261376
}
13271377

13281378
static PyObject *
@@ -1366,7 +1416,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
13661416
return _steal_accumulate(acc, encoded);
13671417
}
13681418
else if (PyLong_Check(obj)) {
1369-
PyObject *encoded = PyObject_Str(obj);
1419+
PyObject *encoded = encoder_encode_long(s, obj);
13701420
if (encoded == NULL)
13711421
return -1;
13721422
return _steal_accumulate(acc, encoded);
@@ -1551,9 +1601,10 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
15511601
goto bail;
15521602
}
15531603
else if (PyLong_Check(key)) {
1554-
kstr = PyObject_Str(key);
1555-
if (kstr == NULL)
1604+
kstr = encoder_encode_long(s, key);
1605+
if (kstr == NULL) {
15561606
goto bail;
1607+
}
15571608
}
15581609
else if (skipkeys) {
15591610
Py_DECREF(item);

0 commit comments

Comments
 (0)