| 
									
										
										
										
											2008-05-08 14:29:10 +00:00
										 |  |  | import decimal | 
					
						
							|  |  |  | from unittest import TestCase | 
					
						
							| 
									
										
										
										
											2009-04-21 03:09:17 +00:00
										 |  |  | from io import StringIO | 
					
						
							| 
									
										
										
										
											2010-09-04 20:16:53 +00:00
										 |  |  | from contextlib import contextmanager | 
					
						
							| 
									
										
										
										
											2008-05-08 14:29:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import json | 
					
						
							| 
									
										
										
										
											2010-09-04 20:16:53 +00:00
										 |  |  | import json.decoder | 
					
						
							|  |  |  | import json.scanner | 
					
						
							| 
									
										
										
										
											2009-04-21 03:09:17 +00:00
										 |  |  | from collections import OrderedDict | 
					
						
							| 
									
										
										
										
											2008-05-08 14:29:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 20:16:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | @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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-08 14:29:10 +00:00
										 |  |  | class TestDecode(TestCase): | 
					
						
							|  |  |  |     def test_decimal(self): | 
					
						
							|  |  |  |         rval = json.loads('1.1', parse_float=decimal.Decimal) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(isinstance(rval, decimal.Decimal)) | 
					
						
							| 
									
										
										
										
											2008-05-08 14:29:10 +00:00
										 |  |  |         self.assertEquals(rval, decimal.Decimal('1.1')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_float(self): | 
					
						
							|  |  |  |         rval = json.loads('1', parse_int=float) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(isinstance(rval, float)) | 
					
						
							| 
									
										
										
										
											2008-05-08 14:29:10 +00:00
										 |  |  |         self.assertEquals(rval, 1.0) | 
					
						
							| 
									
										
										
										
											2009-04-21 03:09:17 +00: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)] | 
					
						
							|  |  |  |         self.assertEqual(json.loads(s), eval(s)) | 
					
						
							|  |  |  |         self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p) | 
					
						
							|  |  |  |         self.assertEqual(json.load(StringIO(s), | 
					
						
							|  |  |  |                                    object_pairs_hook=lambda x: x), p) | 
					
						
							|  |  |  |         od = json.loads(s, object_pairs_hook = OrderedDict) | 
					
						
							|  |  |  |         self.assertEqual(od, OrderedDict(p)) | 
					
						
							|  |  |  |         self.assertEqual(type(od), OrderedDict) | 
					
						
							|  |  |  |         # the object_pairs_hook takes priority over the object_hook | 
					
						
							|  |  |  |         self.assertEqual(json.loads(s, | 
					
						
							|  |  |  |                                     object_pairs_hook = OrderedDict, | 
					
						
							|  |  |  |                                     object_hook = lambda x: None), | 
					
						
							|  |  |  |                          OrderedDict(p)) | 
					
						
							| 
									
										
										
										
											2009-05-02 12:36:44 +00: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. | 
					
						
							|  |  |  |         rval = json.loads('{   "key"    :    "value"    ,  "k":"v"    }') | 
					
						
							|  |  |  |         self.assertEquals(rval, {"key":"value", "k":"v"}) | 
					
						
							| 
									
										
										
										
											2010-09-04 20:16:53 +00: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}]' | 
					
						
							|  |  |  |         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) |