mirror of
https://github.com/nlohmann/json.git
synced 2026-11-17 17:01:30 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b0a4218e38 |
No files matched your search
@@ -1383,7 +1383,6 @@ Use your favorite [**package manager**](https://json.nlohmann.me/integration/pac
|
|||||||
- [**build2**](https://json.nlohmann.me/integration/package_managers/#build2) `nlohmann-json`
|
- [**build2**](https://json.nlohmann.me/integration/package_managers/#build2) `nlohmann-json`
|
||||||
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/CPM.png" height="20"> [**cpm.cmake**](https://json.nlohmann.me/integration/package_managers/#cpmcmake) `gh:nlohmann/json`
|
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/CPM.png" height="20"> [**cpm.cmake**](https://json.nlohmann.me/integration/package_managers/#cpmcmake) `gh:nlohmann/json`
|
||||||
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/xmake.svg" height="20"> [**xmake**](https://json.nlohmann.me/integration/package_managers/#xmake) `nlohmann_json`
|
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/xmake.svg" height="20"> [**xmake**](https://json.nlohmann.me/integration/package_managers/#xmake) `nlohmann_json`
|
||||||
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/nix.svg" height="20"> [**Nix**](https://json.nlohmann.me/integration/package_managers/#nix) `nlohmann_json`
|
|
||||||
|
|
||||||
The library is part of many package managers. See the [**documentation**](https://json.nlohmann.me/integration/package_managers/) for detailed descriptions and examples.
|
The library is part of many package managers. See the [**documentation**](https://json.nlohmann.me/integration/package_managers/) for detailed descriptions and examples.
|
||||||
|
|
||||||
|
|||||||
@@ -1,187 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
||||||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
|
||||||
|
|
||||||
<svg
|
|
||||||
width="535"
|
|
||||||
height="535"
|
|
||||||
viewBox="0 0 501.56251 501.56249"
|
|
||||||
id="svg2"
|
|
||||||
version="1.1"
|
|
||||||
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25)"
|
|
||||||
sodipodi:docname="nix-snowflake-colours.svg"
|
|
||||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
|
||||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
|
||||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
xmlns:svg="http://www.w3.org/2000/svg"
|
|
||||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
|
||||||
xmlns:cc="http://creativecommons.org/ns#"
|
|
||||||
xmlns:dc="http://purl.org/dc/elements/1.1/">
|
|
||||||
<defs
|
|
||||||
id="defs4">
|
|
||||||
<linearGradient
|
|
||||||
inkscape:collect="always"
|
|
||||||
id="linearGradient5562">
|
|
||||||
<stop
|
|
||||||
style="stop-color:#699ad7;stop-opacity:1"
|
|
||||||
offset="0"
|
|
||||||
id="stop5564" />
|
|
||||||
<stop
|
|
||||||
id="stop5566"
|
|
||||||
offset="0.24345198"
|
|
||||||
style="stop-color:#7eb1dd;stop-opacity:1" />
|
|
||||||
<stop
|
|
||||||
style="stop-color:#7ebae4;stop-opacity:1"
|
|
||||||
offset="1"
|
|
||||||
id="stop5568" />
|
|
||||||
</linearGradient>
|
|
||||||
<linearGradient
|
|
||||||
inkscape:collect="always"
|
|
||||||
id="linearGradient5053">
|
|
||||||
<stop
|
|
||||||
style="stop-color:#415e9a;stop-opacity:1"
|
|
||||||
offset="0"
|
|
||||||
id="stop5055" />
|
|
||||||
<stop
|
|
||||||
id="stop5057"
|
|
||||||
offset="0.23168644"
|
|
||||||
style="stop-color:#4a6baf;stop-opacity:1" />
|
|
||||||
<stop
|
|
||||||
style="stop-color:#5277c3;stop-opacity:1"
|
|
||||||
offset="1"
|
|
||||||
id="stop5059" />
|
|
||||||
</linearGradient>
|
|
||||||
<linearGradient
|
|
||||||
inkscape:collect="always"
|
|
||||||
xlink:href="#linearGradient5562"
|
|
||||||
id="linearGradient4328"
|
|
||||||
gradientUnits="userSpaceOnUse"
|
|
||||||
gradientTransform="translate(70.650339,-1055.1511)"
|
|
||||||
x1="200.59668"
|
|
||||||
y1="351.41116"
|
|
||||||
x2="290.08701"
|
|
||||||
y2="506.18814" />
|
|
||||||
<linearGradient
|
|
||||||
inkscape:collect="always"
|
|
||||||
xlink:href="#linearGradient5053"
|
|
||||||
id="linearGradient4330"
|
|
||||||
gradientUnits="userSpaceOnUse"
|
|
||||||
gradientTransform="translate(864.69589,-1491.3405)"
|
|
||||||
x1="-584.19934"
|
|
||||||
y1="782.33563"
|
|
||||||
x2="-496.29703"
|
|
||||||
y2="937.71399" />
|
|
||||||
</defs>
|
|
||||||
<sodipodi:namedview
|
|
||||||
id="base"
|
|
||||||
pagecolor="#ffffff"
|
|
||||||
bordercolor="#666666"
|
|
||||||
borderopacity="1.0"
|
|
||||||
inkscape:pageopacity="0.0"
|
|
||||||
inkscape:pageshadow="2"
|
|
||||||
inkscape:zoom="0.70904368"
|
|
||||||
inkscape:cx="99.429699"
|
|
||||||
inkscape:cy="195.33352"
|
|
||||||
inkscape:document-units="px"
|
|
||||||
inkscape:current-layer="layer3"
|
|
||||||
showgrid="false"
|
|
||||||
inkscape:window-width="1920"
|
|
||||||
inkscape:window-height="1050"
|
|
||||||
inkscape:window-x="1920"
|
|
||||||
inkscape:window-y="30"
|
|
||||||
inkscape:window-maximized="1"
|
|
||||||
inkscape:snap-global="true"
|
|
||||||
fit-margin-top="0"
|
|
||||||
fit-margin-left="0"
|
|
||||||
fit-margin-right="0"
|
|
||||||
fit-margin-bottom="0"
|
|
||||||
inkscape:showpageshadow="2"
|
|
||||||
inkscape:pagecheckerboard="0"
|
|
||||||
inkscape:deskcolor="#d1d1d1" />
|
|
||||||
<metadata
|
|
||||||
id="metadata7">
|
|
||||||
<rdf:RDF>
|
|
||||||
<cc:Work
|
|
||||||
rdf:about="">
|
|
||||||
<dc:format>image/svg+xml</dc:format>
|
|
||||||
<dc:type
|
|
||||||
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
|
||||||
</cc:Work>
|
|
||||||
</rdf:RDF>
|
|
||||||
</metadata>
|
|
||||||
<g
|
|
||||||
inkscape:groupmode="layer"
|
|
||||||
id="layer3"
|
|
||||||
inkscape:label="gradient-logo"
|
|
||||||
style="display:inline;opacity:1"
|
|
||||||
transform="translate(-156.41121,933.30685)">
|
|
||||||
<g
|
|
||||||
id="g2"
|
|
||||||
transform="matrix(0.99994059,0,0,0.99994059,-0.06321798,33.188377)"
|
|
||||||
style="stroke-width:1.00006">
|
|
||||||
<path
|
|
||||||
sodipodi:nodetypes="cccccccccc"
|
|
||||||
inkscape:connector-curvature="0"
|
|
||||||
id="path3336-6"
|
|
||||||
d="m 309.54892,-710.38827 122.19683,211.67512 -56.15706,0.5268 -32.6236,-56.8692 -32.85645,56.5653 -27.90237,-0.011 -14.29086,-24.6896 46.81047,-80.4901 -33.22946,-57.8257 z"
|
|
||||||
style="opacity:1;fill:url(#linearGradient4328);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:3.00018;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
|
|
||||||
<use
|
|
||||||
height="100%"
|
|
||||||
width="100%"
|
|
||||||
transform="rotate(60,407.11155,-715.78724)"
|
|
||||||
id="use3439-6"
|
|
||||||
inkscape:transform-center-y="151.59082"
|
|
||||||
inkscape:transform-center-x="124.43045"
|
|
||||||
xlink:href="#path3336-6"
|
|
||||||
y="0"
|
|
||||||
x="0"
|
|
||||||
style="stroke-width:1.00006" />
|
|
||||||
<use
|
|
||||||
height="100%"
|
|
||||||
width="100%"
|
|
||||||
transform="rotate(-60,407.31177,-715.70016)"
|
|
||||||
id="use3445-0"
|
|
||||||
inkscape:transform-center-y="75.573958"
|
|
||||||
inkscape:transform-center-x="-168.20651"
|
|
||||||
xlink:href="#path3336-6"
|
|
||||||
y="0"
|
|
||||||
x="0"
|
|
||||||
style="stroke-width:1.00006" />
|
|
||||||
<use
|
|
||||||
height="100%"
|
|
||||||
width="100%"
|
|
||||||
transform="rotate(180,407.41868,-715.7565)"
|
|
||||||
id="use3449-5"
|
|
||||||
inkscape:transform-center-y="-139.94592"
|
|
||||||
inkscape:transform-center-x="59.669705"
|
|
||||||
xlink:href="#path3336-6"
|
|
||||||
y="0"
|
|
||||||
x="0"
|
|
||||||
style="stroke-width:1.00006" />
|
|
||||||
<path
|
|
||||||
style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;fill:url(#linearGradient4330);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:3.00018;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
|
|
||||||
d="m 309.54892,-710.38827 122.19683,211.67512 -56.15706,0.5268 -32.6236,-56.8692 -32.85645,56.5653 -27.90237,-0.011 -14.29086,-24.6896 46.81047,-80.4901 -33.22946,-57.8256 z"
|
|
||||||
id="path4260-0"
|
|
||||||
inkscape:connector-curvature="0"
|
|
||||||
sodipodi:nodetypes="cccccccccc" />
|
|
||||||
<use
|
|
||||||
height="100%"
|
|
||||||
width="100%"
|
|
||||||
transform="rotate(120,407.33916,-716.08356)"
|
|
||||||
id="use4354-5"
|
|
||||||
xlink:href="#path4260-0"
|
|
||||||
y="0"
|
|
||||||
x="0"
|
|
||||||
style="display:inline;stroke-width:1.00006" />
|
|
||||||
<use
|
|
||||||
height="100%"
|
|
||||||
width="100%"
|
|
||||||
transform="rotate(-120,407.28823,-715.86995)"
|
|
||||||
id="use4362-2"
|
|
||||||
xlink:href="#path4260-0"
|
|
||||||
y="0"
|
|
||||||
x="0"
|
|
||||||
style="display:inline;stroke-width:1.00006" />
|
|
||||||
</g>
|
|
||||||
</g>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 6.4 KiB |
@@ -1,7 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.15)
|
|
||||||
project(json_example)
|
|
||||||
|
|
||||||
find_package(nlohmann_json CONFIG REQUIRED)
|
|
||||||
|
|
||||||
add_executable(json_example example.cpp)
|
|
||||||
target_link_libraries(json_example PRIVATE nlohmann_json::nlohmann_json)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <iostream>
|
|
||||||
#include <iomanip>
|
|
||||||
|
|
||||||
using nlohmann::json;
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
std::cout << std::setw(4) << json::meta() << std::endl;
|
|
||||||
}
|
|
||||||
@@ -14,8 +14,7 @@
|
|||||||
{: style="height:1em"} [**Conda**](#conda) `nlohmann_json`  
|
{: style="height:1em"} [**Conda**](#conda) `nlohmann_json`  
|
||||||
{: style="height:1em"} [**MacPorts**](#macports) `nlohmann-json`<br>
|
{: style="height:1em"} [**MacPorts**](#macports) `nlohmann-json`<br>
|
||||||
{: style="height:1em"} [**CPM.cmake**](#cpmcmake) `gh:nlohmann/json`  
|
{: style="height:1em"} [**CPM.cmake**](#cpmcmake) `gh:nlohmann/json`  
|
||||||
{: style="height:1em"} [**xmake**](#xmake) `nlohmann_json`  
|
{: style="height:1em"} [**xmake**](#xmake) `nlohmann_json`
|
||||||
{: style="height:1em"} [**Nix**](#nix) `nlohmann_json`
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
## Running example
|
## Running example
|
||||||
@@ -907,56 +906,6 @@ CPMAddPackage("gh:nlohmann/json@3.12.0")
|
|||||||
|
|
||||||
* * *
|
* * *
|
||||||
|
|
||||||
## Nix
|
|
||||||
|
|
||||||
!!! abstract "Summary"
|
|
||||||
|
|
||||||
package: [**`nlohmann_json`**](https://search.nixos.org/packages?channel=unstable&show=nlohmann_json&query=nlohmann_json)
|
|
||||||
|
|
||||||
- :octicons-tag-24: Available versions: current version of each Nixpkgs channel (may lag behind the latest release)
|
|
||||||
- :octicons-rocket-24: The package is maintained by the Nixpkgs community and updated after releases.
|
|
||||||
- :octicons-file-24: File issues at the [Nixpkgs issue tracker](https://github.com/NixOS/nixpkgs/issues)
|
|
||||||
- :octicons-question-24: [Nix website](https://nixos.org)
|
|
||||||
|
|
||||||
If you are using [Nix](https://nixos.org) or [NixOS](https://nixos.org/manual/nixos/stable/), the library is available
|
|
||||||
as the [nlohmann_json](https://search.nixos.org/packages?channel=unstable&show=nlohmann_json&query=nlohmann_json)
|
|
||||||
package in [Nixpkgs](https://github.com/NixOS/nixpkgs). To try it in a temporary shell, execute
|
|
||||||
|
|
||||||
```shell
|
|
||||||
nix-shell -p nlohmann_json
|
|
||||||
```
|
|
||||||
|
|
||||||
On NixOS, the package can be installed system-wide by adding it to `configuration.nix`:
|
|
||||||
|
|
||||||
```nix
|
|
||||||
environment.systemPackages = [ pkgs.nlohmann_json ];
|
|
||||||
```
|
|
||||||
|
|
||||||
??? example "Example: CMake"
|
|
||||||
|
|
||||||
1. Create the following files:
|
|
||||||
|
|
||||||
```cpp title="example.cpp"
|
|
||||||
--8<-- "integration/nix/example.cpp"
|
|
||||||
```
|
|
||||||
|
|
||||||
```cmake title="CMakeLists.txt"
|
|
||||||
--8<-- "integration/nix/CMakeLists.txt"
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Enter a shell that provides the package, CMake, and a C++ compiler:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
nix-shell -p nlohmann_json cmake
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Compile the code:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
cmake -S . -B build
|
|
||||||
cmake --build build
|
|
||||||
```
|
|
||||||
|
|
||||||
## Other package managers
|
## Other package managers
|
||||||
|
|
||||||
The library is also contained in many other package repositories; [Repology](https://repology.org/project/nlohmann-json/versions) tracks the packaged
|
The library is also contained in many other package repositories; [Repology](https://repology.org/project/nlohmann-json/versions) tracks the packaged
|
||||||
|
|||||||
@@ -1927,6 +1927,21 @@ class binary_reader
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief move past a value of @a size bytes, starting with @a head, that a
|
||||||
|
bulk run reads, as get() and get_number() would
|
||||||
|
|
||||||
|
Called before the value is passed on, so that an error reported while
|
||||||
|
passing it on (see @ref emit_float) has the position and the last byte
|
||||||
|
that reading the value byte by byte gives.
|
||||||
|
*/
|
||||||
|
void bulk_advance(const std::size_t size, const unsigned char head)
|
||||||
|
{
|
||||||
|
ia.bulk_skip(size);
|
||||||
|
chars_read += size;
|
||||||
|
current = head;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read the integers, floats, booleans, and nulls that follow in a
|
@brief read the integers, floats, booleans, and nulls that follow in a
|
||||||
definite-length array, without the per-element bookkeeping of
|
definite-length array, without the per-element bookkeeping of
|
||||||
@@ -1952,71 +1967,70 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
const unsigned char head = data[0];
|
const unsigned char head = data[0];
|
||||||
std::size_t size = 0;
|
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
switch (head)
|
switch (head)
|
||||||
{
|
{
|
||||||
case 0x18:
|
case 0x18:
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_unsigned(data[1]);
|
accepted = emit_unsigned(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0x19:
|
case 0x19:
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x1A:
|
case 0x1A:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x1B:
|
case 0x1B:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x38:
|
case 0x38:
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_cbor_negative_integer(data[1]);
|
accepted = emit_cbor_negative_integer(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0x39:
|
case 0x39:
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x3A:
|
case 0x3A:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x3B:
|
case 0x3B:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xF4:
|
case 0xF4:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(false);
|
accepted = sax->boolean(false);
|
||||||
break;
|
break;
|
||||||
case 0xF5:
|
case 0xF5:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(true);
|
accepted = sax->boolean(true);
|
||||||
break;
|
break;
|
||||||
case 0xF6:
|
case 0xF6:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->null();
|
accepted = sax->null();
|
||||||
break;
|
break;
|
||||||
case 0xFA:
|
case 0xFA:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xFB:
|
case 0xFB:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (head <= 0x17)
|
if (head <= 0x17)
|
||||||
{
|
{
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||||
}
|
}
|
||||||
else if (head >= 0x20 && head <= 0x37)
|
else if (head >= 0x20 && head <= 0x37)
|
||||||
{
|
{
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -2026,10 +2040,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// as if read by get() and get_number(): current is the head byte
|
|
||||||
ia.bulk_skip(size);
|
|
||||||
chars_read += size;
|
|
||||||
current = head;
|
|
||||||
--remaining;
|
--remaining;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||||
{
|
{
|
||||||
@@ -2867,73 +2877,72 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
const unsigned char head = data[0];
|
const unsigned char head = data[0];
|
||||||
std::size_t size = 0;
|
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
switch (head)
|
switch (head)
|
||||||
{
|
{
|
||||||
case 0xC0: // nil
|
case 0xC0: // nil
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->null();
|
accepted = sax->null();
|
||||||
break;
|
break;
|
||||||
case 0xC2: // false
|
case 0xC2: // false
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(false);
|
accepted = sax->boolean(false);
|
||||||
break;
|
break;
|
||||||
case 0xC3: // true
|
case 0xC3: // true
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(true);
|
accepted = sax->boolean(true);
|
||||||
break;
|
break;
|
||||||
case 0xCA: // float 32
|
case 0xCA: // float 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCB: // float 64
|
case 0xCB: // float 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCC: // uint 8
|
case 0xCC: // uint 8
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_unsigned(data[1]);
|
accepted = emit_unsigned(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0xCD: // uint 16
|
case 0xCD: // uint 16
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCE: // uint 32
|
case 0xCE: // uint 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCF: // uint 64
|
case 0xCF: // uint 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD0: // int 8
|
case 0xD0: // int 8
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
||||||
break;
|
break;
|
||||||
case 0xD1: // int 16
|
case 0xD1: // int 16
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD2: // int 32
|
case 0xD2: // int 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD3: // int 64
|
case 0xD3: // int 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (head <= 0x7F)
|
if (head <= 0x7F)
|
||||||
{
|
{
|
||||||
// positive fixint
|
// positive fixint
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||||
}
|
}
|
||||||
else if (head >= 0xE0)
|
else if (head >= 0xE0)
|
||||||
{
|
{
|
||||||
// negative fixint
|
// negative fixint
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -2943,10 +2952,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// as if read by get() and get_number(): current is the last byte
|
|
||||||
ia.bulk_skip(size);
|
|
||||||
chars_read += size;
|
|
||||||
current = data[size - 1];
|
|
||||||
--remaining;
|
--remaining;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||||
{
|
{
|
||||||
@@ -3850,6 +3855,7 @@ class binary_reader
|
|||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
// UBJSON stores numbers big-endian, BJData little-endian
|
// UBJSON stores numbers big-endian, BJData little-endian
|
||||||
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
||||||
|
const std::size_t start = chars_read;
|
||||||
std::size_t read = 0;
|
std::size_t read = 0;
|
||||||
bool accepted = true;
|
bool accepted = true;
|
||||||
while (read < count && accepted)
|
while (read < count && accepted)
|
||||||
@@ -3861,16 +3867,39 @@ class binary_reader
|
|||||||
byte_swap(number);
|
byte_swap(number);
|
||||||
}
|
}
|
||||||
++read;
|
++read;
|
||||||
|
// where passing the value on can report an error (see
|
||||||
|
// emit_float), the position is that after the value, as if it
|
||||||
|
// was read by get_number()
|
||||||
|
set_chars_read(start + (read * size), std::integral_constant<bool, may_overflow_number_float<NumberType>()> {});
|
||||||
accepted = emit_ubjson_number(number);
|
accepted = emit_ubjson_number(number);
|
||||||
}
|
}
|
||||||
// as if read by get_number(): current is the last byte read
|
// get_number() does not change current
|
||||||
ia.bulk_skip(read * size);
|
ia.bulk_skip(read * size);
|
||||||
chars_read += read * size;
|
chars_read = start + (read * size);
|
||||||
current = data[(read * size) - 1];
|
|
||||||
remaining -= read;
|
remaining -= read;
|
||||||
return accepted;
|
return accepted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief whether a value of type @a NumberType can be finite and still
|
||||||
|
overflow number_float_t, which @ref emit_float reports as an error
|
||||||
|
|
||||||
|
False for the default number types, so that the loop in @ref
|
||||||
|
parse_ubjson_typed_run need not track the position per value.
|
||||||
|
*/
|
||||||
|
template<typename NumberType>
|
||||||
|
static constexpr bool may_overflow_number_float() noexcept
|
||||||
|
{
|
||||||
|
return static_cast<long double>((std::numeric_limits<number_float_t>::max)()) < static_cast<long double>((std::numeric_limits<NumberType>::max)());
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept
|
||||||
|
{
|
||||||
|
chars_read = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {}
|
||||||
|
|
||||||
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
||||||
bool emit_ubjson_number(const NumberType number)
|
bool emit_ubjson_number(const NumberType number)
|
||||||
{
|
{
|
||||||
|
|||||||
Generated
+72
-43
@@ -20605,6 +20605,21 @@ class binary_reader
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief move past a value of @a size bytes, starting with @a head, that a
|
||||||
|
bulk run reads, as get() and get_number() would
|
||||||
|
|
||||||
|
Called before the value is passed on, so that an error reported while
|
||||||
|
passing it on (see @ref emit_float) has the position and the last byte
|
||||||
|
that reading the value byte by byte gives.
|
||||||
|
*/
|
||||||
|
void bulk_advance(const std::size_t size, const unsigned char head)
|
||||||
|
{
|
||||||
|
ia.bulk_skip(size);
|
||||||
|
chars_read += size;
|
||||||
|
current = head;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read the integers, floats, booleans, and nulls that follow in a
|
@brief read the integers, floats, booleans, and nulls that follow in a
|
||||||
definite-length array, without the per-element bookkeeping of
|
definite-length array, without the per-element bookkeeping of
|
||||||
@@ -20630,71 +20645,70 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
const unsigned char head = data[0];
|
const unsigned char head = data[0];
|
||||||
std::size_t size = 0;
|
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
switch (head)
|
switch (head)
|
||||||
{
|
{
|
||||||
case 0x18:
|
case 0x18:
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_unsigned(data[1]);
|
accepted = emit_unsigned(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0x19:
|
case 0x19:
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x1A:
|
case 0x1A:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x1B:
|
case 0x1B:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x38:
|
case 0x38:
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_cbor_negative_integer(data[1]);
|
accepted = emit_cbor_negative_integer(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0x39:
|
case 0x39:
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x3A:
|
case 0x3A:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x3B:
|
case 0x3B:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xF4:
|
case 0xF4:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(false);
|
accepted = sax->boolean(false);
|
||||||
break;
|
break;
|
||||||
case 0xF5:
|
case 0xF5:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(true);
|
accepted = sax->boolean(true);
|
||||||
break;
|
break;
|
||||||
case 0xF6:
|
case 0xF6:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->null();
|
accepted = sax->null();
|
||||||
break;
|
break;
|
||||||
case 0xFA:
|
case 0xFA:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xFB:
|
case 0xFB:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (head <= 0x17)
|
if (head <= 0x17)
|
||||||
{
|
{
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||||
}
|
}
|
||||||
else if (head >= 0x20 && head <= 0x37)
|
else if (head >= 0x20 && head <= 0x37)
|
||||||
{
|
{
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -20704,10 +20718,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// as if read by get() and get_number(): current is the head byte
|
|
||||||
ia.bulk_skip(size);
|
|
||||||
chars_read += size;
|
|
||||||
current = head;
|
|
||||||
--remaining;
|
--remaining;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||||
{
|
{
|
||||||
@@ -21545,73 +21555,72 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
const unsigned char head = data[0];
|
const unsigned char head = data[0];
|
||||||
std::size_t size = 0;
|
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
switch (head)
|
switch (head)
|
||||||
{
|
{
|
||||||
case 0xC0: // nil
|
case 0xC0: // nil
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->null();
|
accepted = sax->null();
|
||||||
break;
|
break;
|
||||||
case 0xC2: // false
|
case 0xC2: // false
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(false);
|
accepted = sax->boolean(false);
|
||||||
break;
|
break;
|
||||||
case 0xC3: // true
|
case 0xC3: // true
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(true);
|
accepted = sax->boolean(true);
|
||||||
break;
|
break;
|
||||||
case 0xCA: // float 32
|
case 0xCA: // float 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCB: // float 64
|
case 0xCB: // float 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCC: // uint 8
|
case 0xCC: // uint 8
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_unsigned(data[1]);
|
accepted = emit_unsigned(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0xCD: // uint 16
|
case 0xCD: // uint 16
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCE: // uint 32
|
case 0xCE: // uint 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCF: // uint 64
|
case 0xCF: // uint 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD0: // int 8
|
case 0xD0: // int 8
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
||||||
break;
|
break;
|
||||||
case 0xD1: // int 16
|
case 0xD1: // int 16
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD2: // int 32
|
case 0xD2: // int 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD3: // int 64
|
case 0xD3: // int 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (head <= 0x7F)
|
if (head <= 0x7F)
|
||||||
{
|
{
|
||||||
// positive fixint
|
// positive fixint
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||||
}
|
}
|
||||||
else if (head >= 0xE0)
|
else if (head >= 0xE0)
|
||||||
{
|
{
|
||||||
// negative fixint
|
// negative fixint
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -21621,10 +21630,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// as if read by get() and get_number(): current is the last byte
|
|
||||||
ia.bulk_skip(size);
|
|
||||||
chars_read += size;
|
|
||||||
current = data[size - 1];
|
|
||||||
--remaining;
|
--remaining;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||||
{
|
{
|
||||||
@@ -22528,6 +22533,7 @@ class binary_reader
|
|||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
// UBJSON stores numbers big-endian, BJData little-endian
|
// UBJSON stores numbers big-endian, BJData little-endian
|
||||||
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
||||||
|
const std::size_t start = chars_read;
|
||||||
std::size_t read = 0;
|
std::size_t read = 0;
|
||||||
bool accepted = true;
|
bool accepted = true;
|
||||||
while (read < count && accepted)
|
while (read < count && accepted)
|
||||||
@@ -22539,16 +22545,39 @@ class binary_reader
|
|||||||
byte_swap(number);
|
byte_swap(number);
|
||||||
}
|
}
|
||||||
++read;
|
++read;
|
||||||
|
// where passing the value on can report an error (see
|
||||||
|
// emit_float), the position is that after the value, as if it
|
||||||
|
// was read by get_number()
|
||||||
|
set_chars_read(start + (read * size), std::integral_constant<bool, may_overflow_number_float<NumberType>()> {});
|
||||||
accepted = emit_ubjson_number(number);
|
accepted = emit_ubjson_number(number);
|
||||||
}
|
}
|
||||||
// as if read by get_number(): current is the last byte read
|
// get_number() does not change current
|
||||||
ia.bulk_skip(read * size);
|
ia.bulk_skip(read * size);
|
||||||
chars_read += read * size;
|
chars_read = start + (read * size);
|
||||||
current = data[(read * size) - 1];
|
|
||||||
remaining -= read;
|
remaining -= read;
|
||||||
return accepted;
|
return accepted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief whether a value of type @a NumberType can be finite and still
|
||||||
|
overflow number_float_t, which @ref emit_float reports as an error
|
||||||
|
|
||||||
|
False for the default number types, so that the loop in @ref
|
||||||
|
parse_ubjson_typed_run need not track the position per value.
|
||||||
|
*/
|
||||||
|
template<typename NumberType>
|
||||||
|
static constexpr bool may_overflow_number_float() noexcept
|
||||||
|
{
|
||||||
|
return static_cast<long double>((std::numeric_limits<number_float_t>::max)()) < static_cast<long double>((std::numeric_limits<NumberType>::max)());
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept
|
||||||
|
{
|
||||||
|
chars_read = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {}
|
||||||
|
|
||||||
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
||||||
bool emit_ubjson_number(const NumberType number)
|
bool emit_ubjson_number(const NumberType number)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using nlohmann::json;
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -367,3 +368,125 @@ TEST_CASE("Binary formats with narrow number types")
|
|||||||
CHECK(format.decode(format.encode(json(1e300)), false).is_discarded());
|
CHECK(format.decode(format.encode(json(1e300)), false).is_discarded());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
// records the SAX events of narrow_json, including the arguments of parse_error
|
||||||
|
struct narrow_event_recorder
|
||||||
|
{
|
||||||
|
using number_integer_t = narrow_json::number_integer_t;
|
||||||
|
using number_unsigned_t = narrow_json::number_unsigned_t;
|
||||||
|
using number_float_t = narrow_json::number_float_t;
|
||||||
|
using string_t = narrow_json::string_t;
|
||||||
|
using binary_t = narrow_json::binary_t;
|
||||||
|
|
||||||
|
bool null()
|
||||||
|
{
|
||||||
|
events.emplace_back("null");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool boolean(bool val)
|
||||||
|
{
|
||||||
|
events.emplace_back(val ? "true" : "false");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool number_integer(number_integer_t val)
|
||||||
|
{
|
||||||
|
events.push_back(std::to_string(val));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool number_unsigned(number_unsigned_t val)
|
||||||
|
{
|
||||||
|
events.push_back(std::to_string(val));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool number_float(number_float_t val, const string_t& /*unused*/)
|
||||||
|
{
|
||||||
|
events.push_back(std::to_string(val));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool string(string_t& val)
|
||||||
|
{
|
||||||
|
events.push_back(val);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool binary(binary_t& /*unused*/)
|
||||||
|
{
|
||||||
|
events.emplace_back("binary");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool start_object(std::size_t /*unused*/)
|
||||||
|
{
|
||||||
|
events.emplace_back("{");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool key(string_t& val)
|
||||||
|
{
|
||||||
|
events.push_back(val);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool end_object()
|
||||||
|
{
|
||||||
|
events.emplace_back("}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool start_array(std::size_t /*unused*/)
|
||||||
|
{
|
||||||
|
events.emplace_back("[");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool end_array()
|
||||||
|
{
|
||||||
|
events.emplace_back("]");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::detail::exception& ex)
|
||||||
|
{
|
||||||
|
events.push_back("parse_error(" + std::to_string(position) + ", " + last_token + ", " + ex.what() + ")");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> events {}; // NOLINT(readability-redundant-member-init)
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("Binary formats with narrow number types from contiguous and non-contiguous input")
|
||||||
|
{
|
||||||
|
// contiguous input reads runs of numbers in one loop; a number that
|
||||||
|
// overflows number_float_t must be reported with the same position and
|
||||||
|
// last byte as when the input is read byte by byte
|
||||||
|
const json values = {1, 70000, -70000, 1.5, 2.5, 1e300, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5};
|
||||||
|
const json doubles = {1.5, 2.5, 3.5, 1e300, 4.5, 5.5, 6.5, 7.5, 8.5};
|
||||||
|
|
||||||
|
struct binary_input
|
||||||
|
{
|
||||||
|
const char* name;
|
||||||
|
json::input_format_t format;
|
||||||
|
bytes data;
|
||||||
|
};
|
||||||
|
const std::vector<binary_input> inputs =
|
||||||
|
{
|
||||||
|
{"CBOR", json::input_format_t::cbor, json::to_cbor(values)},
|
||||||
|
{"CBOR doubles", json::input_format_t::cbor, json::to_cbor(doubles)},
|
||||||
|
{"MessagePack", json::input_format_t::msgpack, json::to_msgpack(values)},
|
||||||
|
{"MessagePack doubles", json::input_format_t::msgpack, json::to_msgpack(doubles)},
|
||||||
|
{"UBJSON", json::input_format_t::ubjson, json::to_ubjson(values, true)},
|
||||||
|
{"UBJSON typed doubles", json::input_format_t::ubjson, json::to_ubjson(doubles, true, true)},
|
||||||
|
{"BJData", json::input_format_t::bjdata, json::to_bjdata(values, true)},
|
||||||
|
{"BJData typed doubles", json::input_format_t::bjdata, json::to_bjdata(doubles, true, true)},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& input : inputs)
|
||||||
|
{
|
||||||
|
const std::string name = input.name;
|
||||||
|
CAPTURE(name)
|
||||||
|
const std::list<std::uint8_t> bytewise(input.data.begin(), input.data.end());
|
||||||
|
narrow_event_recorder from_contiguous;
|
||||||
|
narrow_event_recorder from_bytewise;
|
||||||
|
CHECK_FALSE(narrow_json::sax_parse(input.data, &from_contiguous, input.format));
|
||||||
|
CHECK_FALSE(narrow_json::sax_parse(bytewise.begin(), bytewise.end(), &from_bytewise, input.format));
|
||||||
|
CHECK(from_contiguous.events == from_bytewise.events);
|
||||||
|
REQUIRE(!from_contiguous.events.empty());
|
||||||
|
CHECK(from_contiguous.events.back().find("out_of_range.406") != std::string::npos);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user