Пример #1
0
        def test_uni(center, radius=0x20):
            for i in range(center - radius, center + radius):
                uni = chr(i)
                u = kson.dumps(uni)
                u_ascii = kson.dumps(uni, ensure_ascii=True)
                u_no_ascii = kson.dumps(uni, ensure_ascii=False)

                assert u == u_no_ascii
                assert u_ascii == json.dumps(uni, ensure_ascii=True)
                assert u_no_ascii == json.dumps(uni, ensure_ascii=False)

                assert uni == json.loads(u)
                assert uni == kson.loads(u_ascii)
                assert uni == kson.loads(u_no_ascii)
Пример #2
0
def convert(opts):
    if not opts["j2j"]:
        in_schema, out_schema, ok = init_schemas(opts)

        if not ok:
            return 1

    in_data = read(opts)

    if opts["j2k"] or opts["j2j"]:
        in_data = json.loads(in_data)

    if opts["k2j"] or opts["k2k"]:
        in_data = kson.loads(in_data, in_schema["id"])

    indent = 4 if opts["--pretty"] else None

    if opts["k2j"] or opts["j2j"]:
        out_data = json.dumps(in_data, indent=indent)

    if opts["j2k"] or opts["k2k"]:
        out_data = kson.dumps(in_data, out_schema["id"], indent=indent)

    write(opts, out_data)
    return 0
Пример #3
0
def convert(opts):
    if not opts['j2j']:
        in_schema, out_schema, ok = init_schemas(opts)

        if not ok:
            return 1

    in_data = read(opts)

    if opts['j2k'] or opts['j2j']:
        in_data = json.loads(in_data)

    if opts['k2j'] or opts['k2k']:
        in_data = kson.loads(in_data, in_schema['id'])

    indent = 4 if opts['--pretty'] else None

    if opts['k2j'] or opts['j2j']:
        out_data = json.dumps(in_data, indent=indent)

    if opts['j2k'] or opts['k2k']:
        out_data = kson.dumps(in_data, out_schema['id'], indent=indent)

    write(opts, out_data)
    return 0
Пример #4
0
def test_basic_loads():
    kson.add_schema("""["schema", "basic", ["a", "b", "c"], [0, 0, "[]"]]""")
    data = kson.loads("""
        ["[]basic", "foo", "bar", ["biz", "baz"], "one", "two", [1,2,3,4,5]]
    """)

    assert len(data) == 2
    assert data[0] == {'a': "foo", 'b': "bar", 'c': ["biz", "baz"]}
    assert data[1] == {'a': "one", 'b': "two", 'c': [1, 2, 3, 4, 5]}
Пример #5
0
def load_all(text, expected=None):
    j, callbacks_j = loads(json.loads, text)
    k, callbacks_k = loads(kson.loads, text)
    j2 = json.loads(text)
    k2 = kson.loads(text)

    assert j == k == j2 == k2
    assert callbacks_j == callbacks_k
    if expected is not None:
        assert callbacks_k == expected
Пример #6
0
def test_nested_loads():
    kson.loads_schemas(FAMILY_SCHEMAS)
    data = kson.loads(FAMILY_KSON)
    par_field = data['par_field']
    kid_a = par_field['kid_a_field']
    kid_b = par_field['kid_b_field']

    assert len(data) == 1
    assert set(data.keys()) == set(['par_field'])
    assert set(par_field.keys()) == set(['kid_a_field', 'kid_b_field'])
    assert set(kid_a.keys()) == set(['a_field', 'a_arr_field'])
    assert set(kid_b.keys()) == set(['b_field', 'b_arr_field'])
Пример #7
0
    def test_unicode_bench(self):
        count = 64

        import time

        t = time.time()
        for i in range(count):
            uni = chr(i)
            u = kson.dumps(uni)
            json.loads(u)

        d1 = time.time() - t

        t = time.time()
        for i in range(count):
            uni = chr(i)
            u = kson.dumps(uni)
            kson.loads(u)
        d2 = time.time() - t

        ratio = d2 / d1
        assert ratio < 5
Пример #8
0
def test_codec_round_trip():
    kson.add_schema("""[
        "schema",
        "codec_test",
        ["c_field", "c_arr"],
        ["date|int36", "[]enum:a:b:c"]
    ]""")
    date = datetime(1955, 11, 5, 0, 0, 0)
    raw = kson.dumps({
        'c_field': date,
        'c_arr': ["a", "a", "b", "b", "c", "a", "b", "a", "no enum"]
    }, "codec_test")

    assert raw == '["codec_test","-7dzxc0",[1,1,2,2,3,1,2,1,"no enum"]]'
    assert kson.loads(raw)['c_field'] == date
Пример #9
0
    def test_nan(self):
        text = '[NaN, Infinity, -Infinity]'
        (nan_j, *rest_j), callbacks_j = loads(json.loads, text)
        (nan_k, *rest_k), callbacks_k = loads(kson.loads, text)
        nan_j2, *rest_j2 = json.loads(text)
        nan_k2, *rest_k2 = kson.loads(text)

        assert nan_j != nan_j
        assert nan_k != nan_k
        assert nan_j2 != nan_j2
        assert nan_k2 != nan_k2
        assert rest_j == rest_k
        assert rest_k == rest_j2
        assert rest_j2 == rest_k2
        assert callbacks_j == callbacks_k
Пример #10
0
    def test_object(self):
        results = []

        def object_hook(*x):
            results.append(("object_hook", x))
            return x

        text = '{"one": 1, "two": {"three": false}}'
        j = json.loads(text, object_hook=object_hook)
        results_j = results[:]
        results.clear()

        k = kson.loads(text, object_hook=object_hook)
        assert j == k
        assert results_j == results
Пример #11
0
    def test_non_finite_numbers(self):
        for i in '[nan, inf, -inf]', '[NaN, Infinity, -Infinity]':
            nan, inf, minus_inf = parse(i)
            assert nan != nan
            assert inf == float('inf')
            assert minus_inf == -inf

        for i in 'NAN', 'nAn', 'Inf', 'infinity', '--inf':
            with self.assertRaises(Exception):
                parse(i)

        for i in 'nan', 'NaN', 'inf', 'Infinity', '-inf', '-Infinity':
            x = kson.loads(i)
            s = kson.dumps(x)
            assert s
            with self.assertRaises(ValueError):
                kson.dumps(x, allow_nan=False)
Пример #12
0
def test_round_trip():
    kson.loads_schemas(FAMILY_SCHEMAS)
    family_obj = kson.loads(kson.dumps(FAMILY_OBJ, 'grand_parent'))
    assert data_eq(family_obj, FAMILY_OBJ)
Пример #13
0
def round_trip(x, pre, post=None, **kwargs):
    post = pre if post is None else post
    assert kson.loads(pre) == x
    assert kson.dumps(x, **kwargs) == post
    if not kwargs:
        assert json.dumps(x) == post