def test_encode_truefalse(self): self.assertEqual(json.dumps( {True: False, False: True}, sort_keys=True), '{"false": true, "true": false}') self.assertEqual( json.dumps( {2: 3.0, 4.0: long_type(5), False: 1, long_type(6): True, "7": 0}, sort_keys=True), '{"2": 3.0, "4.0": 5, "6": true, "7": 0, "false": 1}')
def test_int_as_string_bitcount_overflow(self): long_count = long_type(2)**32 + 31 def test(): encoder.JSONEncoder(int_as_string_bitcount=long_count).encode(0) self.assertRaises((TypeError, OverflowError), test)
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.dumps({k: expect})), {expect: expect}) self.assertEqual( json.loads(json.dumps({k: expect}, sort_keys=True)), {expect: expect}) self.assertRaises(TypeError, json.dumps, {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.dumps(v0, skipkeys=True, sort_keys=sort_keys)), v1) self.assertEqual( json.loads( json.dumps({'': v0}, skipkeys=True, sort_keys=sort_keys)), {'': v1}) self.assertEqual( json.loads( json.dumps([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.dumps({k: expect})), {expect: expect}) self.assertEqual( json.loads(json.dumps({k: expect}, sort_keys=True)), {expect: expect}) self.assertRaises(TypeError, json.dumps, {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.dumps(v0, skipkeys=True, sort_keys=sort_keys)), v1) self.assertEqual( json.loads(json.dumps({'': v0}, skipkeys=True, sort_keys=sort_keys)), {'': v1}) self.assertEqual( json.loads(json.dumps([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.assertEquals(json.loads(json.dumps({k: expect})), {expect: expect}) self.assertEquals(json.loads(json.dumps({k: expect}, sort_keys=True)), {expect: expect}) self.assertRaises(TypeError, json.dumps, {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.assertEquals(json.loads(json.dumps(v0, skipkeys=True, sort_keys=sort_keys)), v1) self.assertEquals(json.loads(json.dumps({"": v0}, skipkeys=True, sort_keys=sort_keys)), {"": v1}) self.assertEquals(json.loads(json.dumps([v0], skipkeys=True, sort_keys=sort_keys)), [v1])
def test_ints(self): for num in [1, long_type(1), 1 << 32, 1 << 64]: self.assertEqual(json.dumps(num), str(num)) self.assertEqual(int(json.dumps(num)), num) self.assertEqual(json.loads(json.dumps(num)), num) self.assertEqual(json.loads(text_type(json.dumps(num))), num)
def test_ints(self): for num in [1, long_type(1), 1<<32, 1<<64]: self.assertEquals(json.dumps(num), str(num)) self.assertEquals(int(json.dumps(num)), num) self.assertEquals(json.loads(json.dumps(num)), num) self.assertEquals(json.loads(text_type(json.dumps(num))), num)
def test_int_as_string_bitcount_overflow(self): long_count = long_type(2)**32+31 def test(): encoder.JSONEncoder(int_as_string_bitcount=long_count).encode(0) self.assertRaises((TypeError, OverflowError), test)