Exemplo n.º 1
0
    def test_encode_numeric_overflow_nested(self):
        class Nested(object):
            x = 12839128391289382193812939

        for _ in range(0, 100):
            with pytest.raises(OverflowError):
                ujson.encode(Nested())
Exemplo n.º 2
0
    def test_invalid_double_precision(self, invalid_val):
        double_input = 30.12345678901234567890
        expected_exception = (ValueError if isinstance(invalid_val, int)
                              else TypeError)

        with pytest.raises(expected_exception):
            ujson.encode(double_input, double_precision=invalid_val)
Exemplo n.º 3
0
    def test_array_reshaped(self, shape):
        arr = np.arange(100)
        arr = arr.reshape(shape)

        tm.assert_numpy_array_equal(
            np.array(ujson.decode(ujson.encode(arr))), arr)
        tm.assert_numpy_array_equal(ujson.decode(
            ujson.encode(arr), numpy=True), arr)
Exemplo n.º 4
0
    def test_array_basic(self):
        arr = np.arange(96)
        arr = arr.reshape((2, 2, 2, 2, 3, 2))

        tm.assert_numpy_array_equal(
            np.array(ujson.decode(ujson.encode(arr))), arr)
        tm.assert_numpy_array_equal(ujson.decode(
            ujson.encode(arr), numpy=True), arr)
Exemplo n.º 5
0
    def test_encode_big_set(self):
        s = set()

        for x in range(0, 100000):
            s.add(x)

        # Make sure no Exception is raised.
        ujson.encode(s)
Exemplo n.º 6
0
 def test_encode_double_tiny_exponential(self):
     num = 1e-40
     assert num == ujson.decode(ujson.encode(num))
     num = 1e-100
     assert num == ujson.decode(ujson.encode(num))
     num = -1e-45
     assert num == ujson.decode(ujson.encode(num))
     num = -1e-145
     assert np.allclose(num, ujson.decode(ujson.encode(num)))
Exemplo n.º 7
0
    def test_series_nested(self, orient):
        s = Series([10, 20, 30, 40, 50, 60], name="series",
                   index=[6, 7, 8, 9, 10, 15]).sort_values()
        nested = {"s1": s, "s2": s.copy()}
        kwargs = {} if orient is None else dict(orient=orient)

        exp = {"s1": ujson.decode(ujson.encode(s, **kwargs)),
               "s2": ujson.decode(ujson.encode(s, **kwargs))}
        assert ujson.decode(ujson.encode(nested, **kwargs)) == exp
Exemplo n.º 8
0
    def test_dataframe_nested(self, orient):
        df = DataFrame([[1, 2, 3], [4, 5, 6]], index=[
            "a", "b"], columns=["x", "y", "z"])

        nested = {"df1": df, "df2": df.copy()}
        kwargs = {} if orient is None else dict(orient=orient)

        exp = {"df1": ujson.decode(ujson.encode(df, **kwargs)),
               "df2": ujson.decode(ujson.encode(df, **kwargs))}
        assert ujson.decode(ujson.encode(nested, **kwargs)) == exp
Exemplo n.º 9
0
    def test_array_float(self):
        dtype = np.float32

        arr = np.arange(100.202, 200.202, 1, dtype=dtype)
        arr = arr.reshape((5, 5, 4))

        arr_out = np.array(ujson.decode(ujson.encode(arr)), dtype=dtype)
        tm.assert_almost_equal(arr, arr_out)

        arr_out = ujson.decode(ujson.encode(arr), numpy=True, dtype=dtype)
        tm.assert_almost_equal(arr, arr_out)
Exemplo n.º 10
0
    def test_double_precision(self):
        double_input = 30.012345678901234
        output = ujson.encode(double_input, double_precision=15)

        assert double_input == json.loads(output)
        assert double_input == ujson.decode(output)

        for double_precision in (3, 9):
            output = ujson.encode(double_input,
                                  double_precision=double_precision)
            rounded_input = round(double_input, double_precision)

            assert rounded_input == json.loads(output)
            assert rounded_input == ujson.decode(output)
Exemplo n.º 11
0
    def test_encode_null_character(self):
        wrapped_input = "31337 \x00 1337"
        output = ujson.encode(wrapped_input)

        assert wrapped_input == json.loads(output)
        assert output == json.dumps(wrapped_input)
        assert wrapped_input == ujson.decode(output)

        alone_input = "\x00"
        output = ujson.encode(alone_input)

        assert alone_input == json.loads(output)
        assert output == json.dumps(alone_input)
        assert alone_input == ujson.decode(output)
        assert '"  \\u0000\\r\\n "' == ujson.dumps(u("  \u0000\r\n "))
Exemplo n.º 12
0
    def test_datetime_index(self):
        date_unit = "ns"

        rng = date_range("1/1/2000", periods=20)
        encoded = ujson.encode(rng, date_unit=date_unit)

        decoded = DatetimeIndex(np.array(ujson.decode(encoded)))
        tm.assert_index_equal(rng, decoded)

        ts = Series(np.random.randn(len(rng)), index=rng)
        decoded = Series(ujson.decode(ujson.encode(ts, date_unit=date_unit)))

        idx_values = decoded.index.values.astype(np.int64)
        decoded.index = DatetimeIndex(idx_values)
        tm.assert_series_equal(ts, decoded)
Exemplo n.º 13
0
    def test_default_handler(self):

        class _TestObject(object):

            def __init__(self, val):
                self.val = val

            @property
            def recursive_attr(self):
                return _TestObject("recursive_attr")

            def __str__(self):
                return str(self.val)

        msg = "Maximum recursion level reached"
        with pytest.raises(OverflowError, match=msg):
            ujson.encode(_TestObject("foo"))
        assert '"foo"' == ujson.encode(_TestObject("foo"),
                                       default_handler=str)

        def my_handler(_):
            return "foobar"

        assert '"foobar"' == ujson.encode(_TestObject("foo"),
                                          default_handler=my_handler)

        def my_handler_raises(_):
            raise TypeError("I raise for anything")

        with pytest.raises(TypeError, match="I raise for anything"):
            ujson.encode(_TestObject("foo"), default_handler=my_handler_raises)

        def my_int_handler(_):
            return 42

        assert ujson.decode(ujson.encode(_TestObject("foo"),
                                         default_handler=my_int_handler)) == 42

        def my_obj_handler(_):
            return datetime.datetime(2013, 2, 3)

        assert (ujson.decode(ujson.encode(datetime.datetime(2013, 2, 3))) ==
                ujson.decode(ujson.encode(_TestObject("foo"),
                                          default_handler=my_obj_handler)))

        obj_list = [_TestObject("foo"), _TestObject("bar")]
        assert (json.loads(json.dumps(obj_list, default=str)) ==
                ujson.decode(ujson.encode(obj_list, default_handler=str)))
Exemplo n.º 14
0
    def test_encode_set(self):
        s = {1, 2, 3, 4, 5, 6, 7, 8, 9}
        enc = ujson.encode(s)
        dec = ujson.decode(enc)

        for v in dec:
            assert v in s
Exemplo n.º 15
0
    def test_encode_long_conversion(self):
        long_input = 9223372036854775807
        output = ujson.encode(long_input)

        assert long_input == json.loads(output)
        assert output == json.dumps(long_input)
        assert long_input == ujson.decode(output)
Exemplo n.º 16
0
    def test_dataframe(self, orient, numpy):
        if orient == "records" and numpy:
            pytest.skip("Not idiomatic pandas")

        df = DataFrame([[1, 2, 3], [4, 5, 6]], index=[
            "a", "b"], columns=["x", "y", "z"])
        encode_kwargs = {} if orient is None else dict(orient=orient)
        decode_kwargs = {} if numpy is None else dict(numpy=numpy)

        output = ujson.decode(ujson.encode(df, **encode_kwargs),
                              **decode_kwargs)

        # Ensure proper DataFrame initialization.
        if orient == "split":
            dec = _clean_dict(output)
            output = DataFrame(**dec)
        else:
            output = DataFrame(output)

        # Corrections to enable DataFrame comparison.
        if orient == "values":
            df.columns = [0, 1, 2]
            df.index = [0, 1]
        elif orient == "records":
            df.index = [0, 1]
        elif orient == "index":
            df = df.transpose()

        tm.assert_frame_equal(output, df, check_dtype=False)
Exemplo n.º 17
0
    def test_series(self, orient, numpy):
        s = Series([10, 20, 30, 40, 50, 60], name="series",
                   index=[6, 7, 8, 9, 10, 15]).sort_values()

        encode_kwargs = {} if orient is None else dict(orient=orient)
        decode_kwargs = {} if numpy is None else dict(numpy=numpy)

        output = ujson.decode(ujson.encode(s, **encode_kwargs),
                              **decode_kwargs)

        if orient == "split":
            dec = _clean_dict(output)
            output = Series(**dec)
        else:
            output = Series(output)

        if orient in (None, "index"):
            s.name = None
            output = output.sort_values()
            s.index = ["6", "7", "8", "9", "10", "15"]
        elif orient in ("records", "values"):
            s.name = None
            s.index = [0, 1, 2, 3, 4, 5]

        tm.assert_series_equal(output, s, check_dtype=False)
Exemplo n.º 18
0
    def test_encode_string_conversion2(self):
        string_input = "A string \\ / \b \f \n \r \t"
        output = ujson.encode(string_input)

        assert string_input == json.loads(output)
        assert string_input == ujson.decode(output)
        assert output == '"A string \\\\ \\/ \\b \\f \\n \\r \\t"'
Exemplo n.º 19
0
    def test_int_array(self, any_int_dtype):
        arr = np.arange(100, dtype=np.int)
        arr_input = arr.astype(any_int_dtype)

        arr_output = np.array(ujson.decode(ujson.encode(arr_input)),
                              dtype=any_int_dtype)
        tm.assert_numpy_array_equal(arr_input, arr_output)
Exemplo n.º 20
0
    def test_encode_control_escaping(self):
        escaped_input = "\x19"
        enc = ujson.encode(escaped_input)
        dec = ujson.decode(enc)

        assert escaped_input == dec
        assert enc == json_unicode(escaped_input)
Exemplo n.º 21
0
    def test_encode_unicode_surrogate_pair(self):
        surrogate_input = "\xf0\x90\x8d\x86"
        enc = ujson.encode(surrogate_input)
        dec = ujson.decode(enc)

        assert enc == json_unicode(surrogate_input)
        assert dec == json.loads(enc)
Exemplo n.º 22
0
    def test_encode_unicode_4bytes_utf8(self):
        four_bytes_input = "\xf0\x91\x80\xb0TRAILINGNORMAL"
        enc = ujson.encode(four_bytes_input)
        dec = ujson.decode(enc)

        assert enc == json_unicode(four_bytes_input)
        assert dec == json.loads(enc)
Exemplo n.º 23
0
    def test_encode_datetime_conversion(self):
        datetime_input = datetime.datetime.fromtimestamp(time.time())
        output = ujson.encode(datetime_input, date_unit="s")
        expected = calendar.timegm(datetime_input.utctimetuple())

        assert int(expected) == json.loads(output)
        assert int(expected) == ujson.decode(output)
Exemplo n.º 24
0
 def test_bool_array(self):
     bool_array = np.array([
         True, False, True, True,
         False, True, False, False], dtype=np.bool)
     output = np.array(ujson.decode(
         ujson.encode(bool_array)), dtype=np.bool)
     tm.assert_numpy_array_equal(bool_array, output)
Exemplo n.º 25
0
    def test_index(self):
        i = Index([23, 45, 18, 98, 43, 11], name="index")

        # Column indexed.
        output = Index(ujson.decode(ujson.encode(i)), name="index")
        tm.assert_index_equal(i, output)

        output = Index(ujson.decode(ujson.encode(i), numpy=True), name="index")
        tm.assert_index_equal(i, output)

        dec = _clean_dict(ujson.decode(ujson.encode(i, orient="split")))
        output = Index(**dec)

        tm.assert_index_equal(i, output)
        assert i.name == output.name

        dec = _clean_dict(ujson.decode(ujson.encode(i, orient="split"),
                                       numpy=True))
        output = Index(**dec)

        tm.assert_index_equal(i, output)
        assert i.name == output.name

        output = Index(ujson.decode(ujson.encode(i, orient="values")),
                       name="index")
        tm.assert_index_equal(i, output)

        output = Index(ujson.decode(ujson.encode(i, orient="values"),
                                    numpy=True), name="index")
        tm.assert_index_equal(i, output)

        output = Index(ujson.decode(ujson.encode(i, orient="records")),
                       name="index")
        tm.assert_index_equal(i, output)

        output = Index(ujson.decode(ujson.encode(i, orient="records"),
                                    numpy=True), name="index")
        tm.assert_index_equal(i, output)

        output = Index(ujson.decode(ujson.encode(i, orient="index")),
                       name="index")
        tm.assert_index_equal(i, output)

        output = Index(ujson.decode(ujson.encode(i, orient="index"),
                                    numpy=True), name="index")
        tm.assert_index_equal(i, output)
Exemplo n.º 26
0
    def test_float_array(self, float_dtype):
        arr = np.arange(12.5, 185.72, 1.7322, dtype=np.float)
        float_input = arr.astype(float_dtype)

        float_output = np.array(ujson.decode(
            ujson.encode(float_input, double_precision=15)),
            dtype=float_dtype)
        tm.assert_almost_equal(float_input, float_output)
Exemplo n.º 27
0
    def test_encode_unicode_4bytes_utf8highest(self):
        four_bytes_input = "\xf3\xbf\xbf\xbfTRAILINGNORMAL"
        enc = ujson.encode(four_bytes_input)

        dec = ujson.decode(enc)

        assert enc == json_unicode(four_bytes_input)
        assert dec == json.loads(enc)
Exemplo n.º 28
0
    def test_datetime_units(self):
        val = datetime.datetime(2013, 8, 17, 21, 17, 12, 215504)
        stamp = Timestamp(val)

        roundtrip = ujson.decode(ujson.encode(val, date_unit='s'))
        assert roundtrip == stamp.value // 10**9

        roundtrip = ujson.decode(ujson.encode(val, date_unit='ms'))
        assert roundtrip == stamp.value // 10**6

        roundtrip = ujson.decode(ujson.encode(val, date_unit='us'))
        assert roundtrip == stamp.value // 10**3

        roundtrip = ujson.decode(ujson.encode(val, date_unit='ns'))
        assert roundtrip == stamp.value

        pytest.raises(ValueError, ujson.encode, val, date_unit='foo')
Exemplo n.º 29
0
    def test_encode_recursion_max(self):
        # 8 is the max recursion depth

        class O2(object):
            member = 0
            pass

        class O1(object):
            member = 0
            pass

        decoded_input = O1()
        decoded_input.member = O2()
        decoded_input.member.member = decoded_input

        with pytest.raises(OverflowError):
            ujson.encode(decoded_input)
Exemplo n.º 30
0
    def test_encode_to_utf8(self):
        unencoded = "\xe6\x97\xa5\xd1\x88"

        enc = ujson.encode(unencoded, ensure_ascii=False)
        dec = ujson.decode(enc)

        assert enc == json_unicode(unencoded, ensure_ascii=False)
        assert dec == json.loads(enc)
Exemplo n.º 31
0
 def test_encode_as_null(self, decoded_input):
     assert ujson.encode(decoded_input) == "null", "Expected null"
Exemplo n.º 32
0
    def test_float_max(self, float_dtype):
        klass = np.dtype(float_dtype).type
        num = klass(np.finfo(float_dtype).max / 10)

        tm.assert_almost_equal(
            klass(ujson.decode(ujson.encode(num, double_precision=15))), num)
Exemplo n.º 33
0
 def test_encode_unicode_4bytes_utf8_fail(self):
     with pytest.raises(OverflowError):
         ujson.encode("\xfd\xbf\xbf\xbf\xbf\xbf")
Exemplo n.º 34
0
 def test_encode_big_escape(self):
     # Make sure no Exception is raised.
     for _ in range(10):
         base = '\u00e5'.encode("utf-8") if compat.PY3 else "\xc3\xa5"
         escape_input = base * 1024 * 1024 * 2
         ujson.encode(escape_input)
Exemplo n.º 35
0
 def test_encode_empty_set(self):
     assert "[]" == ujson.encode(set())
Exemplo n.º 36
0
    def test_encode_timedelta_iso(self, td):
        # GH 28256
        result = ujson.encode(td, iso_dates=True)
        expected = f'"{td.isoformat()}"'

        assert result == expected
Exemplo n.º 37
0
 def test_encode_dict_with_unicode_keys(self, unicode_key):
     unicode_dict = {unicode_key: "value1"}
     assert unicode_dict == ujson.decode(ujson.encode(unicode_dict))
Exemplo n.º 38
0
    def test_encode_with_decimal(self):
        decimal_input = 1.0
        output = ujson.encode(decimal_input)

        assert output == "1.0"
Exemplo n.º 39
0
 def test_encode_big_escape(self):
     # Make sure no Exception is raised.
     for _ in range(10):
         base = "\u00e5".encode()
         escape_input = base * 1024 * 1024 * 2
         ujson.encode(escape_input)
Exemplo n.º 40
0
 def test_bool(self, bool_input):
     b = bool(bool_input)
     assert ujson.decode(ujson.encode(b)) == b
Exemplo n.º 41
0
 def test_bool_array(self):
     bool_array = np.array(
         [True, False, True, True, False, True, False, False], dtype=bool)
     output = np.array(ujson.decode(ujson.encode(bool_array)), dtype=bool)
     tm.assert_numpy_array_equal(bool_array, output)
Exemplo n.º 42
0
    def test_int(self, any_int_dtype):
        klass = np.dtype(any_int_dtype).type
        num = klass(1)

        assert klass(ujson.decode(ujson.encode(num))) == num
Exemplo n.º 43
0
 def test_encode_time_conversion_pytz(self):
     # see gh-11473: to_json segfaults with timezone-aware datetimes
     test = datetime.time(10, 12, 15, 343243, pytz.utc)
     output = ujson.encode(test)
     expected = '"{iso}"'.format(iso=test.isoformat())
     assert expected == output
Exemplo n.º 44
0
    def test_double_long_numbers(self, long_number):
        sut = {"a": long_number}
        encoded = ujson.encode(sut, double_precision=15)

        decoded = ujson.decode(encoded)
        assert sut == decoded
Exemplo n.º 45
0
 def test_encode_time_conversion_dateutil(self):
     # see gh-11473: to_json segfaults with timezone-aware datetimes
     test = datetime.time(10, 12, 15, 343243, dateutil.tz.tzutc())
     output = ujson.encode(test)
     expected = f'"{test.isoformat()}"'
     assert expected == output
Exemplo n.º 46
0
 def test_encode_time_conversion_basic(self, test):
     output = ujson.encode(test)
     expected = f'"{test.isoformat()}"'
     assert expected == output
Exemplo n.º 47
0
 def test_encode_double_conversion(self, double_input):
     output = ujson.encode(double_input)
     assert round(double_input, 5) == round(json.loads(output), 5)
     assert round(double_input, 5) == round(ujson.decode(output), 5)
Exemplo n.º 48
0
    def test_array_reshaped(self, shape):
        arr = np.arange(100)
        arr = arr.reshape(shape)

        tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
        tm.assert_numpy_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)
Exemplo n.º 49
0
 def test_decimal_decode_test_precise(self):
     sut = {"a": 4.56}
     encoded = ujson.encode(sut)
     decoded = ujson.decode(encoded, precise_float=True)
     assert sut == decoded
Exemplo n.º 50
0
    def test_invalid_double_precision(self, invalid_val):
        double_input = 30.12345678901234567890
        expected_exception = ValueError if isinstance(invalid_val, int) else TypeError

        with pytest.raises(expected_exception):
            ujson.encode(double_input, double_precision=invalid_val)
Exemplo n.º 51
0
 def test_encode_builtin_values_conversion(self, builtin_value):
     output = ujson.encode(builtin_value)
     assert builtin_value == json.loads(output)
     assert output == json.dumps(builtin_value)
     assert builtin_value == ujson.decode(output)
Exemplo n.º 52
0
 def test_0d_array(self):
     # gh-18878
     msg = re.escape(
         "array(1) (0d array) is not JSON serializable at the moment")
     with pytest.raises(TypeError, match=msg):
         ujson.encode(np.array(1))
Exemplo n.º 53
0
    def test_array_basic(self):
        arr = np.arange(96)
        arr = arr.reshape((2, 2, 2, 2, 3, 2))

        tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
        tm.assert_numpy_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)
Exemplo n.º 54
0
    def test_encode_dict_conversion(self):
        dict_input = {"k1": 1, "k2": 2, "k3": 3, "k4": 4}
        output = ujson.encode(dict_input)

        assert dict_input == json.loads(output)
        assert dict_input == ujson.decode(output)
Exemplo n.º 55
0
 def test_0d_array(self):
     with pytest.raises(TypeError):
         ujson.encode(np.array(1))
Exemplo n.º 56
0
 def test_encode_num_conversion(self, num_input):
     output = ujson.encode(num_input)
     assert num_input == json.loads(output)
     assert output == json.dumps(num_input)
     assert num_input == ujson.decode(output)
Exemplo n.º 57
0
 def test_encode_numeric_overflow(self):
     with pytest.raises(OverflowError):
         ujson.encode(12839128391289382193812939)
Exemplo n.º 58
0
    def test_encode_unicode_conversion(self, unicode_input):
        enc = ujson.encode(unicode_input)
        dec = ujson.decode(enc)

        assert enc == json.dumps(unicode_input)
        assert dec == json.loads(enc)
Exemplo n.º 59
0
 def test_encode_time_conversion_basic(self, test):
     output = ujson.encode(test)
     expected = '"{iso}"'.format(iso=test.isoformat())
     assert expected == output
Exemplo n.º 60
0
    def test_float(self, float_dtype):
        klass = np.dtype(float_dtype).type
        num = klass(256.2013)

        assert klass(ujson.decode(ujson.encode(num))) == num