Example #1
0
 def test_tuple_array_dumps(self):
     t = (1, 2, 3)
     expect = pbjson.dumps(list(t))
     # Default is True
     self.assertEqual(expect, pbjson.dumps(t))
     # Ensure that the "default" does not get called
     self.assertEqual(expect, pbjson.dumps(t, convert=repr))
Example #2
0
 def assertRoundTrip(self, obj, other, use_for_json=True):
     if use_for_json is None:
         # None will use the default
         s = pbjson.dumps(obj)
     else:
         s = pbjson.dumps(obj, use_for_json=use_for_json)
     self.assertEqual(pbjson.loads(s), other)
Example #3
0
 def test_case(self):
     a = {'a': 1, 'c': 5, 'Jack': 'jill', 'pick': 'axe', 'Array': [1, 5, 6, 9], 'tuple': (83, 12, 3), 'crate': 'dog', 'zeak': 'oh'}
     self.assertEqual(
         b'\xe8\x05Array\xc4!\x01!\x05!\x06!\t\x04Jack\x84jill\x01a!\x01\x01c!\x05\x05crate\x83dog\x04pick\x83axe\x05tuple\xc3!S!\x0c!\x03\x04zeak\x82oh',
         pbjson.dumps(a, sort_keys=itemgetter(0)))
     self.assertEqual(
         b'\xe8\x01a!\x01\x05Array\xc4!\x01!\x05!\x06!\t\x01c!\x05\x05crate\x83dog\x04Jack\x84jill\x04pick\x83axe\x05tuple\xc3!S!\x0c!\x03\x04zeak\x82oh',
         pbjson.dumps(a, sort_keys=lambda kv: kv[0].lower()))
Example #4
0
 def assertRoundTrip(self, obj, other, use_for_json=True):
     if use_for_json is None:
         # None will use the default
         s = pbjson.dumps(obj)
     else:
         s = pbjson.dumps(obj, use_for_json=use_for_json)
     self.assertEqual(
         pbjson.loads(s),
         other)
Example #5
0
 def test_floats(self):
     assert_type = self.assertEqual if pbjson._has_encoder_speedups() else self.assertGreaterEqual
     for num in [
         Decimal("1617161771.7650001"),
         math.pi,
         math.pi ** 100,
         math.pi ** -100,
         3.1,
         3.1000000001,
         3.1000000002,
         3.10000000001,
         3.100000000001,
         3.1000000000001,
         3.10000000000001,
         0.00012345678901234572,
         0.00012345678901234574,
         0.00012345678901234576,
         0.00012345678901234578,
         152.79823,
         152.798229999999975,
         0.7,
     ]:
         encoded = pbjson.dumps(float(num))
         decoded = pbjson.loads(encoded)
         s = str(num)
         if s[0] == "0":
             s = s[1:]
         length = 1 + int((len(s) + 1) / 2)
         self.assertEqual(decoded, float(num))
         assert_type(length, len(encoded), num)
Example #6
0
 def test_tuple_array_dump(self):
     t = (1, 2, 3)
     expect = pbjson.dumps(list(t))
     # Default is True
     sio = BytesIO()
     pbjson.dump(t, sio)
     self.assertEqual(expect, sio.getvalue())
     # Ensure that the "default" does not get called
     sio = BytesIO()
     pbjson.dump(t, sio, convert=repr)
     self.assertEqual(expect, sio.getvalue())
Example #7
0
 def test_dumps(self):
     encoded = pbjson.dumps(
         {
             "countries": [
                 {"code": "us", "name": "United States"},
                 {"code": "ca", "name": "Canada"},
                 {"code": "mx", "name": "Mexico"}
             ],
             "region": 3,
         }, sort_keys=True)
     self.assertEqual(b'\xe2\x09countries\xc3\xe2\x04code\x82us\x04name\x8DUnited States\xe2\x81\x82ca\x82\x86Canada\xe2\x81\x82mx\x82\x86Mexico\x06region\x21\x03', encoded)
Example #8
0
 def test_floats(self):
     assert_type = self.assertEqual if pbjson._has_encoder_speedups() else self.assertGreaterEqual
     for num in [Decimal('1617161771.7650001'), math.pi, math.pi ** 100,
                 math.pi ** -100, 3.1, 3.1000000001, 3.1000000002, 3.10000000001, 3.100000000001, 3.1000000000001, 3.10000000000001, 0.00012345678901234572, 0.00012345678901234574, 0.00012345678901234576, 0.00012345678901234578, 152.79823, 152.798229999999975, 0.7]:
         encoded = pbjson.dumps(float(num))
         decoded = pbjson.loads(encoded)
         s = str(num)
         if s[0] == '0':
             s = s[1:]
         length = 1 + int((len(s) + 1) / 2)
         self.assertEqual(decoded, float(num))
         assert_type(length, len(encoded), num)
Example #9
0
 def test_european(self):
     original = ['Politique de Confidentialité', 'Política de Privacidad']
     pbj = pbjson.dumps(original)
     round_trip = pbjson.loads(pbj)
     self.assertEqual(original, round_trip)
     with open('temp.pbj', 'wb') as f:
         f.write(pbj)
     with open('temp.pbj', 'rb') as f:
         fpbj = f.read()
     self.assertEqual(pbj, fpbj)
     with open('temp.pbj', 'rb') as f:
         round_trip = pbjson.load(f)
     self.assertEqual(original, round_trip)
Example #10
0
 def test_asian(self):
     original = ['その子は絶えずくすくす笑っていた', 'お役に立てば幸いです。']
     pbj = pbjson.dumps(original)
     round_trip = pbjson.loads(pbj)
     self.assertEqual(original, round_trip)
     with open('temp.pbj', 'wb') as f:
         f.write(pbj)
     with open('temp.pbj', 'rb') as f:
         fpbj = f.read()
     self.assertEqual(pbj, fpbj)
     with open('temp.pbj', 'rb') as f:
         round_trip = pbjson.load(f)
     self.assertEqual(original, round_trip)
Example #11
0
 def test_european(self):
     original = ['Politique de Confidentialité', 'Política de Privacidad']
     pbj = pbjson.dumps(original)
     round_trip = pbjson.loads(pbj)
     self.assertEqual(original, round_trip)
     with open('temp.pbj', 'wb') as f:
         f.write(pbj)
     with open('temp.pbj', 'rb') as f:
         fpbj = f.read()
     self.assertEqual(pbj, fpbj)
     with open('temp.pbj', 'rb') as f:
         round_trip = pbjson.load(f)
     self.assertEqual(original, round_trip)
Example #12
0
 def test_asian(self):
     original = ['その子は絶えずくすくす笑っていた', 'お役に立てば幸いです。']
     pbj = pbjson.dumps(original)
     round_trip = pbjson.loads(pbj)
     self.assertEqual(original, round_trip)
     with open('temp.pbj', 'wb') as f:
         f.write(pbj)
     with open('temp.pbj', 'rb') as f:
         fpbj = f.read()
     self.assertEqual(pbj, fpbj)
     with open('temp.pbj', 'rb') as f:
         round_trip = pbjson.load(f)
     self.assertEqual(original, round_trip)
Example #13
0
 def test_simple_first(self):
     a = CaseInsensitiveDict({
         'a': 1,
         'c': 5,
         'jack': 'jill',
         'pick': 'axe',
         'array': [1, 5, 6, 9],
         'tuple': (83, 12, 3),
         'crate': 'dog',
         'zeak': 'oh'
     })
     self.assertEqual(
         b'\xe8\x01a!\x01\x01c!\x05\x05crate\x83dog\x04jack\x84jill\x04pick\x83axe\x04zeak\x82oh\x05array\xc4!\x01!\x05!\x06!\t\x05tuple\xc3!S!\x0c!\x03',
         pbjson.dumps(a, sort_keys=pbjson.simple_first))
Example #14
0
 def test_dumps(self):
     encoded = pbjson.dumps(
         {
             "countries": [
                 {"code": "us", "name": "United States"},
                 {"code": "ca", "name": "Canada"},
                 {"code": "mx", "name": "Mexico"},
             ],
             "region": 3,
         },
         sort_keys=True,
     )
     self.assertEqual(
         b"\xe2\x09countries\xc3\xe2\x04code\x82us\x04name\x8DUnited States\xe2\x81\x82ca\x82\x86Canada\xe2\x81\x82mx\x82\x86Mexico\x06region\x21\x03",
         encoded,
     )
Example #15
0
    def test_speed(self):
        if pbjson._has_decoder_speedups():
            encoded = json.dumps(sample)
            start = time()
            for i in range(100):
                json.loads(encoded)
            json_time = time() - start

            encoded = pbjson.dumps(sample)
            start = time()
            for i in range(100):
                pbjson.loads(encoded)
            binary_json_time = time() - start

            # noinspection PyUnboundLocalVariable
            # print('\nPBJSON: {} seconds\nJSON: {} seconds ({}%)\nMarshal: {} seconds ({}%)\nPickle: {} seconds ({}%)'.format(binary_json_time, json_time, int(json_time / binary_json_time * 100), marshal_time, int(marshal_time / binary_json_time * 100), pickle_time, int(pickle_time / binary_json_time * 100)))
            self.assertLess(binary_json_time, json_time)
Example #16
0
 def test_custom_datetime_with_decode(self):
     a = datetime(2000, 3, 17, 11, 21, 45)
     p = pbjson.dumps(a, custom=((datetime, encode_date_string),))
     self.assertEqual(a, pbjson.loads(p, custom=decode_date_string))
Example #17
0
 def test_custom_datetime_with_encode(self):
     self.assertEqual(
         b"\x0e\x90\x132000-03-17 11:21:45",
         pbjson.dumps(datetime(2000, 3, 17, 11, 21, 45), custom=(datetime, encode_date_string)),
     )
Example #18
0
 def test_parse(self):
     # test in/out equivalence and parsing
     encoded = pbjson.dumps(JSON, skip_illegal_keys=True)
     decoded = pbjson.loads(encoded)
     self.assertEqual(JSON, decoded)
Example #19
0
 def test_degenerates_allow(self):
     for inf in (float("inf"), float("-inf")):
         self.assertEqual(pbjson.loads(pbjson.dumps(inf)), inf)
     # Python 2.5 doesn't have math.isnan
     nan = pbjson.loads(pbjson.dumps(float("nan")))
     self.assertTrue((0 + nan) != nan)
Example #20
0
 def test_int_roundtrip(self):
     encoded = pbjson.dumps(1234567890)
     decoded = pbjson.loads(encoded)
     self.assertEqual(1234567890, decoded)
Example #21
0
 def test_int_roundtrip(self):
     encoded = pbjson.dumps(1234567890)
     decoded = pbjson.loads(encoded)
     self.assertEqual(1234567890, decoded)
 def test_simple_first(self):
     a = {'a': 1, 'c': 5, 'jack': 'jill', 'pick': 'axe', 'array': [1, 5, 6, 9], 'tuple': (83, 12, 3), 'crate': 'dog', 'zeak': 'oh'}
     self.assertEqual(
         b'\xe8\x01a!\x01\x01c!\x05\x05crate\x83dog\x04jack\x84jill\x04pick\x83axe\x04zeak\x82oh\x05array\xc4!\x01!\x05!\x06!\t\x05tuple\xc3!S!\x0c!\x03',
         pbjson.dumps(a, sort_keys=pbjson.simple_first))
Example #23
0
    # v = 3.100000000001
    # a = pbjson.dumps(v)
    # b = pbjson.loads(a)
    # s = repr(v)
    # if s[0] == '0':
    #     s = s[1:]
    # length = 1 + int((len(s)+1)/2)
    # print(v, b, v - b if v-b else '', 'Bad length {}, expected {}'.format(len(a), length) if len(a) != length else '')

    iterations = 10000
    start = time()
    for i in range(iterations):
        json_size = len(json.dumps(sample))
    json_time = time() - start

    size = len(pbjson.dumps(sample))
    start = time()
    for i in range(iterations):
        binary_json_size = len(pbjson.dumps(sample))
        # self.assertEqual(size, binary_json_size)
    binary_json_time = time() - start

    start = time()
    for i in range(iterations):
        marshal_size = len(marshal.dumps(sample))
    marshal_time = time() - start

    start = time()
    for i in range(iterations):
        pickle_size = len(pickle.dumps(sample))
    pickle_time = time() - start
Example #24
0
 def test_e_roundtrip(self):
     encoded = pbjson.dumps(0.123456789e-12)
     decoded = pbjson.loads(encoded)
     self.assertEqual(0.123456789e-12, decoded)
Example #25
0
 def test_ints(self):
     for num in [1, long_type(1), 1 << 32, 1 << 64]:
         self.assertEqual(pbjson.loads(pbjson.dumps(num)), num)
Example #26
0
 def test_ints(self):
     for num in [1, long_type(1), 1 << 32, 1 << 64]:
         self.assertEqual(pbjson.loads(pbjson.dumps(num)), num)
Example #27
0
 def test_e_roundtrip(self):
     encoded = pbjson.dumps(0.123456789e-12)
     decoded = pbjson.loads(encoded)
     self.assertEqual(0.123456789e-12, decoded)
Example #28
0
 def test_parse(self):
     # test in/out equivalence and parsing
     encoded = pbjson.dumps(JSON, skip_illegal_keys=True)
     decoded = pbjson.loads(encoded)
     self.assertEqual(JSON, decoded)
Example #29
0
 def test_custom_datetime_with_pickle(self):
     a = datetime(2000, 3, 17, 11, 21, 45)
     p = pbjson.dumps(a, custom=((datetime, pickle.dumps),))
     self.assertEqual(a, pbjson.loads(p, custom=pickle.loads))
Example #30
0
 def test_custom_datetime_with_pickle(self):
     a = datetime(2000, 3, 17, 11, 21, 45)
     p = pbjson.dumps(a, custom=((datetime, pickle.dumps),))
     self.assertEqual(a, pbjson.loads(p, custom=pickle.loads))
Example #31
0
 def test_parse(self):
     # test in/out equivalence and parsing
     encoded = pbjson.dumps(JSON)
     decoded = pbjson.loads(encoded)
     self.assertEqual(JSON, decoded)
Example #32
0
 def test_default(self):
     self.assertEqual(pbjson.dumps(type, convert=repr),
                      pbjson.dumps(repr(type)))
Example #33
0
 def test_custom_datetime_with_encode(self):
     self.assertEqual(b'\x0e\x90\x132000-03-17 11:21:45', pbjson.dumps(datetime(2000, 3, 17, 11, 21, 45), custom=(datetime, encode_date_string)))
Example #34
0
 def test_custom_datetime_with_decode(self):
     a = datetime(2000, 3, 17, 11, 21, 45)
     p = pbjson.dumps(a, custom=((datetime, encode_date_string),))
     self.assertEqual(a, pbjson.loads(p, custom=decode_date_string))
Example #35
0
            binary_json_time = time() - start

            # noinspection PyUnboundLocalVariable
            # print('\nPBJSON: {} seconds\nJSON: {} seconds ({}%)\nMarshal: {} seconds ({}%)\nPickle: {} seconds ({}%)'.format(binary_json_time, json_time, int(json_time / binary_json_time * 100), marshal_time, int(marshal_time / binary_json_time * 100), pickle_time, int(pickle_time / binary_json_time * 100)))
            self.assertLess(binary_json_time, json_time)


if __name__ == '__main__':
    iterations = 10000
    encoded = json.dumps(sample)
    start = time()
    for i in range(iterations):
        json.loads(encoded)
    json_time = time() - start

    encoded = pbjson.dumps(sample)
    start = time()
    for i in range(iterations):
        pbjson.loads(encoded)
    binary_json_time = time() - start

    encoded = marshal.dumps(sample)
    start = time()
    for i in range(iterations):
        marshal.loads(encoded)
    marshal_time = time() - start

    encoded = pickle.dumps(sample)
    start = time()
    for i in range(iterations):
        pickle.loads(encoded)
Example #36
0
 def test_parse(self):
     # test in/out equivalence and parsing
     encoded = pbjson.dumps(JSON)
     decoded = pbjson.loads(encoded)
     self.assertEqual(JSON, decoded)
Example #37
0
 def process_bind_param(self, value, dialect):
     if value is None:
         return '{}'
     else:
         return pbjson.dumps(value)
Example #38
0
 def test_default(self):
     self.assertEqual(
         pbjson.dumps(type, convert=repr),
         pbjson.dumps(repr(type)))
Example #39
0
 def test_degenerates_allow(self):
     for inf in (float('inf'), float('-inf')):
         self.assertEqual(pbjson.loads(pbjson.dumps(inf)), inf)
     # Python 2.5 doesn't have math.isnan
     nan = pbjson.loads(pbjson.dumps(float('nan')))
     self.assertTrue((0 + nan) != nan)
Example #40
0
    # v = 3.100000000001
    # a = pbjson.dumps(v)
    # b = pbjson.loads(a)
    # s = repr(v)
    # if s[0] == '0':
    #     s = s[1:]
    # length = 1 + int((len(s)+1)/2)
    # print(v, b, v - b if v-b else '', 'Bad length {}, expected {}'.format(len(a), length) if len(a) != length else '')

    iterations = 10000
    start = time()
    for i in range(iterations):
        json_size = len(json.dumps(sample))
    json_time = time() - start

    size = len(pbjson.dumps(sample))
    start = time()
    for i in range(iterations):
        binary_json_size = len(pbjson.dumps(sample))
        # self.assertEqual(size, binary_json_size)
    binary_json_time = time() - start

    start = time()
    for i in range(iterations):
        marshal_size = len(marshal.dumps(sample))
    marshal_time = time() - start

    start = time()
    for i in range(iterations):
        pickle_size = len(pickle.dumps(sample))
    pickle_time = time() - start