Verify the BJData chapter against the library and the Draft 2/3/4 specifications and fix what did not match: - bjdata.md: correct the list of unused markers (N, not Z), the ND-array example (3x2, not 2x3), the conditions under which an object is packed as an ND-array (exactly three keys, row vectors, float-only elements for single/double, char 0..127, byte only in Draft 3), the binary table row and notes (which flag/draft combinations write [$B), the "$" type restriction (Draft 3, Z is excluded too), the H marker in the deserialization table, and the claim of a complete deserialization mapping. Document the supported drafts, that Draft 4 (SoA containers, extension type E) and column-major ND-arrays are rejected, ND-arrays of half floats, duplicate object keys, and link the Draft 3 specifications instead of master. - Add an API page for basic_json::bjdata_version_t with an example, and link it from the nav, the API index and to_bjdata. - Update binary_formats/index.md and binary_values.md, which still claimed that BJData has no binary type, and fix a wrong byte comment there. - Document parse_error.115 and the out_of_range.408 cases for from_bjdata, the precise other_error.502 condition for to_bjdata, and reword the note on truncated unsigned integers in exceptions.md (only ND-array elements were truncated before 3.13.0). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
34 lines
974 B
C++
34 lines
974 B
C++
#include <iomanip>
|
|
#include <iostream>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
using json = nlohmann::json;
|
|
|
|
void print_hex(const char* label, const std::vector<std::uint8_t>& v)
|
|
{
|
|
std::cout << label << ':';
|
|
for (auto b : v)
|
|
{
|
|
std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(b);
|
|
}
|
|
std::cout << std::dec << std::setfill(' ') << '\n';
|
|
}
|
|
|
|
int main()
|
|
{
|
|
// a binary value
|
|
json j = json::binary({0xCA, 0xFE, 0xBA, 0xBE});
|
|
|
|
// Draft 2 (the default) writes an array of integers
|
|
auto v2 = json::to_bjdata(j, true, true, json::bjdata_version_t::draft2);
|
|
print_hex("draft2", v2);
|
|
|
|
// Draft 3 writes an optimized binary array
|
|
auto v3 = json::to_bjdata(j, true, true, json::bjdata_version_t::draft3);
|
|
print_hex("draft3", v3);
|
|
|
|
// reading yields an array for Draft 2 and a binary value for Draft 3
|
|
std::cout << json::from_bjdata(v2) << '\n';
|
|
std::cout << json::from_bjdata(v3) << '\n';
|
|
}
|