• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from collections import OrderedDict
2from json.tests import PyTest, CTest
3
4
5class TestUnicode(object):
6    def test_encoding1(self):
7        encoder = self.json.JSONEncoder(encoding='utf-8')
8        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
9        s = u.encode('utf-8')
10        ju = encoder.encode(u)
11        js = encoder.encode(s)
12        self.assertEqual(ju, js)
13
14    def test_encoding2(self):
15        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
16        s = u.encode('utf-8')
17        ju = self.dumps(u, encoding='utf-8')
18        js = self.dumps(s, encoding='utf-8')
19        self.assertEqual(ju, js)
20
21    def test_encoding3(self):
22        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
23        j = self.dumps(u)
24        self.assertEqual(j, '"\\u03b1\\u03a9"')
25
26    def test_encoding4(self):
27        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
28        j = self.dumps([u])
29        self.assertEqual(j, '["\\u03b1\\u03a9"]')
30
31    def test_encoding5(self):
32        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
33        j = self.dumps(u, ensure_ascii=False)
34        self.assertEqual(j, u'"{0}"'.format(u))
35
36    def test_encoding6(self):
37        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
38        j = self.dumps([u], ensure_ascii=False)
39        self.assertEqual(j, u'["{0}"]'.format(u))
40
41    def test_big_unicode_encode(self):
42        u = u'\U0001d120'
43        self.assertEqual(self.dumps(u), '"\\ud834\\udd20"')
44        self.assertEqual(self.dumps(u, ensure_ascii=False), u'"\U0001d120"')
45
46    def test_big_unicode_decode(self):
47        u = u'z\U0001d120x'
48        self.assertEqual(self.loads('"' + u + '"'), u)
49        self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)
50
51    def test_unicode_decode(self):
52        for i in range(0, 0xd7ff):
53            u = unichr(i)
54            s = '"\\u{0:04x}"'.format(i)
55            self.assertEqual(self.loads(s), u)
56
57    def test_object_pairs_hook_with_unicode(self):
58        s = u'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
59        p = [(u"xkd", 1), (u"kcw", 2), (u"art", 3), (u"hxm", 4),
60             (u"qrt", 5), (u"pad", 6), (u"hoy", 7)]
61        self.assertEqual(self.loads(s), eval(s))
62        self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)
63        od = self.loads(s, object_pairs_hook = OrderedDict)
64        self.assertEqual(od, OrderedDict(p))
65        self.assertEqual(type(od), OrderedDict)
66        # the object_pairs_hook takes priority over the object_hook
67        self.assertEqual(self.loads(s,
68                                    object_pairs_hook = OrderedDict,
69                                    object_hook = lambda x: None),
70                         OrderedDict(p))
71
72    def test_default_encoding(self):
73        self.assertEqual(self.loads(u'{"a": "\xe9"}'.encode('utf-8')),
74            {'a': u'\xe9'})
75
76    def test_unicode_preservation(self):
77        self.assertEqual(type(self.loads(u'""')), unicode)
78        self.assertEqual(type(self.loads(u'"a"')), unicode)
79        self.assertEqual(type(self.loads(u'["a"]')[0]), unicode)
80        # Issue 10038.
81        self.assertEqual(type(self.loads('"foo"')), unicode)
82
83    def test_bad_encoding(self):
84        self.assertRaises(UnicodeEncodeError, self.loads, '"a"', u"rat\xe9")
85        self.assertRaises(TypeError, self.loads, '"a"', 1)
86
87
88class TestPyUnicode(TestUnicode, PyTest): pass
89class TestCUnicode(TestUnicode, CTest): pass
90