nlohmann::basic_json::to_bjdata¶
// (1)
staticstd::vector<std::uint8_t>to_bjdata(constbasic_json&j,
constbooluse_size=false,
constbooluse_type=false,
constbjdata_version_tversion=bjdata_version_t::draft2);
// (2)
staticvoidto_bjdata(constbasic_json&j,detail::output_adapter<std::uint8_t>o,
constbooluse_size=false,constbooluse_type=false,
constbjdata_version_tversion=bjdata_version_t::draft2);
staticvoidto_bjdata(constbasic_json&j,detail::output_adapter<char>o,
constbooluse_size=false,constbooluse_type=false,
constbjdata_version_tversion=bjdata_version_t::draft2);
Serializes a given JSON value j to a byte vector using the BJData (Binary JData) serialization format. BJData aims to be more compact than JSON itself, yet more efficient to parse.
- Returns a byte vector containing the BJData serialization.
- Writes the BJData serialization to an output adapter.
The exact mapping and its limitations are described on a dedicated page.
Parameters¶
j(in)- JSON value to serialize
o(in)- output adapter to write serialization to
use_size(in)- whether to add size annotations to container types; optional,
falseby default. use_type(in)- whether to add type annotations to container types (must be combined with
use_size=true); optional,falseby default. version(in)- which version of BJData to use (see note on "Binary values" on BJData); optional,
bjdata_version_t::draft2by default.
Return value¶
- BJData serialization as byte vector
- (none)
Exception safety¶
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
Complexity¶
Linear in the size of the JSON value j.
Examples¶
Example
The example shows the serialization of a JSON value to a byte vector in BJData format.
#include<iostream>
#include<iomanip>
#include<nlohmann/json.hpp>
usingjson=nlohmann::json;
usingnamespacenlohmann::literals;
// function to print BJData's diagnostic format
voidprint_byte(uint8_tbyte)
{
if(32<byteandbyte<128)
{
std::cout<<(char)byte;
}
else
{
std::cout<<(int)byte;
}
}
intmain()
{
// create a JSON value
jsonj=R"({"compact": true, "schema": false})"_json;
// serialize it to BJData
std::vector<std::uint8_t>v=json::to_bjdata(j);
// print the vector content
for(auto&byte:v)
{
print_byte(byte);
}
std::cout<<std::endl;
// create an array of numbers
jsonarray={1,2,3,4,5,6,7,8};
// serialize it to BJData using default representation
std::vector<std::uint8_t>v_array=json::to_bjdata(array);
// serialize it to BJData using size optimization
std::vector<std::uint8_t>v_array_size=json::to_bjdata(array,true);
// serialize it to BJData using type optimization
std::vector<std::uint8_t>v_array_size_and_type=json::to_bjdata(array,true,true);
// print the vector contents
for(auto&byte:v_array)
{
print_byte(byte);
}
std::cout<<std::endl;
for(auto&byte:v_array_size)
{
print_byte(byte);
}
std::cout<<std::endl;
for(auto&byte:v_array_size_and_type)
{
print_byte(byte);
}
std::cout<<std::endl;
}
Output:
{i7compactTi6schemaF}
[i1i2i3i4i5i6i7i8]
[#i8i1i2i3i4i5i6i7i8
[$i#i812345678
See also¶
- from_bjdata create a JSON value from an input in BJData format
- to_cbor create a CBOR serialization of a JSON value
- to_msgpack create a MessagePack serialization of a JSON value
- to_bson create a BSON serialization of a JSON value
- to_ubjson create a UBJSON serialization of a JSON value
Version history¶
- Added in version 3.11.0.
- BJData version parameter (for draft3 binary encoding) added in version 3.12.0.