Skip to content

Commit af6e34a

Browse files
committed
Merge branch 'develop' into cleanup
# Conflicts: # .gitignore
2 parents 50bc973 + 0cb1241 commit af6e34a

File tree

14 files changed

+521
-275
lines changed

14 files changed

+521
-275
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444
venv
4545

4646
nlohmann_json.spdx
47+
48+
# Bazel-related
49+
MODULE.bazel.lock

BUILD.bazel

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
2+
load("@rules_license//rules:license.bzl", "license")
3+
4+
package(
5+
default_applicable_licenses = [":license"],
6+
)
7+
8+
exports_files([
9+
"LICENSE.MIT",
10+
])
11+
12+
license(
13+
name = "license",
14+
license_kinds = ["@rules_license//licenses/spdx:MIT"],
15+
license_text = "LICENSE.MIT",
16+
)
17+
118
cc_library(
219
name = "json",
320
hdrs = [
@@ -52,3 +69,12 @@ cc_library(
5269
visibility = ["//visibility:public"],
5370
alwayslink = True,
5471
)
72+
73+
cc_library(
74+
name = "singleheader-json",
75+
hdrs = [
76+
"single_include/nlohmann/json.hpp",
77+
],
78+
includes = ["single_include"],
79+
visibility = ["//visibility:public"],
80+
)

MODULE.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module(
2+
name = "nlohmann_json",
3+
compatibility_level = 1,
4+
)
5+
6+
bazel_dep(name = "rules_cc", version = "0.0.17")
7+
bazel_dep(name = "rules_license", version = "1.0.0")

WORKSPACE.bazel

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/mkdocs/docs/api/basic_json/to_bjdata.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
// (1)
55
static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
66
const bool use_size = false,
7-
const bool use_type = false);
7+
const bool use_type = false,
8+
const bjdata_version_t version = bjdata_version_t::draft2);
89

910
// (2)
1011
static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
11-
const bool use_size = false, const bool use_type = false);
12+
const bool use_size = false, const bool use_type = false,
13+
const bjdata_version_t version = bjdata_version_t::draft2);
1214
static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
13-
const bool use_size = false, const bool use_type = false);
15+
const bool use_size = false, const bool use_type = false,
16+
const bjdata_version_t version = bjdata_version_t::draft2);
1417
```
1518
1619
Serializes a given JSON value `j` to a byte vector using the BJData (Binary JData) serialization format. BJData aims to
@@ -34,6 +37,9 @@ The exact mapping and its limitations is described on a [dedicated page](../../f
3437
3538
`use_type` (in)
3639
: whether to add type annotations to container types (must be combined with `#!cpp use_size = true`); optional,
40+
41+
`version` (in)
42+
: which version of BJData to use (see [draft 3](../../features/binary_formats/bjdata.md#draft-3-binary-format)); optional,
3743
`#!cpp false` by default.
3844
3945
## Return value
@@ -68,3 +74,4 @@ Linear in the size of the JSON value `j`.
6874
## Version history
6975
7076
- Added in version 3.11.0.
77+
- BJData version parameter (for draft3 binary encoding) added in version 3.12.0.

docs/mkdocs/docs/features/binary_formats/bjdata.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
The [BJData format](https://neurojson.org) was derived from and improved upon
44
[Universal Binary JSON(UBJSON)](https://ubjson.org) specification (Draft 12). Specifically, it introduces an optimized
5-
array container for efficient storage of N-dimensional packed arrays (**ND-arrays**); it also adds 4 new type markers -
6-
`[u] - uint16`, `[m] - uint32`, `[M] - uint64` and `[h] - float16` - to unambiguously map common binary numeric types;
7-
furthermore, it uses little-endian (LE) to store all numerics instead of big-endian (BE) as in UBJSON to avoid
8-
unnecessary conversions on commonly available platforms.
5+
array container for efficient storage of N-dimensional packed arrays (**ND-arrays**); it also adds 5 new type markers -
6+
`[u] - uint16`, `[m] - uint32`, `[M] - uint64`, `[h] - float16` and `[B] - byte` - to unambiguously map common binary
7+
numeric types; furthermore, it uses little-endian (LE) to store all numerics instead of big-endian (BE) as in UBJSON to
8+
avoid unnecessary conversions on commonly available platforms.
99

1010
Compared to other binary JSON-like formats such as MessagePack and CBOR, both BJData and UBJSON demonstrate a rare
1111
combination of being both binary and **quasi-human-readable**. This is because all semantic elements in BJData and
@@ -49,6 +49,7 @@ The library uses the following mapping from JSON values types to BJData types ac
4949
| string | *with shortest length indicator* | string | `S` |
5050
| array | *see notes on optimized format/ND-array* | array | `[` |
5151
| object | *see notes on optimized format* | map | `{` |
52+
| binary | *see notes on binary values* | array | `[$B` |
5253

5354
!!! success "Complete mapping"
5455

@@ -128,15 +129,24 @@ The library uses the following mapping from JSON values types to BJData types ac
128129

129130
Due to diminished space saving, hampered readability, and increased security risks, in BJData, the allowed data
130131
types following the `$` marker in an optimized array and object container are restricted to
131-
**non-zero-fixed-length** data types. Therefore, the valid optimized type markers can only be one of `UiuImlMLhdDC`.
132-
This also means other variable (`[{SH`) or zero-length types (`TFN`) can not be used in an optimized array or object
133-
in BJData.
132+
**non-zero-fixed-length** data types. Therefore, the valid optimized type markers can only be one of
133+
`UiuImlMLhdDCB`. This also means other variable (`[{SH`) or zero-length types (`TFN`) can not be used in an
134+
optimized array or object in BJData.
134135

135136
!!! info "Binary values"
136137

137-
If the JSON data contains the binary type, the value stored is a list of integers, as suggested by the BJData
138-
documentation. In particular, this means that the serialization and the deserialization of JSON containing binary
139-
values into BJData and back will result in a different JSON object.
138+
BJData provides a dedicated `B` marker (defined in the [BJData specification (Draft 3)][BJDataBinArr]) that is used
139+
in optimized arrays to designate binary data. This means that, unlike UBJSON, binary data can be both serialized and
140+
deserialized.
141+
142+
To preserve compatibility with BJData Draft 2, the Draft 3 optimized binary array must be explicitly enabled using
143+
the `version` parameter of [`to_bjdata`](../../api/basic_json/to_bjdata.md).
144+
145+
In Draft2 mode (default), if the JSON data contains the binary type, the value stored as a list of integers, as
146+
suggested by the BJData documentation. In particular, this means that the serialization and the deserialization of
147+
JSON containing binary values into BJData and back will result in a different JSON object.
148+
149+
[BJDataBinArr]: https://github.com/NeuroJSON/bjdata/blob/master/Binary_JData_Specification.md#optimized-binary-array)
140150

141151
??? example
142152

@@ -171,11 +181,13 @@ The library maps BJData types to JSON value types as follows:
171181
| int32 | number_integer | `l` |
172182
| uint64 | number_unsigned | `M` |
173183
| int64 | number_integer | `L` |
184+
| byte | number_unsigned | `B` |
174185
| string | string | `S` |
175186
| char | string | `C` |
176187
| array | array (optimized values are supported) | `[` |
177188
| ND-array | object (in JData annotated array format)|`[$.#[.`|
178189
| object | object (optimized values are supported) | `{` |
190+
| binary | binary (strongly-typed byte array) | `[$B` |
179191

180192
!!! success "Complete mapping"
181193

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel_dep(name = "nlohmann_json", version = "3.11.3.bcr.1")

docs/mkdocs/docs/integration/bazel/WORKSPACE

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/mkdocs/docs/integration/package_managers.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,14 @@ using the subproject directly.
156156

157157
!!! abstract "Summary"
158158

159-
use `http_archive`, `git_repository`, or `local_repository`
159+
use `bazel_dep`, `git_override`, or `local_path_override`
160160

161-
- :octicons-tag-24: Any version, as version is specified in `WORKSPACE` file
161+
- :octicons-tag-24: Any version, that is available via [Bazel Central Registry](https://registry.bazel.build/modules/nlohmann_json)
162162
- :octicons-file-24: File issues at the [library issue tracker](https://github.com/nlohmann/json/issues)
163163
- :octicons-question-24: [Bazel website](https://bazel.build)
164164

165-
This repository provides a [Bazel](https://bazel.build/) `WORKSPACE.bazel` and a corresponding `BUILD.bazel` file. Therefore, this
166-
repository can be referenced by workspace rules such as `http_archive`, `git_repository`, or `local_repository` from
167-
other Bazel workspaces. To use the library you only need to depend on the target `@nlohmann_json//:json` (e.g., via
168-
`deps` attribute).
165+
This repository provides a [Bazel](https://bazel.build/) `MODULE.bazel` and a corresponding `BUILD.bazel` file. Therefore, this
166+
repository can be referenced within a `MODULE.bazel` by rules such as `archive_override`, `git_override`, or `local_path_override`. To use the library you need to depend on the target `@nlohmann_json//:json` (i.e., via `deps` attribute).
169167

170168
??? example
171169

@@ -176,7 +174,7 @@ other Bazel workspaces. To use the library you only need to depend on the target
176174
```
177175

178176
```ini title="WORKSPACE"
179-
--8<-- "integration/bazel/WORKSPACE"
177+
--8<-- "integration/bazel/MODULE.bazel"
180178
```
181179

182180
```cpp title="example.cpp"

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,16 @@ class binary_reader
23132313
case 'Z': // null
23142314
return sax->null();
23152315

2316+
case 'B': // byte
2317+
{
2318+
if (input_format != input_format_t::bjdata)
2319+
{
2320+
break;
2321+
}
2322+
std::uint8_t number{};
2323+
return get_number(input_format, number) && sax->number_unsigned(number);
2324+
}
2325+
23162326
case 'U':
23172327
{
23182328
std::uint8_t number{};
@@ -2513,7 +2523,7 @@ class binary_reader
25132523
return false;
25142524
}
25152525

2516-
if (size_and_type.second == 'C')
2526+
if (size_and_type.second == 'C' || size_and_type.second == 'B')
25172527
{
25182528
size_and_type.second = 'U';
25192529
}
@@ -2535,6 +2545,13 @@ class binary_reader
25352545
return (sax->end_array() && sax->end_object());
25362546
}
25372547

2548+
// If BJData type marker is 'B' decode as binary
2549+
if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
2550+
{
2551+
binary_t result;
2552+
return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
2553+
}
2554+
25382555
if (size_and_type.first != npos)
25392556
{
25402557
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
@@ -3008,6 +3025,7 @@ class binary_reader
30083025

30093026
#define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
30103027
make_array<bjd_type>( \
3028+
bjd_type{'B', "byte"}, \
30113029
bjd_type{'C', "char"}, \
30123030
bjd_type{'D', "double"}, \
30133031
bjd_type{'I', "int16"}, \

0 commit comments

Comments
 (0)