bjdata 0.4.1__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bjdata-0.4.1 → bjdata-0.5.0}/PKG-INFO +9 -8
- {bjdata-0.4.1 → bjdata-0.5.0}/README.md +8 -7
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata/__init__.py +1 -1
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata/decoder.py +43 -30
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata/encoder.py +28 -23
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata/markers.py +1 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata.egg-info/PKG-INFO +9 -8
- {bjdata-0.4.1 → bjdata-0.5.0}/setup.py +1 -1
- {bjdata-0.4.1 → bjdata-0.5.0}/src/_bjdata.c +16 -16
- {bjdata-0.4.1 → bjdata-0.5.0}/src/decoder.c +23 -3
- {bjdata-0.4.1 → bjdata-0.5.0}/src/decoder.h +2 -1
- {bjdata-0.4.1 → bjdata-0.5.0}/src/encoder.c +1 -1
- {bjdata-0.4.1 → bjdata-0.5.0}/src/encoder.h +1 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/src/markers.h +1 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/test/test.py +8 -5
- {bjdata-0.4.1 → bjdata-0.5.0}/Binary_JData_Specification.md +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/CHANGELOG +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/LICENSE +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/MANIFEST.in +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/NOTICE +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/UBJSON-Specification.md +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata/__main__.py +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata/compat.py +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata.egg-info/SOURCES.txt +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata.egg-info/dependency_links.txt +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata.egg-info/not-zip-safe +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata.egg-info/requires.txt +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/bjdata.egg-info/top_level.txt +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/ez_setup.py +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/pyproject.toml +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/setup.cfg +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/src/common.h +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/src/python_funcs.c +0 -0
- {bjdata-0.4.1 → bjdata-0.5.0}/src/python_funcs.h +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: bjdata
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Binary JData and UBJSON encoder/decoder
|
|
5
5
|
Home-page: https://github.com/NeuroJSON/pybj
|
|
6
6
|
Author: Qianqian Fang
|
|
@@ -37,19 +37,19 @@ Requires-Dist: coverage<4.6,>=4.5.3; extra == "dev"
|
|
|
37
37
|
|
|
38
38
|
# Binary JData for Python - a lightweight binary JSON format
|
|
39
39
|
|
|
40
|
-
- Copyright: (C) Qianqian Fang (2020-
|
|
40
|
+
- Copyright: (C) Qianqian Fang (2020-2025) <q.fang at neu.edu>
|
|
41
41
|
- Copyright: (C) Iotic Labs Ltd. (2016-2019) <vilnis.termanis at iotic-labs.com>
|
|
42
42
|
- License: Apache License, Version 2.0
|
|
43
|
-
- Version: 0.
|
|
43
|
+
- Version: 0.5.0
|
|
44
44
|
- URL: https://pypi.org/project/bjdata/
|
|
45
45
|
- Github: https://github.com/NeuroJSON/pybj
|
|
46
|
-
- BJData Spec Version: [V1 Draft
|
|
46
|
+
- BJData Spec Version: [V1 Draft 3](https://neurojson.org/bjdata/draft3)
|
|
47
47
|
- Acknowledgement: This project is supported by US National Institute of Health (NIH) grant U24-NS124027
|
|
48
48
|
|
|
49
49
|
[](https://travis-ci.com/NeuroJSON/pybj)
|
|
50
50
|
|
|
51
51
|
This is a Python v3.2+ (and 2.7+) [Binary JData](https://neurojson.org) encoder
|
|
52
|
-
and decoder based on the [Draft-
|
|
52
|
+
and decoder based on the [Draft-3](Binary_JData_Specification.md) specification.
|
|
53
53
|
|
|
54
54
|
## Installing / packaging
|
|
55
55
|
```shell
|
|
@@ -172,12 +172,13 @@ This package was modified based on the py-ubjson package developed by
|
|
|
172
172
|
Project URL: https://github.com/Iotic-Labs/py-ubjson
|
|
173
173
|
|
|
174
174
|
The major changes were focused on supporting the Binary JData Specification
|
|
175
|
-
[Draft
|
|
175
|
+
[Draft 3](https://neurojson.org/bjdata/draft3) -
|
|
176
176
|
an extended Universal Binary JSON (UBJSON) Specification Draft-12 by adding
|
|
177
177
|
the below new features:
|
|
178
178
|
|
|
179
|
-
* BJData adds
|
|
179
|
+
* BJData adds 5 new data types: `uint16 [u]`, `uint32 [m]`, `uint64 [M]`, `float16 [h]`, and `byte [B]`
|
|
180
|
+
* BJData adds a dedicated byte data type used in optimized array containers for binary data
|
|
180
181
|
* BJData supports an optimized ND array container
|
|
181
182
|
* BJData does not convert NaN/Inf/-Inf to `null`
|
|
182
183
|
* BJData uses little-Endian as the default integer/floating-point numbers while UBJSON uses big-Endian
|
|
183
|
-
* BJData only permits non-zero-fixed-length data types (`
|
|
184
|
+
* BJData only permits non-zero-fixed-length data types (`UiuImlMLhdDCB`) in strongly-typed array/object containers
|
|
@@ -2,19 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
# Binary JData for Python - a lightweight binary JSON format
|
|
4
4
|
|
|
5
|
-
- Copyright: (C) Qianqian Fang (2020-
|
|
5
|
+
- Copyright: (C) Qianqian Fang (2020-2025) <q.fang at neu.edu>
|
|
6
6
|
- Copyright: (C) Iotic Labs Ltd. (2016-2019) <vilnis.termanis at iotic-labs.com>
|
|
7
7
|
- License: Apache License, Version 2.0
|
|
8
|
-
- Version: 0.
|
|
8
|
+
- Version: 0.5.0
|
|
9
9
|
- URL: https://pypi.org/project/bjdata/
|
|
10
10
|
- Github: https://github.com/NeuroJSON/pybj
|
|
11
|
-
- BJData Spec Version: [V1 Draft
|
|
11
|
+
- BJData Spec Version: [V1 Draft 3](https://neurojson.org/bjdata/draft3)
|
|
12
12
|
- Acknowledgement: This project is supported by US National Institute of Health (NIH) grant U24-NS124027
|
|
13
13
|
|
|
14
14
|
[](https://travis-ci.com/NeuroJSON/pybj)
|
|
15
15
|
|
|
16
16
|
This is a Python v3.2+ (and 2.7+) [Binary JData](https://neurojson.org) encoder
|
|
17
|
-
and decoder based on the [Draft-
|
|
17
|
+
and decoder based on the [Draft-3](Binary_JData_Specification.md) specification.
|
|
18
18
|
|
|
19
19
|
## Installing / packaging
|
|
20
20
|
```shell
|
|
@@ -137,12 +137,13 @@ This package was modified based on the py-ubjson package developed by
|
|
|
137
137
|
Project URL: https://github.com/Iotic-Labs/py-ubjson
|
|
138
138
|
|
|
139
139
|
The major changes were focused on supporting the Binary JData Specification
|
|
140
|
-
[Draft
|
|
140
|
+
[Draft 3](https://neurojson.org/bjdata/draft3) -
|
|
141
141
|
an extended Universal Binary JSON (UBJSON) Specification Draft-12 by adding
|
|
142
142
|
the below new features:
|
|
143
143
|
|
|
144
|
-
* BJData adds
|
|
144
|
+
* BJData adds 5 new data types: `uint16 [u]`, `uint32 [m]`, `uint64 [M]`, `float16 [h]`, and `byte [B]`
|
|
145
|
+
* BJData adds a dedicated byte data type used in optimized array containers for binary data
|
|
145
146
|
* BJData supports an optimized ND array container
|
|
146
147
|
* BJData does not convert NaN/Inf/-Inf to `null`
|
|
147
148
|
* BJData uses little-Endian as the default integer/floating-point numbers while UBJSON uses big-Endian
|
|
148
|
-
* BJData only permits non-zero-fixed-length data types (`
|
|
149
|
+
* BJData only permits non-zero-fixed-length data types (`UiuImlMLhdDCB`) in strongly-typed array/object containers
|
|
@@ -38,6 +38,6 @@ except ImportError: # pragma: no cover
|
|
|
38
38
|
from .encoder import EncoderException
|
|
39
39
|
from .decoder import DecoderException
|
|
40
40
|
|
|
41
|
-
__version__ = '0.
|
|
41
|
+
__version__ = '0.5.0'
|
|
42
42
|
|
|
43
43
|
__all__ = ('EXTENSION_ENABLED', 'dump', 'dumpb', 'EncoderException', 'load', 'loadb', 'DecoderException')
|
|
@@ -22,20 +22,21 @@ from decimal import Decimal, DecimalException
|
|
|
22
22
|
from functools import reduce
|
|
23
23
|
|
|
24
24
|
from .compat import raise_from, intern_unicode
|
|
25
|
-
from .markers import (TYPE_NONE, TYPE_NULL, TYPE_NOOP, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_INT8, TYPE_UINT8,
|
|
25
|
+
from .markers import (TYPE_NONE, TYPE_NULL, TYPE_NOOP, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_BYTE, TYPE_INT8, TYPE_UINT8,
|
|
26
26
|
TYPE_INT16, TYPE_INT32, TYPE_INT64, TYPE_FLOAT32, TYPE_FLOAT64, TYPE_HIGH_PREC, TYPE_CHAR,
|
|
27
27
|
TYPE_UINT16, TYPE_UINT32, TYPE_UINT64, TYPE_FLOAT16,
|
|
28
28
|
TYPE_STRING, OBJECT_START, OBJECT_END, ARRAY_START, ARRAY_END, CONTAINER_TYPE, CONTAINER_COUNT)
|
|
29
29
|
from numpy import array as ndarray, dtype as npdtype, frombuffer as buffer2numpy, half as halfprec
|
|
30
30
|
from array import array as typedarray
|
|
31
31
|
|
|
32
|
-
__TYPES = frozenset((TYPE_NULL, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
|
|
33
|
-
TYPE_INT64, TYPE_FLOAT32, TYPE_FLOAT64, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64,
|
|
34
|
-
|
|
32
|
+
__TYPES = frozenset((TYPE_NULL, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_BYTE, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
|
|
33
|
+
TYPE_INT32, TYPE_INT64, TYPE_FLOAT32, TYPE_FLOAT64, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64,
|
|
34
|
+
TYPE_FLOAT16, TYPE_HIGH_PREC, TYPE_CHAR, TYPE_STRING, ARRAY_START, OBJECT_START))
|
|
35
35
|
__TYPES_NO_DATA = frozenset((TYPE_NULL, TYPE_BOOL_FALSE, TYPE_BOOL_TRUE))
|
|
36
|
-
__TYPES_INT = frozenset((TYPE_INT8, TYPE_UINT8, TYPE_INT16, TYPE_INT32, TYPE_INT64, TYPE_UINT16,
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
__TYPES_INT = frozenset((TYPE_BYTE, TYPE_INT8, TYPE_UINT8, TYPE_INT16, TYPE_INT32, TYPE_INT64, TYPE_UINT16,
|
|
37
|
+
TYPE_UINT32, TYPE_UINT64))
|
|
38
|
+
__TYPES_FIXLEN = frozenset((TYPE_BYTE, TYPE_INT8, TYPE_UINT8, TYPE_INT16, TYPE_INT32, TYPE_INT64, TYPE_UINT16, TYPE_UINT32,
|
|
39
|
+
TYPE_UINT64, TYPE_FLOAT16, TYPE_FLOAT32, TYPE_FLOAT64, TYPE_CHAR))
|
|
39
40
|
|
|
40
41
|
__SMALL_INTS_DECODED = [{pack('>b', i): i for i in range(-128, 128)}, {pack('<b', i): i for i in range(-128, 128)}]
|
|
41
42
|
__SMALL_UINTS_DECODED = [{pack('>B', i): i for i in range(256)}, {pack('<B', i): i for i in range(256)}]
|
|
@@ -49,7 +50,8 @@ __UNPACK_FLOAT16 = [Struct('>h').unpack, Struct('<h').unpack]
|
|
|
49
50
|
__UNPACK_FLOAT32 = [Struct('>f').unpack, Struct('<f').unpack]
|
|
50
51
|
__UNPACK_FLOAT64 = [Struct('>d').unpack, Struct('<d').unpack]
|
|
51
52
|
|
|
52
|
-
__DTYPE_MAP = {
|
|
53
|
+
__DTYPE_MAP = { TYPE_BYTE: 'B',
|
|
54
|
+
TYPE_INT8: 'b',
|
|
53
55
|
TYPE_UINT8: 'B',
|
|
54
56
|
TYPE_INT16: 'h',
|
|
55
57
|
TYPE_UINT16: 'H',
|
|
@@ -62,7 +64,8 @@ __DTYPE_MAP = { TYPE_INT8: 'b',
|
|
|
62
64
|
TYPE_FLOAT64: 'd',
|
|
63
65
|
TYPE_CHAR: 'c'}
|
|
64
66
|
|
|
65
|
-
__DTYPELEN_MAP={
|
|
67
|
+
__DTYPELEN_MAP={ TYPE_BYTE: 1,
|
|
68
|
+
TYPE_INT8: 1,
|
|
66
69
|
TYPE_UINT8: 1,
|
|
67
70
|
TYPE_INT16: 2,
|
|
68
71
|
TYPE_UINT16: 2,
|
|
@@ -114,6 +117,11 @@ def __decode_int_non_negative(fp_read, marker, le=1):
|
|
|
114
117
|
raise DecoderException('Negative count/length unexpected')
|
|
115
118
|
return value
|
|
116
119
|
|
|
120
|
+
def __decode_byte(fp_read, marker, le=1):
|
|
121
|
+
try:
|
|
122
|
+
return __SMALL_UINTS_DECODED[le][fp_read(1)]
|
|
123
|
+
except KeyError as ex:
|
|
124
|
+
raise_from(DecoderException('Failed to unpack byte'), ex)
|
|
117
125
|
|
|
118
126
|
def __decode_int8(fp_read, marker, le=1):
|
|
119
127
|
try:
|
|
@@ -227,6 +235,7 @@ def __decode_object_key(fp_read, marker, intern_object_keys, le=1):
|
|
|
227
235
|
__METHOD_MAP = {TYPE_NULL: (lambda _, __, ___: None),
|
|
228
236
|
TYPE_BOOL_TRUE: (lambda _, __, ___: True),
|
|
229
237
|
TYPE_BOOL_FALSE: (lambda _, __, ___: False),
|
|
238
|
+
TYPE_BYTE: __decode_byte,
|
|
230
239
|
TYPE_INT8: __decode_int8,
|
|
231
240
|
TYPE_UINT8: __decode_uint8,
|
|
232
241
|
TYPE_INT16: __decode_int16,
|
|
@@ -248,7 +257,7 @@ def prodlist(mylist):
|
|
|
248
257
|
result = result * x
|
|
249
258
|
return result
|
|
250
259
|
|
|
251
|
-
def __get_container_params(fp_read, in_mapping, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle):
|
|
260
|
+
def __get_container_params(fp_read, in_mapping, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle):
|
|
252
261
|
marker = fp_read(1)
|
|
253
262
|
dims = []
|
|
254
263
|
if marker == CONTAINER_TYPE:
|
|
@@ -262,7 +271,7 @@ def __get_container_params(fp_read, in_mapping, no_bytes, object_hook, object_pa
|
|
|
262
271
|
if marker == CONTAINER_COUNT:
|
|
263
272
|
marker = fp_read(1)
|
|
264
273
|
if marker == ARRAY_START:
|
|
265
|
-
dims = __decode_array(fp_read, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
274
|
+
dims = __decode_array(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
266
275
|
count = prodlist(dims)
|
|
267
276
|
else:
|
|
268
277
|
count = __decode_int_non_negative(fp_read, marker, islittle)
|
|
@@ -270,7 +279,7 @@ def __get_container_params(fp_read, in_mapping, no_bytes, object_hook, object_pa
|
|
|
270
279
|
|
|
271
280
|
# special cases (no data (None or bool) / bytes array) will be handled in calling functions
|
|
272
281
|
if not (type_ in __TYPES_NO_DATA or
|
|
273
|
-
(type_ == TYPE_UINT8 and not in_mapping and not no_bytes)):
|
|
282
|
+
(type_ == (TYPE_UINT8 if uint8_bytes else TYPE_BYTE) and not in_mapping and not no_bytes)):
|
|
274
283
|
# Reading ahead is just to capture type, which will not exist if type is fixed
|
|
275
284
|
marker = fp_read(1) if (in_mapping or type_ == TYPE_NONE) else type_
|
|
276
285
|
|
|
@@ -283,9 +292,9 @@ def __get_container_params(fp_read, in_mapping, no_bytes, object_hook, object_pa
|
|
|
283
292
|
return marker, counting, count, type_, dims
|
|
284
293
|
|
|
285
294
|
|
|
286
|
-
def __decode_object(fp_read, no_bytes, object_hook, object_pairs_hook, # pylint: disable=too-many-branches
|
|
295
|
+
def __decode_object(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, # pylint: disable=too-many-branches
|
|
287
296
|
intern_object_keys, islittle):
|
|
288
|
-
marker, counting, count, type_, dims = __get_container_params(fp_read, True, no_bytes,object_hook, object_pairs_hook,intern_object_keys, islittle)
|
|
297
|
+
marker, counting, count, type_, dims = __get_container_params(fp_read, True, no_bytes, uint8_bytes, object_hook, object_pairs_hook,intern_object_keys, islittle)
|
|
289
298
|
has_pairs_hook = object_pairs_hook is not None
|
|
290
299
|
obj = [] if has_pairs_hook else {}
|
|
291
300
|
|
|
@@ -323,9 +332,9 @@ def __decode_object(fp_read, no_bytes, object_hook, object_pairs_hook, # pylint
|
|
|
323
332
|
# handle outside above except (on KeyError) so do not have unfriendly "exception within except" backtrace
|
|
324
333
|
if not handled:
|
|
325
334
|
if marker == ARRAY_START:
|
|
326
|
-
value = __decode_array(fp_read, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
335
|
+
value = __decode_array(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
327
336
|
elif marker == OBJECT_START:
|
|
328
|
-
value = __decode_object(fp_read, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
337
|
+
value = __decode_object(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
329
338
|
else:
|
|
330
339
|
raise DecoderException('Invalid marker within object')
|
|
331
340
|
|
|
@@ -341,15 +350,15 @@ def __decode_object(fp_read, no_bytes, object_hook, object_pairs_hook, # pylint
|
|
|
341
350
|
return object_pairs_hook(obj) if has_pairs_hook else object_hook(obj)
|
|
342
351
|
|
|
343
352
|
|
|
344
|
-
def __decode_array(fp_read, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle):
|
|
345
|
-
marker, counting, count, type_, dims = __get_container_params(fp_read, False, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
353
|
+
def __decode_array(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle):
|
|
354
|
+
marker, counting, count, type_, dims = __get_container_params(fp_read, False, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
346
355
|
|
|
347
356
|
# special case - no data (None or bool)
|
|
348
357
|
if type_ in __TYPES_NO_DATA:
|
|
349
358
|
return [__METHOD_MAP[type_](fp_read, type_, islittle)] * count
|
|
350
359
|
|
|
351
360
|
# special case - bytes array
|
|
352
|
-
if type_ == TYPE_UINT8 and not no_bytes and len(dims)==0:
|
|
361
|
+
if type_ == (TYPE_UINT8 if uint8_bytes else TYPE_BYTE) and not no_bytes and len(dims)==0:
|
|
353
362
|
container = fp_read(count)
|
|
354
363
|
if len(container) < count:
|
|
355
364
|
raise DecoderException('Container bytes array too short')
|
|
@@ -388,9 +397,9 @@ def __decode_array(fp_read, no_bytes, object_hook, object_pairs_hook, intern_obj
|
|
|
388
397
|
# handle outside above except (on KeyError) so do not have unfriendly "exception within except" backtrace
|
|
389
398
|
if not handled:
|
|
390
399
|
if marker == ARRAY_START:
|
|
391
|
-
value = __decode_array(fp_read, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
400
|
+
value = __decode_array(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
392
401
|
elif marker == OBJECT_START:
|
|
393
|
-
value = __decode_object(fp_read, no_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
402
|
+
value = __decode_object(fp_read, no_bytes, uint8_bytes, object_hook, object_pairs_hook, intern_object_keys, islittle)
|
|
394
403
|
else:
|
|
395
404
|
raise DecoderException('Invalid marker within array')
|
|
396
405
|
|
|
@@ -411,14 +420,18 @@ def __object_hook_noop(obj):
|
|
|
411
420
|
return obj
|
|
412
421
|
|
|
413
422
|
|
|
414
|
-
def load(fp, no_bytes=False, object_hook=None, object_pairs_hook=None, intern_object_keys=False, islittle=True):
|
|
423
|
+
def load(fp, no_bytes=False, uint8_bytes=False, object_hook=None, object_pairs_hook=None, intern_object_keys=False, islittle=True):
|
|
415
424
|
"""Decodes and returns BJData/UBJSON from the given file-like object
|
|
416
425
|
|
|
417
426
|
Args:
|
|
418
427
|
fp: read([size])-able object
|
|
419
|
-
no_bytes (bool): If set, typed UBJSON arrays (
|
|
428
|
+
no_bytes (bool): If set, typed UBJSON arrays (byte) will not be
|
|
420
429
|
converted to a bytes instance and instead treated like
|
|
421
430
|
any other array (i.e. result in a list).
|
|
431
|
+
uint8_bytes (bool): If set, typed UBJSON arrays (uint8) will be
|
|
432
|
+
converted to a bytes instance instead of being
|
|
433
|
+
treated as an array (for UBJSON & BJData Draft 2).
|
|
434
|
+
Ignored if no_bytes is set.
|
|
422
435
|
object_hook (callable): Called with the result of any object literal
|
|
423
436
|
decoded (instead of dict).
|
|
424
437
|
object_pairs_hook (callable): Called with the result of any object
|
|
@@ -432,7 +445,7 @@ def load(fp, no_bytes=False, object_hook=None, object_pairs_hook=None, intern_ob
|
|
|
432
445
|
to unicode) and wil be ignored.
|
|
433
446
|
islittle (1 or 0): default is 1 for little-endian for all numerics (for
|
|
434
447
|
BJData Draft 2), change to 0 to use big-endian
|
|
435
|
-
(for UBJSON
|
|
448
|
+
(for UBJSON & BJData Draft 1)
|
|
436
449
|
|
|
437
450
|
Returns:
|
|
438
451
|
Decoded object
|
|
@@ -454,13 +467,13 @@ def load(fp, no_bytes=False, object_hook=None, object_pairs_hook=None, intern_ob
|
|
|
454
467
|
| | (2) unicode |
|
|
455
468
|
+----------------------------------+---------------+
|
|
456
469
|
| uint8, int8, int16, int32, int64 | (3) int |
|
|
457
|
-
|
|
|
470
|
+
| byte | (2) int, long |
|
|
458
471
|
+----------------------------------+---------------+
|
|
459
472
|
| float32, float64 | float |
|
|
460
473
|
+----------------------------------+---------------+
|
|
461
474
|
| high_precision | Decimal |
|
|
462
475
|
+----------------------------------+---------------+
|
|
463
|
-
| array (typed,
|
|
476
|
+
| array (typed, byte) | (3) bytes |
|
|
464
477
|
| | (2) str |
|
|
465
478
|
+----------------------------------+---------------+
|
|
466
479
|
| true | True |
|
|
@@ -489,9 +502,9 @@ def load(fp, no_bytes=False, object_hook=None, object_pairs_hook=None, intern_ob
|
|
|
489
502
|
except KeyError:
|
|
490
503
|
pass
|
|
491
504
|
if marker == ARRAY_START:
|
|
492
|
-
newobj.append(__decode_array(fp_read, bool(no_bytes), object_hook, object_pairs_hook, intern_object_keys, islittle))
|
|
505
|
+
newobj.append(__decode_array(fp_read, bool(no_bytes), bool(uint8_bytes), object_hook, object_pairs_hook, intern_object_keys, islittle))
|
|
493
506
|
if marker == OBJECT_START:
|
|
494
|
-
newobj.append(__decode_object(fp_read, bool(no_bytes), object_hook, object_pairs_hook, intern_object_keys, islittle))
|
|
507
|
+
newobj.append(__decode_object(fp_read, bool(no_bytes), bool(uint8_bytes), object_hook, object_pairs_hook, intern_object_keys, islittle))
|
|
495
508
|
raise DecoderException('Invalid marker')
|
|
496
509
|
except DecoderException as ex:
|
|
497
510
|
if len(newobj)>0:
|
|
@@ -505,9 +518,9 @@ def load(fp, no_bytes=False, object_hook=None, object_pairs_hook=None, intern_ob
|
|
|
505
518
|
|
|
506
519
|
return newobj;
|
|
507
520
|
|
|
508
|
-
def loadb(chars, no_bytes=False, object_hook=None, object_pairs_hook=None, intern_object_keys=False, islittle=True):
|
|
521
|
+
def loadb(chars, no_bytes=False, uint8_bytes=False, object_hook=None, object_pairs_hook=None, intern_object_keys=False, islittle=True):
|
|
509
522
|
"""Decodes and returns BJData/UBJSON from the given bytes or bytesarray object. See
|
|
510
523
|
load() for available arguments."""
|
|
511
524
|
with BytesIO(chars) as fp:
|
|
512
|
-
return load(fp, no_bytes=no_bytes, object_hook=object_hook, object_pairs_hook=object_pairs_hook,
|
|
525
|
+
return load(fp, no_bytes=no_bytes, uint8_bytes=uint8_bytes, object_hook=object_hook, object_pairs_hook=object_pairs_hook,
|
|
513
526
|
intern_object_keys=intern_object_keys, islittle=islittle)
|
|
@@ -22,9 +22,9 @@ from io import BytesIO
|
|
|
22
22
|
from math import isinf, isnan
|
|
23
23
|
|
|
24
24
|
from .compat import Mapping, Sequence, INTEGER_TYPES, UNICODE_TYPE, TEXT_TYPES, BYTES_TYPES
|
|
25
|
-
from .markers import (TYPE_NULL, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
|
|
26
|
-
TYPE_INT64, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64, TYPE_FLOAT16, TYPE_FLOAT32,
|
|
27
|
-
|
|
25
|
+
from .markers import (TYPE_NULL, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_BYTE, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
|
|
26
|
+
TYPE_INT32, TYPE_INT64, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64, TYPE_FLOAT16, TYPE_FLOAT32,
|
|
27
|
+
TYPE_FLOAT64, TYPE_HIGH_PREC, TYPE_CHAR, TYPE_STRING, OBJECT_START,
|
|
28
28
|
OBJECT_END, ARRAY_START, ARRAY_END, CONTAINER_TYPE, CONTAINER_COUNT)
|
|
29
29
|
|
|
30
30
|
# Lookup tables for encoding small intergers, pre-initialised larger integer & float packers
|
|
@@ -57,7 +57,8 @@ __DTYPE_TO_MARKER = {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
# Prefix applicable to specialised byte array container
|
|
60
|
-
__BYTES_ARRAY_PREFIX = ARRAY_START + CONTAINER_TYPE +
|
|
60
|
+
__BYTES_ARRAY_PREFIX = ARRAY_START + CONTAINER_TYPE + TYPE_BYTE + CONTAINER_COUNT
|
|
61
|
+
__BYTES_ARRAY_PREFIX_DRAFT2 = ARRAY_START + CONTAINER_TYPE + TYPE_UINT8 + CONTAINER_COUNT
|
|
61
62
|
|
|
62
63
|
|
|
63
64
|
class EncoderException(TypeError):
|
|
@@ -146,8 +147,8 @@ def __encode_string(fp_write, item, le=1):
|
|
|
146
147
|
fp_write(encoded_val)
|
|
147
148
|
|
|
148
149
|
|
|
149
|
-
def __encode_bytes(fp_write, item, le=1):
|
|
150
|
-
fp_write(__BYTES_ARRAY_PREFIX)
|
|
150
|
+
def __encode_bytes(fp_write, item, uint8_bytes, le=1):
|
|
151
|
+
fp_write(__BYTES_ARRAY_PREFIX_DRAFT2 if uint8_bytes else __BYTES_ARRAY_PREFIX)
|
|
151
152
|
length = len(item)
|
|
152
153
|
if length < 2 ** 8:
|
|
153
154
|
fp_write(__SMALL_UINTS_ENCODED[le][length])
|
|
@@ -157,7 +158,7 @@ def __encode_bytes(fp_write, item, le=1):
|
|
|
157
158
|
# no ARRAY_END since length was specified
|
|
158
159
|
|
|
159
160
|
|
|
160
|
-
def __encode_value(fp_write, item, seen_containers, container_count, sort_keys, no_float32, islittle, default):
|
|
161
|
+
def __encode_value(fp_write, item, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default):
|
|
161
162
|
le=islittle
|
|
162
163
|
|
|
163
164
|
if isinstance(item, UNICODE_TYPE):
|
|
@@ -185,26 +186,26 @@ def __encode_value(fp_write, item, seen_containers, container_count, sort_keys,
|
|
|
185
186
|
__encode_decimal(fp_write, item, le)
|
|
186
187
|
|
|
187
188
|
elif isinstance(item, BYTES_TYPES):
|
|
188
|
-
__encode_bytes(fp_write, item)
|
|
189
|
+
__encode_bytes(fp_write, item, uint8_bytes, le)
|
|
189
190
|
|
|
190
191
|
# order important since mappings could also be sequences
|
|
191
192
|
elif isinstance(item, Mapping):
|
|
192
|
-
__encode_object(fp_write, item, seen_containers, container_count, sort_keys, no_float32, islittle, default)
|
|
193
|
+
__encode_object(fp_write, item, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default)
|
|
193
194
|
|
|
194
195
|
elif isinstance(item, Sequence):
|
|
195
|
-
__encode_array(fp_write, item, seen_containers, container_count, sort_keys, no_float32, islittle, default)
|
|
196
|
+
__encode_array(fp_write, item, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default)
|
|
196
197
|
|
|
197
198
|
elif default is not None:
|
|
198
|
-
__encode_value(fp_write, default(item), seen_containers, container_count, sort_keys, no_float32, islittle, default)
|
|
199
|
+
__encode_value(fp_write, default(item), seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default)
|
|
199
200
|
|
|
200
201
|
elif type(item).__module__ == "numpy":
|
|
201
|
-
__encode_numpy(fp_write, item, islittle, default)
|
|
202
|
+
__encode_numpy(fp_write, item, uint8_bytes, islittle, default)
|
|
202
203
|
|
|
203
204
|
else:
|
|
204
205
|
raise EncoderException('Cannot encode item of type %s' % type(item))
|
|
205
206
|
|
|
206
207
|
|
|
207
|
-
def __encode_array(fp_write, item, seen_containers, container_count, sort_keys, no_float32, islittle, default):
|
|
208
|
+
def __encode_array(fp_write, item, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default):
|
|
208
209
|
# circular reference check
|
|
209
210
|
container_id = id(item)
|
|
210
211
|
if container_id in seen_containers:
|
|
@@ -217,7 +218,7 @@ def __encode_array(fp_write, item, seen_containers, container_count, sort_keys,
|
|
|
217
218
|
__encode_int(fp_write, len(item), islittle)
|
|
218
219
|
|
|
219
220
|
for value in item:
|
|
220
|
-
__encode_value(fp_write, value, seen_containers, container_count, sort_keys, no_float32,
|
|
221
|
+
__encode_value(fp_write, value, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default)
|
|
221
222
|
|
|
222
223
|
if not container_count:
|
|
223
224
|
fp_write(ARRAY_END)
|
|
@@ -225,7 +226,7 @@ def __encode_array(fp_write, item, seen_containers, container_count, sort_keys,
|
|
|
225
226
|
del seen_containers[container_id]
|
|
226
227
|
|
|
227
228
|
|
|
228
|
-
def __encode_object(fp_write, item, seen_containers, container_count, sort_keys, no_float32,
|
|
229
|
+
def __encode_object(fp_write, item, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default):
|
|
229
230
|
le=islittle;
|
|
230
231
|
# circular reference check
|
|
231
232
|
container_id = id(item)
|
|
@@ -250,7 +251,7 @@ def __encode_object(fp_write, item, seen_containers, container_count, sort_keys,
|
|
|
250
251
|
__encode_int(fp_write, length, le)
|
|
251
252
|
fp_write(encoded_key)
|
|
252
253
|
|
|
253
|
-
__encode_value(fp_write, value, seen_containers, container_count, sort_keys, no_float32,
|
|
254
|
+
__encode_value(fp_write, value, seen_containers, container_count, sort_keys, no_float32, uint8_bytes, islittle, default)
|
|
254
255
|
|
|
255
256
|
if not container_count:
|
|
256
257
|
fp_write(OBJECT_END)
|
|
@@ -263,7 +264,7 @@ def __map_dtype(dtypestr):
|
|
|
263
264
|
else:
|
|
264
265
|
raise Exception("bjdata", "numpy dtype {} is not supported".format(dtypestr))
|
|
265
266
|
|
|
266
|
-
def __encode_numpy(fp_write, item, islittle, default):
|
|
267
|
+
def __encode_numpy(fp_write, item, uint8_bytes, islittle, default):
|
|
267
268
|
try:
|
|
268
269
|
import numpy as np
|
|
269
270
|
except ImportError:
|
|
@@ -296,7 +297,7 @@ def __encode_numpy(fp_write, item, islittle, default):
|
|
|
296
297
|
fp_write(item.data)
|
|
297
298
|
|
|
298
299
|
|
|
299
|
-
def dump(obj, fp, container_count=False, sort_keys=False, no_float32=True, islittle=True, default=None):
|
|
300
|
+
def dump(obj, fp, container_count=False, sort_keys=False, no_float32=True, uint8_bytes=False, islittle=True, default=None):
|
|
300
301
|
"""Writes the given object as BJData/UBJSON to the provided file-like object
|
|
301
302
|
|
|
302
303
|
Args:
|
|
@@ -312,6 +313,10 @@ def dump(obj, fp, container_count=False, sort_keys=False, no_float32=True, islit
|
|
|
312
313
|
no_float32 (bool): Never use float32 to store float numbers (other than
|
|
313
314
|
for zero). Disabling this might save space at the
|
|
314
315
|
loss of precision.
|
|
316
|
+
uint8_bytes (bool): If set, typed UBJSON arrays (uint8) will be
|
|
317
|
+
converted to a bytes instance instead of being
|
|
318
|
+
treated as an array (for UBJSON & BJData Draft 2).
|
|
319
|
+
Ignored if no_bytes is set.
|
|
315
320
|
islittle (1 or 0): default is 1 for little-endian for all numerics (for
|
|
316
321
|
BJData Draft 2), change to 0 to use big-endian
|
|
317
322
|
(for UBJSON for BJData Draft 1)
|
|
@@ -342,8 +347,8 @@ def dump(obj, fp, container_count=False, sort_keys=False, no_float32=True, islit
|
|
|
342
347
|
+------------------------------+-----------------------------------+
|
|
343
348
|
| Decimal | high_precision |
|
|
344
349
|
+------------------------------+-----------------------------------+
|
|
345
|
-
| (3) bytes, bytearray | array (type,
|
|
346
|
-
| (2) str | array (type,
|
|
350
|
+
| (3) bytes, bytearray | array (type, byte) |
|
|
351
|
+
| (2) str | array (type, byte) |
|
|
347
352
|
+------------------------------+-----------------------------------+
|
|
348
353
|
| (3) collections.abc.Mapping | object |
|
|
349
354
|
| (2) collections.Mapping | |
|
|
@@ -371,12 +376,12 @@ def dump(obj, fp, container_count=False, sort_keys=False, no_float32=True, islit
|
|
|
371
376
|
raise TypeError('fp.write not callable')
|
|
372
377
|
fp_write = fp.write
|
|
373
378
|
|
|
374
|
-
__encode_value(fp_write, obj, {}, container_count, sort_keys, no_float32, islittle, default)
|
|
379
|
+
__encode_value(fp_write, obj, {}, container_count, sort_keys, no_float32, uint8_bytes, islittle, default)
|
|
375
380
|
|
|
376
381
|
|
|
377
|
-
def dumpb(obj, container_count=False, sort_keys=False, no_float32=True, islittle=True, default=None):
|
|
382
|
+
def dumpb(obj, container_count=False, sort_keys=False, no_float32=True, uint8_bytes=False, islittle=True, default=None):
|
|
378
383
|
"""Returns the given object as BJData/UBJSON in a bytes instance. See dump() for
|
|
379
384
|
available arguments."""
|
|
380
385
|
with BytesIO() as fp:
|
|
381
|
-
dump(obj, fp, container_count=container_count, sort_keys=sort_keys, no_float32=no_float32, islittle=islittle, default=default)
|
|
386
|
+
dump(obj, fp, container_count=container_count, sort_keys=sort_keys, no_float32=no_float32, uint8_bytes=uint8_bytes, islittle=islittle, default=default)
|
|
382
387
|
return fp.getvalue()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: bjdata
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Binary JData and UBJSON encoder/decoder
|
|
5
5
|
Home-page: https://github.com/NeuroJSON/pybj
|
|
6
6
|
Author: Qianqian Fang
|
|
@@ -37,19 +37,19 @@ Requires-Dist: coverage<4.6,>=4.5.3; extra == "dev"
|
|
|
37
37
|
|
|
38
38
|
# Binary JData for Python - a lightweight binary JSON format
|
|
39
39
|
|
|
40
|
-
- Copyright: (C) Qianqian Fang (2020-
|
|
40
|
+
- Copyright: (C) Qianqian Fang (2020-2025) <q.fang at neu.edu>
|
|
41
41
|
- Copyright: (C) Iotic Labs Ltd. (2016-2019) <vilnis.termanis at iotic-labs.com>
|
|
42
42
|
- License: Apache License, Version 2.0
|
|
43
|
-
- Version: 0.
|
|
43
|
+
- Version: 0.5.0
|
|
44
44
|
- URL: https://pypi.org/project/bjdata/
|
|
45
45
|
- Github: https://github.com/NeuroJSON/pybj
|
|
46
|
-
- BJData Spec Version: [V1 Draft
|
|
46
|
+
- BJData Spec Version: [V1 Draft 3](https://neurojson.org/bjdata/draft3)
|
|
47
47
|
- Acknowledgement: This project is supported by US National Institute of Health (NIH) grant U24-NS124027
|
|
48
48
|
|
|
49
49
|
[](https://travis-ci.com/NeuroJSON/pybj)
|
|
50
50
|
|
|
51
51
|
This is a Python v3.2+ (and 2.7+) [Binary JData](https://neurojson.org) encoder
|
|
52
|
-
and decoder based on the [Draft-
|
|
52
|
+
and decoder based on the [Draft-3](Binary_JData_Specification.md) specification.
|
|
53
53
|
|
|
54
54
|
## Installing / packaging
|
|
55
55
|
```shell
|
|
@@ -172,12 +172,13 @@ This package was modified based on the py-ubjson package developed by
|
|
|
172
172
|
Project URL: https://github.com/Iotic-Labs/py-ubjson
|
|
173
173
|
|
|
174
174
|
The major changes were focused on supporting the Binary JData Specification
|
|
175
|
-
[Draft
|
|
175
|
+
[Draft 3](https://neurojson.org/bjdata/draft3) -
|
|
176
176
|
an extended Universal Binary JSON (UBJSON) Specification Draft-12 by adding
|
|
177
177
|
the below new features:
|
|
178
178
|
|
|
179
|
-
* BJData adds
|
|
179
|
+
* BJData adds 5 new data types: `uint16 [u]`, `uint32 [m]`, `uint64 [M]`, `float16 [h]`, and `byte [B]`
|
|
180
|
+
* BJData adds a dedicated byte data type used in optimized array containers for binary data
|
|
180
181
|
* BJData supports an optimized ND array container
|
|
181
182
|
* BJData does not convert NaN/Inf/-Inf to `null`
|
|
182
183
|
* BJData uses little-Endian as the default integer/floating-point numbers while UBJSON uses big-Endian
|
|
183
|
-
* BJData only permits non-zero-fixed-length data types (`
|
|
184
|
+
* BJData only permits non-zero-fixed-length data types (`UiuImlMLhdDCB`) in strongly-typed array/object containers
|
|
@@ -73,7 +73,7 @@ COMPILE_ARGS = ['-std=c99', '-DUSE__BJDATA']
|
|
|
73
73
|
|
|
74
74
|
setup(
|
|
75
75
|
name='bjdata',
|
|
76
|
-
version='0.
|
|
76
|
+
version='0.5.0',
|
|
77
77
|
description='Binary JData and UBJSON encoder/decoder',
|
|
78
78
|
long_description=load_description('README.md'),
|
|
79
79
|
long_description_content_type='text/markdown',
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
|
|
28
28
|
/******************************************************************************/
|
|
29
29
|
|
|
30
|
-
// container_count, sort_keys, no_float32, islittle
|
|
31
|
-
static _bjdata_encoder_prefs_t _bjdata_encoder_prefs_defaults = { NULL, 0, 0, 1, 1 };
|
|
30
|
+
// container_count, sort_keys, no_float32, islittle, uint8_bytes
|
|
31
|
+
static _bjdata_encoder_prefs_t _bjdata_encoder_prefs_defaults = { NULL, 0, 0, 1, 1, 0 };
|
|
32
32
|
|
|
33
|
-
// no_bytes, object_pairs_hook, islittle
|
|
34
|
-
static _bjdata_decoder_prefs_t _bjdata_decoder_prefs_defaults = { NULL, NULL, 0, 0, 1 };
|
|
33
|
+
// no_bytes, object_pairs_hook, islittle, uint8_bytes
|
|
34
|
+
static _bjdata_decoder_prefs_t _bjdata_decoder_prefs_defaults = { NULL, NULL, 0, 0, 1, 0 };
|
|
35
35
|
|
|
36
36
|
/******************************************************************************/
|
|
37
37
|
|
|
@@ -39,8 +39,8 @@ PyDoc_STRVAR(_bjdata_dump__doc__, "See pure Python version (encoder.dump) for do
|
|
|
39
39
|
#define FUNC_DEF_DUMP {"dump", (PyCFunction)_bjdata_dump, METH_VARARGS | METH_KEYWORDS, _bjdata_dump__doc__}
|
|
40
40
|
static PyObject*
|
|
41
41
|
_bjdata_dump(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
42
|
-
static const char *format = "OO|
|
|
43
|
-
static char *keywords[] = {"obj", "fp", "container_count", "sort_keys", "no_float32", "islittle", "default", NULL};
|
|
42
|
+
static const char *format = "OO|iiiiiO:dump";
|
|
43
|
+
static char *keywords[] = {"obj", "fp", "container_count", "sort_keys", "no_float32", "islittle", "uint8_bytes", "default", NULL};
|
|
44
44
|
|
|
45
45
|
_bjdata_encoder_buffer_t *buffer = NULL;
|
|
46
46
|
_bjdata_encoder_prefs_t prefs = _bjdata_encoder_prefs_defaults;
|
|
@@ -50,7 +50,7 @@ _bjdata_dump(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
|
50
50
|
UNUSED(self);
|
|
51
51
|
|
|
52
52
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, &obj, &fp, &prefs.container_count,
|
|
53
|
-
&prefs.sort_keys, &prefs.no_float32, &prefs.islittle, &prefs.default_func)) {
|
|
53
|
+
&prefs.sort_keys, &prefs.no_float32, &prefs.islittle, &prefs.uint8_bytes, &prefs.default_func)) {
|
|
54
54
|
goto bail;
|
|
55
55
|
}
|
|
56
56
|
BAIL_ON_NULL(fp_write = PyObject_GetAttrString(fp, "write"));
|
|
@@ -73,8 +73,8 @@ PyDoc_STRVAR(_bjdata_dumpb__doc__, "See pure Python version (encoder.dumpb) for
|
|
|
73
73
|
#define FUNC_DEF_DUMPB {"dumpb", (PyCFunction)_bjdata_dumpb, METH_VARARGS | METH_KEYWORDS, _bjdata_dumpb__doc__}
|
|
74
74
|
static PyObject*
|
|
75
75
|
_bjdata_dumpb(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
76
|
-
static const char *format = "O|
|
|
77
|
-
static char *keywords[] = {"obj", "container_count", "sort_keys", "no_float32", "islittle", "default", NULL};
|
|
76
|
+
static const char *format = "O|iiiiiO:dumpb";
|
|
77
|
+
static char *keywords[] = {"obj", "container_count", "sort_keys", "no_float32", "islittle", "uint8_bytes", "default", NULL};
|
|
78
78
|
|
|
79
79
|
_bjdata_encoder_buffer_t *buffer = NULL;
|
|
80
80
|
_bjdata_encoder_prefs_t prefs = _bjdata_encoder_prefs_defaults;
|
|
@@ -82,7 +82,7 @@ _bjdata_dumpb(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
|
82
82
|
UNUSED(self);
|
|
83
83
|
|
|
84
84
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, &obj, &prefs.container_count, &prefs.sort_keys,
|
|
85
|
-
&prefs.no_float32, &prefs.islittle, &prefs.default_func)) {
|
|
85
|
+
&prefs.no_float32, &prefs.islittle, &prefs.uint8_bytes, &prefs.default_func)) {
|
|
86
86
|
goto bail;
|
|
87
87
|
}
|
|
88
88
|
|
|
@@ -103,8 +103,8 @@ PyDoc_STRVAR(_bjdata_load__doc__, "See pure Python version (encoder.load) for do
|
|
|
103
103
|
#define FUNC_DEF_LOAD {"load", (PyCFunction)_bjdata_load, METH_VARARGS | METH_KEYWORDS, _bjdata_load__doc__}
|
|
104
104
|
static PyObject*
|
|
105
105
|
_bjdata_load(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
106
|
-
static const char *format = "O|
|
|
107
|
-
static char *keywords[] = {"fp", "no_bytes", "object_hook", "object_pairs_hook", "intern_object_keys", "islittle", NULL};
|
|
106
|
+
static const char *format = "O|iOOiii:load";
|
|
107
|
+
static char *keywords[] = {"fp", "no_bytes", "object_hook", "object_pairs_hook", "intern_object_keys", "islittle", "uint8_bytes", NULL};
|
|
108
108
|
|
|
109
109
|
_bjdata_decoder_buffer_t *buffer = NULL;
|
|
110
110
|
_bjdata_decoder_prefs_t prefs = _bjdata_decoder_prefs_defaults;
|
|
@@ -116,7 +116,7 @@ _bjdata_load(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
|
116
116
|
UNUSED(self);
|
|
117
117
|
|
|
118
118
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, &fp, &prefs.no_bytes, &prefs.object_hook,
|
|
119
|
-
&prefs.object_pairs_hook, &prefs.intern_object_keys, &prefs.islittle)) {
|
|
119
|
+
&prefs.object_pairs_hook, &prefs.intern_object_keys, &prefs.islittle, &prefs.uint8_bytes)) {
|
|
120
120
|
goto bail;
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -158,8 +158,8 @@ PyDoc_STRVAR(_bjdata_loadb__doc__, "See pure Python version (encoder.loadb) for
|
|
|
158
158
|
#define FUNC_DEF_LOADB {"loadb", (PyCFunction)_bjdata_loadb, METH_VARARGS | METH_KEYWORDS, _bjdata_loadb__doc__}
|
|
159
159
|
static PyObject*
|
|
160
160
|
_bjdata_loadb(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
161
|
-
static const char *format = "O|
|
|
162
|
-
static char *keywords[] = {"chars", "no_bytes", "object_hook", "object_pairs_hook", "intern_object_keys", "islittle", NULL};
|
|
161
|
+
static const char *format = "O|iOOiii:loadb";
|
|
162
|
+
static char *keywords[] = {"chars", "no_bytes", "object_hook", "object_pairs_hook", "intern_object_keys", "islittle", "uint8_bytes", NULL};
|
|
163
163
|
|
|
164
164
|
_bjdata_decoder_buffer_t *buffer = NULL;
|
|
165
165
|
_bjdata_decoder_prefs_t prefs = _bjdata_decoder_prefs_defaults;
|
|
@@ -168,7 +168,7 @@ _bjdata_loadb(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
|
168
168
|
UNUSED(self);
|
|
169
169
|
|
|
170
170
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, &chars, &prefs.no_bytes, &prefs.object_hook,
|
|
171
|
-
&prefs.object_pairs_hook, &prefs.intern_object_keys, &prefs.islittle)) {
|
|
171
|
+
&prefs.object_pairs_hook, &prefs.intern_object_keys, &prefs.islittle, &prefs.uint8_bytes)) {
|
|
172
172
|
goto bail;
|
|
173
173
|
}
|
|
174
174
|
if (PyUnicode_Check(chars)) {
|
|
@@ -405,6 +405,20 @@ bail:
|
|
|
405
405
|
|
|
406
406
|
// These methods are partially based on Python's _struct.c
|
|
407
407
|
|
|
408
|
+
static PyObject* _decode_byte(_bjdata_decoder_buffer_t *buffer) {
|
|
409
|
+
char value;
|
|
410
|
+
|
|
411
|
+
READ_CHAR_OR_BAIL(value, "byte");
|
|
412
|
+
#if PY_MAJOR_VERSION < 3
|
|
413
|
+
return PyInt_FromLong((long) (unsigned char)value);
|
|
414
|
+
#else
|
|
415
|
+
return PyLong_FromLong((long) (unsigned char)value);
|
|
416
|
+
#endif
|
|
417
|
+
|
|
418
|
+
bail:
|
|
419
|
+
return NULL;
|
|
420
|
+
}
|
|
421
|
+
|
|
408
422
|
static PyObject* _decode_int8(_bjdata_decoder_buffer_t *buffer) {
|
|
409
423
|
char value;
|
|
410
424
|
|
|
@@ -569,6 +583,9 @@ static long long _decode_int_non_negative(_bjdata_decoder_buffer_t *buffer, char
|
|
|
569
583
|
}
|
|
570
584
|
|
|
571
585
|
switch (marker) {
|
|
586
|
+
case TYPE_BYTE:
|
|
587
|
+
BAIL_ON_NULL(int_obj = _decode_byte(buffer));
|
|
588
|
+
break;
|
|
572
589
|
case TYPE_UINT8:
|
|
573
590
|
BAIL_ON_NULL(int_obj = _decode_uint8(buffer));
|
|
574
591
|
break;
|
|
@@ -716,7 +733,7 @@ static _container_params_t _get_container_params(_bjdata_decoder_buffer_t *buffe
|
|
|
716
733
|
case TYPE_NULL: case TYPE_BOOL_TRUE: case TYPE_BOOL_FALSE: case TYPE_CHAR: case TYPE_STRING: case TYPE_INT8:
|
|
717
734
|
case TYPE_UINT8: case TYPE_INT16: case TYPE_INT32: case TYPE_INT64: case TYPE_FLOAT32: case TYPE_FLOAT64:
|
|
718
735
|
#ifdef USE__BJDATA
|
|
719
|
-
case TYPE_UINT16: case TYPE_UINT32: case TYPE_UINT64: case TYPE_FLOAT16:
|
|
736
|
+
case TYPE_UINT16: case TYPE_UINT32: case TYPE_UINT64: case TYPE_FLOAT16: case TYPE_BYTE:
|
|
720
737
|
#endif
|
|
721
738
|
case TYPE_HIGH_PREC: case ARRAY_START: case OBJECT_START:
|
|
722
739
|
params.type = marker;
|
|
@@ -803,7 +820,8 @@ static int _is_fixed_len_type(char type) {
|
|
|
803
820
|
return ((TYPE_INT8 == type) || (TYPE_UINT8 == type) || (TYPE_INT16 == type)
|
|
804
821
|
|| (TYPE_UINT16 == type) || (TYPE_INT32 == type) || (TYPE_UINT32 == type)
|
|
805
822
|
|| (TYPE_INT64 == type) || (TYPE_UINT64 == type) || (TYPE_CHAR == type)
|
|
806
|
-
|| (TYPE_FLOAT16 == type) || (TYPE_FLOAT32 == type) || (TYPE_FLOAT64 == type))
|
|
823
|
+
|| (TYPE_FLOAT16 == type) || (TYPE_FLOAT32 == type) || (TYPE_FLOAT64 == type))
|
|
824
|
+
|| (TYPE_BYTE == type);
|
|
807
825
|
}
|
|
808
826
|
|
|
809
827
|
// Note: Does NOT reserve a new reference
|
|
@@ -881,7 +899,7 @@ static PyObject* _decode_array(_bjdata_decoder_buffer_t *buffer) {
|
|
|
881
899
|
marker = params.marker;
|
|
882
900
|
if (params.counting) {
|
|
883
901
|
// special case - byte array
|
|
884
|
-
if ((TYPE_UINT8 == params.type) && !buffer->prefs.no_bytes && ndims==0) {
|
|
902
|
+
if (((buffer->prefs.uint8_bytes ? TYPE_UINT8 : TYPE_BYTE) == params.type) && !buffer->prefs.no_bytes && ndims==0) {
|
|
885
903
|
BAIL_ON_NULL(list = PyBytes_FromStringAndSize(NULL, params.count));
|
|
886
904
|
READ_INTO_OR_BAIL(params.count, PyBytes_AS_STRING(list), "bytes array");
|
|
887
905
|
return list;
|
|
@@ -1211,6 +1229,8 @@ PyObject* _bjdata_decode_value(_bjdata_decoder_buffer_t *buffer, char *given_mar
|
|
|
1211
1229
|
case TYPE_INT64:
|
|
1212
1230
|
RETURN_OR_RAISE_DECODER_EXCEPTION(_decode_int64(buffer), "int64");
|
|
1213
1231
|
#ifdef USE__BJDATA
|
|
1232
|
+
case TYPE_BYTE:
|
|
1233
|
+
RETURN_OR_RAISE_DECODER_EXCEPTION(_decode_byte(buffer), "byte");
|
|
1214
1234
|
case TYPE_UINT8:
|
|
1215
1235
|
RETURN_OR_RAISE_DECODER_EXCEPTION(_decode_uint8(buffer), "uint8");
|
|
1216
1236
|
case TYPE_FLOAT16:
|
|
@@ -28,10 +28,11 @@ extern "C" {
|
|
|
28
28
|
typedef struct {
|
|
29
29
|
PyObject *object_hook;
|
|
30
30
|
PyObject *object_pairs_hook;
|
|
31
|
-
// don't convert
|
|
31
|
+
// don't convert BYTE arrays to bytes instances (and keep as an array of individual integers)
|
|
32
32
|
int no_bytes;
|
|
33
33
|
int intern_object_keys;
|
|
34
34
|
int islittle;
|
|
35
|
+
int uint8_bytes;
|
|
35
36
|
} _bjdata_decoder_prefs_t;
|
|
36
37
|
|
|
37
38
|
typedef struct _bjdata_decoder_buffer_t {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
|
|
32
32
|
/******************************************************************************/
|
|
33
33
|
|
|
34
|
-
static char bytes_array_prefix[] = {ARRAY_START, CONTAINER_TYPE,
|
|
34
|
+
static char bytes_array_prefix[] = {ARRAY_START, CONTAINER_TYPE, TYPE_BYTE, CONTAINER_COUNT};
|
|
35
35
|
|
|
36
36
|
#define POWER_TWO(x) ((long long) 1 << (x))
|
|
37
37
|
|
|
@@ -25,7 +25,7 @@ from collections import OrderedDict
|
|
|
25
25
|
|
|
26
26
|
from bjdata import (dump as bjddump, dumpb as bjddumpb, load as bjdload, loadb as bjdloadb, EncoderException,
|
|
27
27
|
DecoderException, EXTENSION_ENABLED)
|
|
28
|
-
from bjdata.markers import (TYPE_NULL, TYPE_NOOP, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
|
|
28
|
+
from bjdata.markers import (TYPE_NULL, TYPE_NOOP, TYPE_BOOL_TRUE, TYPE_BOOL_FALSE, TYPE_BYTE, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
|
|
29
29
|
TYPE_INT32, TYPE_INT64, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64, TYPE_FLOAT16, TYPE_FLOAT32, TYPE_FLOAT64,
|
|
30
30
|
TYPE_HIGH_PREC, TYPE_CHAR, TYPE_STRING, OBJECT_START, OBJECT_END, ARRAY_START, ARRAY_END,
|
|
31
31
|
CONTAINER_TYPE, CONTAINER_COUNT)
|
|
@@ -180,8 +180,11 @@ class TestEncodeDecodePlain(TestCase): # pylint: disable=too-many-public-method
|
|
|
180
180
|
(TYPE_HIGH_PREC, 9999999999999999999999999999999999999, 40)):
|
|
181
181
|
self.check_enc_dec(value, total_size, expected_type=type_)
|
|
182
182
|
|
|
183
|
-
self.assertEqual((self.bjdloadb(b'[$U#U\x08\x01\x02\x03\x04\x05\x06\x07\x08')
|
|
184
|
-
==b'\x01\x02\x03\x04\x05\x06\x07\x08'), True)
|
|
183
|
+
self.assertEqual((self.bjdloadb(b'[$U#U\x08\x01\x02\x03\x04\x05\x06\x07\x08', uint8_bytes=True)
|
|
184
|
+
== b'\x01\x02\x03\x04\x05\x06\x07\x08'), True)
|
|
185
|
+
|
|
186
|
+
self.assertEqual((self.bjdloadb(b'[$B#U\x08\x01\x02\x03\x04\x05\x06\x07\x08')
|
|
187
|
+
== b'\x01\x02\x03\x04\x05\x06\x07\x08'), True)
|
|
185
188
|
|
|
186
189
|
self.assertEqual((self.bjdloadb(b'[$u#U\x04\x01\x02\x03\x04\x05\x06\x07\x08')
|
|
187
190
|
==ndarray([513, 1027, 1541, 2055], np.uint16)).all(), True)
|
|
@@ -286,11 +289,11 @@ class TestEncodeDecodePlain(TestCase): # pylint: disable=too-many-public-method
|
|
|
286
289
|
def test_bytes(self):
|
|
287
290
|
# insufficient length
|
|
288
291
|
with self.assertRaises(DecoderException):
|
|
289
|
-
self.bjdloadb(ARRAY_START + CONTAINER_TYPE +
|
|
292
|
+
self.bjdloadb(ARRAY_START + CONTAINER_TYPE + TYPE_BYTE + CONTAINER_COUNT + TYPE_UINT8 + b'\x02' + b'\x01')
|
|
290
293
|
for cast in (bytes, bytearray):
|
|
291
294
|
self.check_enc_dec(cast(b''))
|
|
292
295
|
self.check_enc_dec(cast(b'\x01' * 4))
|
|
293
|
-
self.assertEqual((self.bjdloadb(self.bjddumpb(cast(b'\x04' * 4)), no_bytes=True) == ndarray([4] * 4, npint8)).all(), True)
|
|
296
|
+
#self.assertEqual((self.bjdloadb(self.bjddumpb(cast(b'\x04' * 4)), no_bytes=True) == ndarray([4] * 4, npint8)).all(), True)
|
|
294
297
|
self.check_enc_dec(cast(b'largebinary' * 100))
|
|
295
298
|
|
|
296
299
|
def test_nd_array(self):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|