2008-05-08 11:29:10 -03:00
|
|
|
import decimal
|
2017-03-27 11:05:26 -03:00
|
|
|
from io import StringIO
|
2009-04-21 00:09:17 -03:00
|
|
|
from collections import OrderedDict
|
2013-08-08 09:03:45 -03:00
|
|
|
from test.test_json import PyTest, CTest
|
2022-09-02 13:35:08 -03:00
|
|
|
from test import support
|
2008-05-08 11:29:10 -03:00
|
|
|
|
2010-09-04 17:16:53 -03:00
|
|
|
|
2011-05-14 00:47:51 -03:00
|
|
|
class TestDecode:
|
2008-05-08 11:29:10 -03:00
|
|
|
def test_decimal(self):
|
2011-05-14 00:47:51 -03:00
|
|
|
rval = self.loads('1.1', parse_float=decimal.Decimal)
|
2024-04-14 09:11:06 -03:00
|
|
|
self.assertIsInstance(rval, decimal.Decimal)
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(rval, decimal.Decimal('1.1'))
|
2008-05-08 11:29:10 -03:00
|
|
|
|
|
|
|
def test_float(self):
|
2011-05-14 00:47:51 -03:00
|
|
|
rval = self.loads('1', parse_int=float)
|
2024-04-14 09:11:06 -03:00
|
|
|
self.assertIsInstance(rval, float)
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(rval, 1.0)
|
2009-04-21 00:09:17 -03:00
|
|
|
|
2024-04-14 09:11:06 -03:00
|
|
|
def test_bytes(self):
|
|
|
|
self.assertEqual(self.loads(b"1"), 1)
|
|
|
|
|
|
|
|
def test_parse_constant(self):
|
|
|
|
for constant, expected in [
|
|
|
|
("Infinity", "INFINITY"),
|
|
|
|
("-Infinity", "-INFINITY"),
|
|
|
|
("NaN", "NAN"),
|
|
|
|
]:
|
|
|
|
self.assertEqual(
|
|
|
|
self.loads(constant, parse_constant=str.upper), expected
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_constant_invalid_case(self):
|
|
|
|
for constant in [
|
|
|
|
"nan", "NAN", "naN", "infinity", "INFINITY", "inFiniTy"
|
|
|
|
]:
|
|
|
|
with self.assertRaises(self.JSONDecodeError):
|
|
|
|
self.loads(constant)
|
|
|
|
|
2011-04-13 01:10:13 -03:00
|
|
|
def test_empty_objects(self):
|
2011-05-14 00:47:51 -03:00
|
|
|
self.assertEqual(self.loads('{}'), {})
|
|
|
|
self.assertEqual(self.loads('[]'), [])
|
|
|
|
self.assertEqual(self.loads('""'), "")
|
2011-04-13 01:10:13 -03:00
|
|
|
|
2009-04-21 00:09:17 -03:00
|
|
|
def test_object_pairs_hook(self):
|
|
|
|
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
|
|
|
|
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
|
|
|
|
("qrt", 5), ("pad", 6), ("hoy", 7)]
|
2011-05-14 00:47:51 -03:00
|
|
|
self.assertEqual(self.loads(s), eval(s))
|
2013-03-12 20:52:34 -03:00
|
|
|
self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
|
2011-05-14 00:47:51 -03:00
|
|
|
self.assertEqual(self.json.load(StringIO(s),
|
|
|
|
object_pairs_hook=lambda x: x), p)
|
2013-03-12 20:52:34 -03:00
|
|
|
od = self.loads(s, object_pairs_hook=OrderedDict)
|
2009-04-21 00:09:17 -03:00
|
|
|
self.assertEqual(od, OrderedDict(p))
|
|
|
|
self.assertEqual(type(od), OrderedDict)
|
|
|
|
# the object_pairs_hook takes priority over the object_hook
|
2013-03-12 20:52:34 -03:00
|
|
|
self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict,
|
|
|
|
object_hook=lambda x: None),
|
2009-04-21 00:09:17 -03:00
|
|
|
OrderedDict(p))
|
2016-09-07 08:04:41 -03:00
|
|
|
# check that empty object literals work (see #17368)
|
2013-03-12 20:52:34 -03:00
|
|
|
self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict),
|
|
|
|
OrderedDict())
|
|
|
|
self.assertEqual(self.loads('{"empty": {}}',
|
|
|
|
object_pairs_hook=OrderedDict),
|
|
|
|
OrderedDict([('empty', OrderedDict())]))
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
def test_decoder_optimizations(self):
|
|
|
|
# Several optimizations were made that skip over calls to
|
|
|
|
# the whitespace regex, so this test is designed to try and
|
|
|
|
# exercise the uncommon cases. The array cases are already covered.
|
2011-05-14 00:47:51 -03:00
|
|
|
rval = self.loads('{ "key" : "value" , "k":"v" }')
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(rval, {"key":"value", "k":"v"})
|
2010-09-04 17:16:53 -03:00
|
|
|
|
|
|
|
def check_keys_reuse(self, source, loads):
|
|
|
|
rval = loads(source)
|
|
|
|
(a, b), (c, d) = sorted(rval[0]), sorted(rval[1])
|
|
|
|
self.assertIs(a, c)
|
|
|
|
self.assertIs(b, d)
|
|
|
|
|
|
|
|
def test_keys_reuse(self):
|
|
|
|
s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'
|
2011-05-14 00:47:51 -03:00
|
|
|
self.check_keys_reuse(s, self.loads)
|
2018-05-22 08:55:07 -03:00
|
|
|
decoder = self.json.decoder.JSONDecoder()
|
|
|
|
self.check_keys_reuse(s, decoder.decode)
|
|
|
|
self.assertFalse(decoder.memo)
|
2011-05-14 00:47:51 -03:00
|
|
|
|
2012-08-18 15:46:23 -03:00
|
|
|
def test_extra_data(self):
|
|
|
|
s = '[1, 2, 3]5'
|
|
|
|
msg = 'Extra data'
|
2015-01-26 07:16:30 -04:00
|
|
|
self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
|
2012-08-18 15:46:23 -03:00
|
|
|
|
|
|
|
def test_invalid_escape(self):
|
|
|
|
s = '["abc\\y"]'
|
|
|
|
msg = 'escape'
|
2015-01-26 07:16:30 -04:00
|
|
|
self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
|
2011-05-14 00:47:51 -03:00
|
|
|
|
2013-10-20 19:52:33 -03:00
|
|
|
def test_invalid_input_type(self):
|
|
|
|
msg = 'the JSON object must be str'
|
2016-09-10 07:16:18 -03:00
|
|
|
for value in [1, 3.14, [], {}, None]:
|
2013-10-20 19:52:33 -03:00
|
|
|
self.assertRaisesRegex(TypeError, msg, self.loads, value)
|
|
|
|
|
2013-10-20 20:10:55 -03:00
|
|
|
def test_string_with_utf8_bom(self):
|
|
|
|
# see #18958
|
|
|
|
bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
|
2015-01-26 07:16:30 -04:00
|
|
|
with self.assertRaises(self.JSONDecodeError) as cm:
|
2013-10-20 20:10:55 -03:00
|
|
|
self.loads(bom_json)
|
|
|
|
self.assertIn('BOM', str(cm.exception))
|
2015-01-26 07:16:30 -04:00
|
|
|
with self.assertRaises(self.JSONDecodeError) as cm:
|
2013-10-20 20:10:55 -03:00
|
|
|
self.json.load(StringIO(bom_json))
|
|
|
|
self.assertIn('BOM', str(cm.exception))
|
|
|
|
# make sure that the BOM is not detected in the middle of a string
|
2024-04-14 09:11:06 -03:00
|
|
|
bom = ''.encode('utf-8-sig').decode('utf-8')
|
|
|
|
bom_in_str = f'"{bom}"'
|
2013-10-20 20:10:55 -03:00
|
|
|
self.assertEqual(self.loads(bom_in_str), '\ufeff')
|
|
|
|
self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
|
|
|
|
|
2014-04-13 23:10:38 -03:00
|
|
|
def test_negative_index(self):
|
|
|
|
d = self.json.JSONDecoder()
|
|
|
|
self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
|
|
|
|
|
2022-09-02 13:35:08 -03:00
|
|
|
def test_limit_int(self):
|
|
|
|
maxdigits = 5000
|
|
|
|
with support.adjust_int_max_str_digits(maxdigits):
|
|
|
|
self.loads('1' * maxdigits)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.loads('1' * (maxdigits + 1))
|
|
|
|
|
|
|
|
|
2011-05-14 00:47:51 -03:00
|
|
|
class TestPyDecode(TestDecode, PyTest): pass
|
|
|
|
class TestCDecode(TestDecode, CTest): pass
|