def testEncodeSymbols(self): s = '\u273f\u2661\u273f' # ✿♡✿ encoded = hyperjson.dumps(s) encoded_json = json.dumps(s) # NOTE: Python's json module escapes the unicode codepoints # While hyperjson converts them to actual utf8 characters # This should be fine because # - JSON supports utf8 (https://stackoverflow.com/a/594881/270334) # - encoding and consecutive decoding yields the input # self.assertEqual(len(encoded), len(s) * 6 + 2) # 6 characters + quotes #self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded) if six.PY3: encoded = hyperjson.dumps(s, ensure_ascii=False) else: encoded = hyperjson.dumps(s, ensure_ascii=False).decode("utf-8") # json outputs an unicode object encoded_json = json.dumps(s, ensure_ascii=False) self.assertEqual(len(encoded), len(s) + 2) # original length + quotes self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded)
def test_encodeArrayInArray(self): input = [[[[]]]] output = hyperjson.dumps(input) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual(output, hyperjson.dumps(input)) self.assertEqual(input, hyperjson.loads(output))
def testEncodeUnicodeBMP(self): s = '\U0001f42e\U0001f42e\U0001F42D\U0001F42D' # 🐮🐮🐭🐭 encoded = hyperjson.dumps(s) encoded_json = hyperjson.dumps(s) if len(s) == 4: self.assertEqual(len(encoded), len(s) * 12 + 2) else: self.assertEqual(len(encoded), len(s) * 6 + 2) self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded) # hyperjson outputs an UTF-8 encoded str object if six.PY3: encoded = hyperjson.dumps(s, ensure_ascii=False) else: encoded = hyperjson.dumps(s, ensure_ascii=False).decode("utf-8") # json outputs an unicode object encoded_json = hyperjson.dumps(s, ensure_ascii=False) self.assertEqual(len(encoded), len(s) + 2) # original length + quotes self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded)
def test_encodeLongUnsignedConversion(self): input = 18446744073709551615 output = hyperjson.dumps(input) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual(output, hyperjson.dumps(input)) self.assertEqual(input, hyperjson.loads(output))
def testEncodeUnicodeBMP(self): s = '\U0001f42e\U0001f42e\U0001F42D\U0001F42D' # 🐮🐮🐭🐭 encoded = hyperjson.dumps(s) encoded_json = json.dumps(s) self.assertEqual(json.loads(json.dumps(s)), s) self.assertEqual(hyperjson.loads(hyperjson.dumps(s)), s) # Ignore length comparison because the output format # of hyperjson and json is slightly different # (code points in the case of json, utf8 in the case of hyperjson) # Loading the serialized object back to Python works, though # so that should not be an issue. # Also see testEncodeSymbols # if len(s) == 4: # self.assertEqual(len(encoded), len(s) * 12 + 2) # else: # self.assertEqual(len(encoded), len(s) * 6 + 2) #self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded) # hyperjson outputs an UTF-8 encoded str object if six.PY3: encoded = hyperjson.dumps(s, ensure_ascii=False) else: encoded = hyperjson.dumps(s, ensure_ascii=False).decode("utf-8") # json outputs an unicode object encoded_json = hyperjson.dumps(s, ensure_ascii=False) self.assertEqual(len(encoded), len(s) + 2) # original length + quotes self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded)
def test_encodeBigEscape(self): for x in range(10): if six.PY3: base = '\u00e5'.encode('utf-8') else: base = "\xc3\xa5" input = base * 1024 * 1024 * 2 hyperjson.dumps(input)
def test_doubleLongIssue(self): sut = {'a': -4342969734183514} encoded = hyperjson.dumps(sut) decoded = hyperjson.loads(encoded) self.assertEqual(sut, decoded) encoded = hyperjson.dumps(sut) decoded = hyperjson.loads(encoded) self.assertEqual(sut, decoded)
def test_doubleLongDecimalIssue(self): sut = {'a': -12345678901234.56789012} encoded = hyperjson.dumps(sut) decoded = hyperjson.loads(encoded) self.assertEqual(sut, decoded) encoded = hyperjson.dumps(sut) decoded = hyperjson.loads(encoded) self.assertEqual(sut, decoded)
def test_encodeToUTF8(self): input = b"\xe6\x97\xa5\xd1\x88" if six.PY3: input = input.decode('utf-8') enc = hyperjson.dumps(input, ensure_ascii=False) dec = hyperjson.loads(enc) self.assertEqual(enc, hyperjson.dumps(input, ensure_ascii=False)) self.assertEqual(dec, hyperjson.loads(enc))
def test_encodeLongNegConversion(self): input = -9223372036854775808 output = hyperjson.dumps(input) hyperjson.loads(output) hyperjson.loads(output) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual(output, hyperjson.dumps(input)) self.assertEqual(input, hyperjson.loads(output))
def test_encodeNullCharacter(self): input = "31337 \x00 1337" output = hyperjson.dumps(input) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual(output, hyperjson.dumps(input)) self.assertEqual(input, hyperjson.loads(output)) input = "\x00" output = hyperjson.dumps(input) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual(output, hyperjson.dumps(input)) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual('" \\u0000\\r\\n "', hyperjson.dumps(" \u0000\r\n "))
def test_encodeDecimal(self): sut = decimal.Decimal("1337.1337") encoded = hyperjson.dumps(sut) print(encoded) decoded = hyperjson.loads(encoded) print(decoded) self.assertEqual(decoded, 1337.1337)
def test_encodeUnicode4BytesUTF8Highest(self): input = "\xf3\xbf\xbf\xbfTRAILINGNORMAL" enc = hyperjson.dumps(input) dec = hyperjson.loads(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, hyperjson.loads(enc))
def test_encodeUnicode4BytesUTF8(self): input = "\xf0\x91\x80\xb0TRAILINGNORMAL" enc = hyperjson.dumps(input) dec = hyperjson.loads(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, hyperjson.loads(enc))
def test_encodeUnicodeSurrogatePair(self): input = "\xf0\x90\x8d\x86" enc = hyperjson.dumps(input) dec = hyperjson.loads(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, hyperjson.loads(enc))
def test_docs(): assert hyperjson.dumps([{ "key": "value" }, 81, True]) == '[{"key":"value"},81,true]' assert hyperjson.loads("""[{"key": "value"}, 81, true]""") == [{ u'key': u'value' }, 81, True]
def test_encodeListLongConversion(self): input = [ 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807 ] output = hyperjson.dumps(input) self.assertEqual(input, hyperjson.loads(output)) self.assertEqual(input, hyperjson.loads(output))
def test_simple_dicts(d, allowed): """ Python dictionaries are guaranteed to be ordered in Python 3.6+, in Python <=3.5 they are not ordered. In Rust, HashMaps are not, but that's not a big deal because JSON also doesn't guarantee order. See https://stackoverflow.com/a/7214316/270334 Therefore, we ignore ordering to avoid flaky tests. """ actual = ignore_whitespace(hyperjson.dumps(d)) assert actual in allowed
def test_dict(v1, v2): """ Python dictionaries are guaranteed to be ordered. In Rust, HashMaps are not, but that's not a big deal because JSON also doesn't guarantee order. See https://stackoverflow.com/a/7214316/270334 Therefore, we ignore ordering to avoid flaky tests. """ expected1 = ignore_whitespace(json.dumps(v1)) expected2 = ignore_whitespace(json.dumps(v2)) actual = ignore_whitespace(hyperjson.dumps(d)) assert (expected1 == actual) or (expected2 == actual)
def test_encodeDictWithUnicodeKeys(self): input = { "key1": "value1", "key1": "value1", "key1": "value1", "key1": "value1", "key1": "value1", "key1": "value1" } hyperjson.dumps(input) input = { "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1" } hyperjson.dumps(input)
def testEncodeSymbols(self): s = '\u273f\u2661\u273f' # ✿♡✿ encoded = hyperjson.dumps(s) encoded_json = hyperjson.dumps(s) self.assertEqual(len(encoded), len(s) * 6 + 2) # 6 characters + quotes self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded) # hyperjson outputs an UTF-8 encoded str object if six.PY3: encoded = hyperjson.dumps(s, ensure_ascii=False) else: encoded = hyperjson.dumps(s, ensure_ascii=False).decode("utf-8") # json outputs an unicode object encoded_json = hyperjson.dumps(s, ensure_ascii=False) self.assertEqual(len(encoded), len(s) + 2) # original length + quotes self.assertEqual(encoded, encoded_json) decoded = hyperjson.loads(encoded) self.assertEqual(s, decoded)
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 hyperjson.dumps(obj) d = {u'key': JSONTest()} output = hyperjson.dumps(d) dec = hyperjson.loads(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 = hyperjson.dumps(d) dec = hyperjson.loads(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 = hyperjson.dumps(o) dec = hyperjson.loads(output) self.assertEqual(dec, d)
def test_dict_of_arrays_of_dict_string_int_pairs(): payload = { '9.865710069007799': [{ '19.37384331792742': 315795 }], '5.076904844489237': [{ '0.479301331636357': 460144 }] } # The order of elements is different when using hyperjson, # because of Rust's hashmap implementation. # assert ignore_whitespace(json.dumps(payload)) == ignore_whitespace( # hyperjson.dumps(payload)) assert hyperjson.loads(hyperjson.dumps(payload)) == payload
def test_decimalDecodeTest(self): sut = {'a': 4.56} encoded = hyperjson.dumps(sut) decoded = hyperjson.loads(encoded) self.assertAlmostEqual(sut[u'a'], decoded[u'a'])
def test_encodeDecodeLongDecimal(self): sut = {'a': -528656961.4399388} encoded = hyperjson.dumps(sut) hyperjson.loads(encoded)
def test_sortKeys(self): data = {"a": 1, "c": 1, "b": 1, "e": 1, "f": 1, "d": 1} sortedKeys = hyperjson.dumps(data, sort_keys=True) self.assertEqual(sortedKeys, '{"a": 1, "b": 1, "c": 1, "d": 1, "e": 1, "f": 1}')
def test_WriteArrayOfSymbolsFromTuple(self): self.assertEqual("[true, false, null]", hyperjson.dumps((True, False, None)))
def test_WriteArrayOfSymbolsFromList(self): self.assertEqual("[true, false, null]", hyperjson.dumps([True, False, None]))