1 from unittest
import TestCase
4 from collections
import OrderedDict
6 class TestUnicode(TestCase
):
7 def test_encoding1(self
):
8 encoder
= json
.JSONEncoder(encoding
='utf-8')
9 u
= u
'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
11 ju
= encoder
.encode(u
)
12 js
= encoder
.encode(s
)
13 self
.assertEquals(ju
, js
)
15 def test_encoding2(self
):
16 u
= u
'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
18 ju
= json
.dumps(u
, encoding
='utf-8')
19 js
= json
.dumps(s
, encoding
='utf-8')
20 self
.assertEquals(ju
, js
)
22 def test_encoding3(self
):
23 u
= u
'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
25 self
.assertEquals(j
, '"\\u03b1\\u03a9"')
27 def test_encoding4(self
):
28 u
= u
'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
30 self
.assertEquals(j
, '["\\u03b1\\u03a9"]')
32 def test_encoding5(self
):
33 u
= u
'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
34 j
= json
.dumps(u
, ensure_ascii
=False)
35 self
.assertEquals(j
, u
'"{0}"'.format(u
))
37 def test_encoding6(self
):
38 u
= u
'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
39 j
= json
.dumps([u
], ensure_ascii
=False)
40 self
.assertEquals(j
, u
'["{0}"]'.format(u
))
42 def test_big_unicode_encode(self
):
44 self
.assertEquals(json
.dumps(u
), '"\\ud834\\udd20"')
45 self
.assertEquals(json
.dumps(u
, ensure_ascii
=False), u
'"\U0001d120"')
47 def test_big_unicode_decode(self
):
49 self
.assertEquals(json
.loads('"' + u
+ '"'), u
)
50 self
.assertEquals(json
.loads('"z\\ud834\\udd20x"'), u
)
52 def test_unicode_decode(self
):
53 for i
in range(0, 0xd7ff):
55 s
= '"\\u{0:04x}"'.format(i
)
56 self
.assertEquals(json
.loads(s
), u
)
58 def test_object_pairs_hook_with_unicode(self
):
59 s
= u
'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
60 p
= [(u
"xkd", 1), (u
"kcw", 2), (u
"art", 3), (u
"hxm", 4),
61 (u
"qrt", 5), (u
"pad", 6), (u
"hoy", 7)]
62 self
.assertEqual(json
.loads(s
), eval(s
))
63 self
.assertEqual(json
.loads(s
, object_pairs_hook
= lambda x
: x
), p
)
64 od
= json
.loads(s
, object_pairs_hook
= OrderedDict
)
65 self
.assertEqual(od
, OrderedDict(p
))
66 self
.assertEqual(type(od
), OrderedDict
)
67 # the object_pairs_hook takes priority over the object_hook
68 self
.assertEqual(json
.loads(s
,
69 object_pairs_hook
= OrderedDict
,
70 object_hook
= lambda x
: None),
73 def test_default_encoding(self
):
74 self
.assertEquals(json
.loads(u
'{"a": "\xe9"}'.encode('utf-8')),
77 def test_unicode_preservation(self
):
78 self
.assertEquals(type(json
.loads(u
'""')), unicode)
79 self
.assertEquals(type(json
.loads(u
'"a"')), unicode)
80 self
.assertEquals(type(json
.loads(u
'["a"]')[0]), unicode)