def test_decode_dict(): test_input = "{}" obj = sd_ujson.decode(test_input) assert {} == obj test_input = '{"one": 1, "two": 2, "three": 3}' obj = sd_ujson.decode(test_input) assert {"one": 1, "two": 2, "three": 3} == obj
def test_encode_long_neg_conversion(): test_input = -9223372036854775808 output = sd_ujson.encode(test_input) sdjson.loads(output) sd_ujson.decode(output) assert test_input == sdjson.loads(output) assert output == sdjson.dumps(test_input) assert test_input == sd_ujson.decode(output)
def test_encode_null_character(): test_input = "31337 \x00 1337" output = sd_ujson.encode(test_input) assert test_input == sdjson.loads(output) assert output == sdjson.dumps(test_input) assert test_input == sd_ujson.decode(output) test_input = "\x00" output = sd_ujson.encode(test_input) assert test_input == sdjson.loads(output) assert output == sdjson.dumps(test_input) assert test_input == sd_ujson.decode(output) assert '" \\u0000\\r\\n "' == sd_ujson.dumps(" \u0000\r\n ")
def test_encode_to_utf8(): test_input = b"\xe6\x97\xa5\xd1\x88" test_input = test_input.decode("utf-8") enc = sd_ujson.encode(test_input, ensure_ascii=False) dec = sd_ujson.decode(enc) assert enc == sdjson.dumps(test_input, ensure_ascii=False) assert dec == sdjson.loads(enc)
def helper(expected_output, **encode_kwargs): if "escape_forward_slashes" not in encode_kwargs: encode_kwargs["escape_forward_slashes"] = True, output = sd_ujson.encode(test_input, **encode_kwargs) assert output == expected_output if encode_kwargs.get("escape_forward_slashes", True): assert test_input == sdjson.loads(output) assert test_input == sd_ujson.decode(output)
def test_double_long_issue(): sut = {"a": -4342969734183514} encoded = sdjson.dumps(sut) decoded = sdjson.loads(encoded) assert sut == decoded encoded = sd_ujson.encode(sut) decoded = sd_ujson.decode(encoded) assert sut == decoded
def test_double_long_decimal_issue(): sut = {"a": -12345678901234.56789012} encoded = sdjson.dumps(sut) decoded = sdjson.loads(encoded) assert sut == decoded encoded = sd_ujson.encode(sut) decoded = sd_ujson.decode(encoded) assert sut == decoded
def test_decode_number_with32bit_sign_bit(): # 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): assert sd_ujson.decode(doc)["id"] == result
def test_object_with_json(): # If __json__ returns a string, then that string # will be used as a raw JSON snippet in the object. output_text = "this is the correct output" class JSONTest: def __json__(self): return '"' + output_text + '"' d = {"key": JSONTest()} output = sd_ujson.encode(d) dec = sd_ujson.decode(output) assert dec == {"key": output_text}
def test_object_with_complex_json(): # If __json__ returns a string, then that string # will be used as a raw JSON snippet in the object. obj = {"foo": ["bar", "baz"]} class JSONTest: def __json__(self): return sd_ujson.encode(obj) d = {"key": JSONTest()} output = sd_ujson.encode(d) dec = sd_ujson.decode(output) assert dec == {"key": obj}
def test_to_dict(): 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 = sd_ujson.encode(o) dec = sd_ujson.decode(output) assert dec == d
def test_encode_long_conversion(test_input): output = sd_ujson.encode(test_input) assert test_input == sdjson.loads(output) assert output == sdjson.dumps(test_input) assert test_input == sd_ujson.decode(output)
def test_decode_raises_for_long_input(test_input, expected): with pytest.raises(expected): sd_ujson.decode(test_input * (1024 * 1024))
def test_decode_raises(test_input, expected): with pytest.raises(expected): print(test_input) sd_ujson.decode(test_input)
def test_decode_range_raises(test_input, expected): for x in range(1000): with pytest.raises(ValueError): sd_ujson.decode(test_input)
def test_decode_numeric_int_exp(test_input): output = sd_ujson.decode(test_input) assert output == sdjson.loads(test_input)
def test_decode_from_unicode(): test_input = '{"obj": 31337}' dec1 = sd_ujson.decode(test_input) dec2 = sd_ujson.decode(str(test_input)) assert dec1 == dec2
def test_encode_unicode(test_input): enc = sd_ujson.encode(test_input) dec = sd_ujson.decode(enc) assert enc == sdjson.dumps(test_input) assert dec == sdjson.loads(enc)
def test_decode_array_empty(): test_input = "[]" obj = sd_ujson.decode(test_input) assert [] == obj
def test_encode_dict_conversion(): test_input = {"k1": 1, "k2": 2, "k3": 3, "k4": 4} output = sd_ujson.encode(test_input) assert test_input == sdjson.loads(output) assert test_input == sd_ujson.decode(output) assert test_input == sd_ujson.decode(output)
def test_encode_decode_long_decimal(): sut = {"a": -528656961.4399388} encoded = sd_ujson.dumps(sut) sd_ujson.decode(encoded)
def test_decode_big_escape(): for x in range(10): base = "\u00e5".encode() quote = b'"' test_input = quote + (base * 1024 * 1024 * 2) + quote sd_ujson.decode(test_input)
def test_encode_decimal(): sut = decimal.Decimal("1337.1337") encoded = sd_ujson.encode(sut) decoded = sd_ujson.decode(encoded) assert decoded == 1337.1337
def test_encode_list_conversion(): test_input = [1, 2, 3, 4] output = sd_ujson.encode(test_input) assert test_input == sdjson.loads(output) assert test_input == sd_ujson.decode(output)
def test_encode_decode(test_input): output = sd_ujson.encode(test_input) assert test_input == sdjson.loads(output) assert output == sdjson.dumps(test_input) assert test_input == sd_ujson.decode(output)
def test_decode_no_assert(test_input): sd_ujson.decode(test_input)
def test_decode(test_input, expected): assert sd_ujson.decode(test_input) == expected
def test_decode_null_character(): test_input = '"31337 \\u0000 31337"' assert sd_ujson.decode(test_input) == sdjson.loads(test_input)
def test_decimal_decode_test(): sut = {"a": 4.56} encoded = sd_ujson.encode(sut) decoded = sd_ujson.decode(encoded) assert_almost_equal(sut["a"], decoded["a"])
def test_encode_control_escaping(): test_input = "\x19" enc = sd_ujson.encode(test_input) dec = sd_ujson.decode(enc) assert test_input == dec assert enc == sdjson.dumps(test_input)