1 from unittest import TestCase
3 import simplejson as json
5 class TestUnicode(TestCase):
6 def test_encoding1(self):
7 encoder = json.JSONEncoder(encoding='utf-8')
8 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
10 ju = encoder.encode(u)
11 js = encoder.encode(s)
12 self.assertEquals(ju, js)
14 def test_encoding2(self):
15 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
17 ju = json.dumps(u, encoding='utf-8')
18 js = json.dumps(s, encoding='utf-8')
19 self.assertEquals(ju, js)
21 def test_encoding3(self):
22 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
24 self.assertEquals(j, '"\\u03b1\\u03a9"')
26 def test_encoding4(self):
27 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
29 self.assertEquals(j, '["\\u03b1\\u03a9"]')
31 def test_encoding5(self):
32 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
33 j = json.dumps(u, ensure_ascii=False)
34 self.assertEquals(j, u'"%s"' % (u,))
36 def test_encoding6(self):
37 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
38 j = json.dumps([u], ensure_ascii=False)
39 self.assertEquals(j, u'["%s"]' % (u,))
41 def test_big_unicode_encode(self):
43 self.assertEquals(json.dumps(u), '"\\ud834\\udd20"')
44 self.assertEquals(json.dumps(u, ensure_ascii=False), u'"\U0001d120"')
46 def test_big_unicode_decode(self):
48 self.assertEquals(json.loads('"' + u + '"'), u)
49 self.assertEquals(json.loads('"z\\ud834\\udd20x"'), u)
51 def test_unicode_decode(self):
52 for i in range(0, 0xd7ff):
54 s = '"\\u%04x"' % (i,)
55 self.assertEquals(json.loads(s), u)
57 def test_default_encoding(self):
58 self.assertEquals(json.loads(u'{"a": "\xe9"}'.encode('utf-8')),
61 def test_unicode_preservation(self):
62 self.assertEquals(type(json.loads(u'""')), unicode)
63 self.assertEquals(type(json.loads(u'"a"')), unicode)
64 self.assertEquals(type(json.loads(u'["a"]')[0]), unicode)