diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-04 20:16:53 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-04 20:16:53 (GMT) |
commit | 7d6e076f6d8dd48cfd748b02dad17dbeb0b346a3 (patch) | |
tree | 3d355bd888b6e2aaba52f33807d183055d61b700 /Lib/json/tests | |
parent | d9107aadeda663173eb68c412396e5e3353a4059 (diff) | |
download | cpython-7d6e076f6d8dd48cfd748b02dad17dbeb0b346a3.zip cpython-7d6e076f6d8dd48cfd748b02dad17dbeb0b346a3.tar.gz cpython-7d6e076f6d8dd48cfd748b02dad17dbeb0b346a3.tar.bz2 |
Issue #7451: Improve decoding performance of JSON objects, and reduce
the memory consumption of said decoded objects when they use the same
strings as keys.
Diffstat (limited to 'Lib/json/tests')
-rw-r--r-- | Lib/json/tests/test_decode.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/json/tests/test_decode.py b/Lib/json/tests/test_decode.py index 4610c6c..beb82a7 100644 --- a/Lib/json/tests/test_decode.py +++ b/Lib/json/tests/test_decode.py @@ -1,10 +1,25 @@ import decimal from unittest import TestCase from io import StringIO +from contextlib import contextmanager import json +import json.decoder +import json.scanner from collections import OrderedDict + +@contextmanager +def use_python_scanner(): + py_scanner = json.scanner.py_make_scanner + old_scanner = json.decoder.make_scanner + json.decoder.make_scanner = py_scanner + try: + yield + finally: + json.decoder.make_scanner = old_scanner + + class TestDecode(TestCase): def test_decimal(self): rval = json.loads('1.1', parse_float=decimal.Decimal) @@ -39,3 +54,16 @@ class TestDecode(TestCase): # exercise the uncommon cases. The array cases are already covered. rval = json.loads('{ "key" : "value" , "k":"v" }') self.assertEquals(rval, {"key":"value", "k":"v"}) + + 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}]' + self.check_keys_reuse(s, json.loads) + # Disabled: the pure Python version of json simply doesn't work + with use_python_scanner(): + self.check_keys_reuse(s, json.decoder.JSONDecoder().decode) |