def test_encodeLongNegConversion(self): input = -9223372036854775808 output = ujson.encode(input) json.loads(output) ujson.decode(output) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_encodeNullCharacter(self): input = "31337 \x00 1337" output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output)) input = "\x00" output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output)) self.assertEqual('" \\u0000\\r\\n "', ujson.dumps(" \u0000\r\n "))
def test_encodeLongUnsignedConversion(self): input = 18446744073709551615 output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_encodeUnicode4BytesUTF8(self): input = "\xf0\x91\x80\xb0TRAILINGNORMAL" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, json.loads(enc))
def test_encodeToUTF8(self): input = b"\xe6\x97\xa5\xd1\x88" input = input.decode("utf-8") enc = ujson.encode(input, ensure_ascii=False) dec = ujson.decode(enc) self.assertEqual(enc, json.dumps(input, ensure_ascii=False)) self.assertEqual(dec, json.loads(enc))
def test_encodeArrayInArray(self): input = [[[[]]]] output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_encodeUnicode4BytesUTF8Highest(self): input = "\xf3\xbf\xbf\xbfTRAILINGNORMAL" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, json.loads(enc))
def test_encodeUnicodeSurrogatePair(self): input = "\xf0\x90\x8d\x86" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, json.loads(enc))
def test_doubleLongIssue(self): sut = {"a": -4342969734183514} encoded = json.dumps(sut) decoded = json.loads(encoded) self.assertEqual(sut, decoded) encoded = ujson.encode(sut) decoded = ujson.decode(encoded) self.assertEqual(sut, decoded)
def test_doubleLongDecimalIssue(self): sut = {"a": -12345678901234.56789012} encoded = json.dumps(sut) decoded = json.loads(encoded) self.assertEqual(sut, decoded) encoded = ujson.encode(sut) decoded = ujson.decode(encoded) self.assertEqual(sut, decoded)
def test_encodeListLongConversion(self): input = [ 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, ] output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(input, ujson.decode(output))
def test_decodeNumberWith32bitSignBit(self): # Test that numbers that fit within 32 bits but would have the # sign bit set (2**31 <= x < 2**32) are decoded properly. docs = ( '{"id": 3590016419}', '{"id": %s}' % 2**31, '{"id": %s}' % 2**32, '{"id": %s}' % ((2**32) - 1), ) results = (3590016419, 2**31, 2**32, 2**32 - 1) for doc, result in zip(docs, results): self.assertEqual(ujson.decode(doc)["id"], result)
def test_object_with_complex_json(self): # If __json__ returns a string, then that string # will be used as a raw JSON snippet in the object. obj = {u"foo": [u"bar", u"baz"]} class JSONTest: def __json__(self): return ujson.encode(obj) d = {u"key": JSONTest()} output = ujson.encode(d) dec = ujson.decode(output) self.assertEqual(dec, {u"key": obj})
def test_object_with_json_unicode(self): # If __json__ returns a string, then that string # will be used as a raw JSON snippet in the object. output_text = u"this is the correct output" class JSONTest: def __json__(self): return u'"' + output_text + u'"' d = {u"key": JSONTest()} output = ujson.encode(d) dec = ujson.decode(output) self.assertEqual(dec, {u"key": output_text})
def test_toDict(self): d = {"key": 31337} class DictTest: def toDict(self): return d def __json__(self): return '"json defined"' # Fallback and shouldn't be called. o = DictTest() output = ujson.encode(o) dec = ujson.decode(output) self.assertEqual(dec, d)
def test_decodeArrayOneItem(self): input = "[31337]" ujson.decode(input)
def test_decodeArrayEmpty(self): input = "[]" obj = ujson.decode(input) self.assertEqual([], obj)
def test_decodeBigValue(self): input = "9223372036854775807" ujson.decode(input)
def test_decodeNumericIntPos(self): input = "31337" self.assertEqual(31337, ujson.decode(input))
def test_decodeWithTrailingWhitespaces(self): input = "{}\n\t " ujson.decode(input)
def test_decodeNumericIntExpe(self): input = "1337e40" output = ujson.decode(input) self.assertEqual(output, json.loads(input))
def test_numericIntFrcExp(self): input = "1.337E40" output = ujson.decode(input) self.assertEqual(output, json.loads(input))
def test_decodeNumericIntNeg(self): input = "-31337" self.assertEqual(-31337, ujson.decode(input))
def test_decodeLongUnsignedValue(self): input = "18446744073709551615" ujson.decode(input)
def test_encodeDecodeLongDecimal(self): sut = {"a": -528656961.4399388} encoded = ujson.dumps(sut) ujson.decode(encoded)
def test_decodeBigEscape(self): for x in range(10): base = "\u00e5".encode("utf-8") quote = '"'.encode() input = quote + (base * 1024 * 1024 * 2) + quote ujson.decode(input)
def test_decodeSmallValue(self): input = "-9223372036854775808" ujson.decode(input)
def test_decodeNullCharacter(self): input = '"31337 \\u0000 31337"' self.assertEqual(ujson.decode(input), json.loads(input))
def test_decimalDecodeTest(self): sut = {"a": 4.56} encoded = ujson.encode(sut) decoded = ujson.decode(encoded) self.assertAlmostEqual(sut[u"a"], decoded[u"a"])
def test_decodeNumericIntExpeMinus(self): input = "1.337e-4" output = ujson.decode(input) self.assertEqual(output, json.loads(input))