def test_stringify_key(self): items = [(b('bytes'), 'bytes'), (1.0, '1.0'), (10, '10'), (True, 'true'), (False, 'false'), (None, 'null'), (long_type(100), '100')] for k, expect in items: self.assertEqual( json.loads(json.dumpsJSON({k: expect})), {expect: expect}) self.assertEqual( json.loads(json.dumpsJSON({k: expect}, sort_keys=True)), {expect: expect}) self.assertRaises(TypeError, json.dumpsJSON, {json: 1}) for v in [{}, {'other': 1}, {b('derp'): 1, 'herp': 2}]: for sort_keys in [False, True]: v0 = dict(v) v0[json] = 1 v1 = dict((as_text_type(key), val) for (key, val) in v.items()) self.assertEqual( json.loads(json.dumpsJSON(v0, skipkeys=True, sort_keys=sort_keys)), v1) self.assertEqual( json.loads(json.dumpsJSON({'': v0}, skipkeys=True, sort_keys=sort_keys)), {'': v1}) self.assertEqual( json.loads(json.dumpsJSON([v0], skipkeys=True, sort_keys=sort_keys)), [v1])
def test_stringify_key(self): items = [(b('bytes'), 'bytes'), (1.0, '1.0'), (10, '10'), (True, 'true'), (False, 'false'), (None, 'null'), (long_type(100), '100')] for k, expect in items: self.assertEqual(json.loads(json.dumpsJSON({k: expect})), {expect: expect}) self.assertEqual( json.loads(json.dumpsJSON({k: expect}, sort_keys=True)), {expect: expect}) self.assertRaises(TypeError, json.dumpsJSON, {json: 1}) for v in [{}, {'other': 1}, {b('derp'): 1, 'herp': 2}]: for sort_keys in [False, True]: v0 = dict(v) v0[json] = 1 v1 = dict((as_text_type(key), val) for (key, val) in v.items()) self.assertEqual( json.loads( json.dumpsJSON(v0, skipkeys=True, sort_keys=sort_keys)), v1) self.assertEqual( json.loads( json.dumpsJSON({'': v0}, skipkeys=True, sort_keys=sort_keys)), {'': v1}) self.assertEqual( json.loads( json.dumpsJSON([v0], skipkeys=True, sort_keys=sort_keys)), [v1])
def test_ensure_ascii_false_bytestring_encoding(self): # http://code.google.com/p/simplejson/issues/detail?id=48 doc1 = {u'quux': b('Arr\xc3\xaat sur images')} doc2 = {u'quux': u('Arr\xeat sur images')} doc_ascii = '{"quux": "Arr\\u00eat sur images"}' doc_unicode = u'{"quux": "Arr\xeat sur images"}' self.assertEqual(json.dumpsJSON(doc1), doc_ascii) self.assertEqual(json.dumpsJSON(doc2), doc_ascii) self.assertEqual(json.dumpsJSON(doc1, ensure_ascii=False), doc_unicode) self.assertEqual(json.dumpsJSON(doc2, ensure_ascii=False), doc_unicode)
def test_ensure_ascii_false_bytestring_encoding(self): # http://code.google.com/p/simplejson/issues/detail?id=48 doc1 = {u"quux": b("Arr\xc3\xaat sur images")} doc2 = {u"quux": u("Arr\xeat sur images")} doc_ascii = '{"quux": "Arr\\u00eat sur images"}' doc_unicode = u'{"quux": "Arr\xeat sur images"}' self.assertEqual(json.dumpsJSON(doc1), doc_ascii) self.assertEqual(json.dumpsJSON(doc2), doc_ascii) self.assertEqual(json.dumpsJSON(doc1, ensure_ascii=False), doc_unicode) self.assertEqual(json.dumpsJSON(doc2, ensure_ascii=False), doc_unicode)
def test_stringify_key(self): items = [ (b("bytes"), "bytes"), (1.0, "1.0"), (10, "10"), (True, "true"), (False, "false"), (None, "null"), (long_type(100), "100"), ] for k, expect in items: self.assertEqual(json.loads(json.dumpsJSON({k: expect})), {expect: expect}) self.assertEqual( json.loads(json.dumpsJSON({k: expect}, sort_keys=True)), {expect: expect}, ) self.assertRaises(TypeError, json.dumpsJSON, {json: 1}) for v in [{}, {"other": 1}, {b("derp"): 1, "herp": 2}]: for sort_keys in [False, True]: v0 = dict(v) v0[json] = 1 v1 = {as_text_type(key): val for (key, val) in v.items()} self.assertEqual( json.loads( json.dumpsJSON(v0, skipkeys=True, sort_keys=sort_keys)), v1, ) self.assertEqual( json.loads( json.dumpsJSON({"": v0}, skipkeys=True, sort_keys=sort_keys)), {"": v1}, ) self.assertEqual( json.loads( json.dumpsJSON([v0], skipkeys=True, sort_keys=sort_keys)), [v1], )
def test_issue3623(self): self.assertRaises(ValueError, json.decoder.scanstring, "xxx", 1, "xxx") self.assertRaises(UnicodeDecodeError, json.encoder.encode_basestring_ascii, b("xx\xff"))
'"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"', # noqa: B950 ), ( u"\u0123\u4567\u89ab\ucdef\uabcd\uef4a", '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"', ), (u"controls", '"controls"'), (u"\x08\x0c\n\r\t", '"\\b\\f\\n\\r\\t"'), ( u'{"object with 1 member":["array with 1 element"]}', '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"', ), (u" s p a c e d ", '" s p a c e d "'), (u"\U0001d120", '"\\ud834\\udd20"'), (u"\u03b1\u03a9", '"\\u03b1\\u03a9"'), (b("\xce\xb1\xce\xa9"), '"\\u03b1\\u03a9"'), (u"\u03b1\u03a9", '"\\u03b1\\u03a9"'), (b("\xce\xb1\xce\xa9"), '"\\u03b1\\u03a9"'), (u"\u03b1\u03a9", '"\\u03b1\\u03a9"'), (u"\u03b1\u03a9", '"\\u03b1\\u03a9"'), (u"`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'), (u"\x08\x0c\n\r\t", '"\\b\\f\\n\\r\\t"'), ( u"\u0123\u4567\u89ab\ucdef\uabcd\uef4a", '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"', ), ] class TestEncodeBaseStringAscii(TestCase): def test_py_encode_basestring_ascii(self):
from hjson.compat import b CASES = [ (u'/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?', '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"' ), (u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'), (u'controls', '"controls"'), (u'\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'), (u'{"object with 1 member":["array with 1 element"]}', '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"'), (u' s p a c e d ', '" s p a c e d "'), (u'\U0001d120', '"\\ud834\\udd20"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (b('\xce\xb1\xce\xa9'), '"\\u03b1\\u03a9"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (b('\xce\xb1\xce\xa9'), '"\\u03b1\\u03a9"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (u"`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'), (u'\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'), (u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'), ] class TestEncodeBaseStringAscii(TestCase): def test_py_encode_basestring_ascii(self): self._test_encode_basestring_ascii( hjson.encoder.encode_basestring_ascii)
def test_strip_bom(self): content = u"\u3053\u3093\u306b\u3061\u308f" json_doc = codecs.BOM_UTF8 + b(json.dumpsJSON(content)) self.assertEqual(json.load(BytesIO(json_doc)), content) for doc in json_doc, json_doc.decode('utf8'): self.assertEqual(json.loads(doc), content)
from unittest import TestCase import hjson.encoder from hjson.compat import b CASES = [ (u'/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?', '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"'), (u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'), (u'controls', '"controls"'), (u'\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'), (u'{"object with 1 member":["array with 1 element"]}', '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"'), (u' s p a c e d ', '" s p a c e d "'), (u'\U0001d120', '"\\ud834\\udd20"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (b('\xce\xb1\xce\xa9'), '"\\u03b1\\u03a9"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (b('\xce\xb1\xce\xa9'), '"\\u03b1\\u03a9"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (u'\u03b1\u03a9', '"\\u03b1\\u03a9"'), (u"`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'), (u'\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'), (u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'), ] class TestEncodeBaseStringAscii(TestCase): def test_py_encode_basestring_ascii(self): self._test_encode_basestring_ascii(hjson.encoder.encode_basestring_ascii) def _test_encode_basestring_ascii(self, encode_basestring_ascii): fname = encode_basestring_ascii.__name__ for input_string, expect in CASES:
def test_strip_bom(self): content = u"\u3053\u3093\u306b\u3061\u308f" json_doc = codecs.BOM_UTF8 + b(json.dumpsJSON(content)) self.assertEqual(json.load(BytesIO(json_doc)), content) for doc in json_doc, json_doc.decode("utf8"): self.assertEqual(json.loads(doc), content)