Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | from unittest import TestCase |
| 2 | |
| 3 | import json |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 4 | from collections import OrderedDict |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 5 | |
| 6 | class TestUnicode(TestCase): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 7 | # test_encoding1 and test_encoding2 from 2.x are irrelevant (only str |
| 8 | # is supported as input, not bytes). |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 9 | |
| 10 | def test_encoding3(self): |
| 11 | u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
| 12 | j = json.dumps(u) |
| 13 | self.assertEquals(j, '"\\u03b1\\u03a9"') |
| 14 | |
| 15 | def test_encoding4(self): |
| 16 | u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
| 17 | j = json.dumps([u]) |
| 18 | self.assertEquals(j, '["\\u03b1\\u03a9"]') |
| 19 | |
| 20 | def test_encoding5(self): |
| 21 | u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
| 22 | j = json.dumps(u, ensure_ascii=False) |
| 23 | self.assertEquals(j, '"{0}"'.format(u)) |
| 24 | |
| 25 | def test_encoding6(self): |
| 26 | u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
| 27 | j = json.dumps([u], ensure_ascii=False) |
| 28 | self.assertEquals(j, '["{0}"]'.format(u)) |
| 29 | |
| 30 | def test_big_unicode_encode(self): |
| 31 | u = '\U0001d120' |
| 32 | self.assertEquals(json.dumps(u), '"\\ud834\\udd20"') |
| 33 | self.assertEquals(json.dumps(u, ensure_ascii=False), '"\U0001d120"') |
| 34 | |
| 35 | def test_big_unicode_decode(self): |
| 36 | u = 'z\U0001d120x' |
| 37 | self.assertEquals(json.loads('"' + u + '"'), u) |
| 38 | self.assertEquals(json.loads('"z\\ud834\\udd20x"'), u) |
| 39 | |
| 40 | def test_unicode_decode(self): |
| 41 | for i in range(0, 0xd7ff): |
| 42 | u = chr(i) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 43 | s = '"\\u{0:04x}"'.format(i) |
| 44 | self.assertEquals(json.loads(s), u) |
| 45 | |
| 46 | def test_unicode_preservation(self): |
| 47 | self.assertEquals(type(json.loads('""')), str) |
| 48 | self.assertEquals(type(json.loads('"a"')), str) |
| 49 | self.assertEquals(type(json.loads('["a"]')[0]), str) |
| 50 | |
| 51 | def test_bytes_encode(self): |
| 52 | self.assertRaises(TypeError, json.dumps, b"hi") |
| 53 | self.assertRaises(TypeError, json.dumps, [b"hi"]) |
| 54 | |
| 55 | def test_bytes_decode(self): |
| 56 | self.assertRaises(TypeError, json.loads, b'"hi"') |
| 57 | self.assertRaises(TypeError, json.loads, b'["hi"]') |
| 58 | |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 59 | |
| 60 | def test_object_pairs_hook_with_unicode(self): |
| 61 | s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' |
| 62 | p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), |
| 63 | ("qrt", 5), ("pad", 6), ("hoy", 7)] |
| 64 | self.assertEqual(json.loads(s), eval(s)) |
| 65 | self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p) |
| 66 | od = json.loads(s, object_pairs_hook = OrderedDict) |
| 67 | self.assertEqual(od, OrderedDict(p)) |
| 68 | self.assertEqual(type(od), OrderedDict) |
| 69 | # the object_pairs_hook takes priority over the object_hook |
| 70 | self.assertEqual(json.loads(s, |
| 71 | object_pairs_hook = OrderedDict, |
| 72 | object_hook = lambda x: None), |
| 73 | OrderedDict(p)) |