JSON for Modern C++  3.7.2

◆ from_ubjson() [2/2]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static basic_json nlohmann::basic_json::from_ubjson ( detail::input_adapter &&  i,
const bool  strict = true,
const bool  allow_exceptions = true 

Deserializes a given input i to a JSON value using the UBJSON (Universal Binary JSON) serialization format.

The library maps UBJSON types to JSON value types as follows:

UBJSON type JSON value type marker
no-op no value, next value is read N
null null Z
false false F
true true T
float32 number_float d
float64 number_float D
uint8 number_unsigned U
int8 number_integer i
int16 number_integer I
int32 number_integer l
int64 number_integer L
string string S
char string C
array array (optimized values are supported) [
object object (optimized values are supported) {
The mapping is complete in the sense that any UBJSON value can be converted to a JSON value.
[in]ian input in UBJSON format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if a parse error occurs
parse_error.113if a string could not be parsed successfully
Linear in the size of the input i.
The example shows the deserialization of a byte vector in UBJSON format to a JSON value.
1 #include <iostream>
2 #include <iomanip>
3 #include <nlohmann/json.hpp>
5 using json = nlohmann::json;
7 int main()
8 {
9  // create byte vector
10  std::vector<uint8_t> v = {0x7B, 0x69, 0x07, 0x63, 0x6F, 0x6D, 0x70, 0x61,
11  0x63, 0x74, 0x54, 0x69, 0x06, 0x73, 0x63, 0x68,
12  0x65, 0x6D, 0x61, 0x69, 0x00, 0x7D
13  };
15  // deserialize it with UBJSON
16  json j = json::from_ubjson(v);
18  // print the deserialized JSON value
19  std::cout << std::setw(2) << j << std::endl;
20 }

Output (play with this example online):
  "compact": true,
  "schema": 0
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/from_ubjson.cpp -o from_ubjson 
See also
to_ubjson(const basic_json&, const bool, const bool) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_bson(detail::input_adapter&&, const bool, const bool) for the related BSON format
version 3.1.0; added allow_exceptions parameter since 3.2.0

Definition at line 21726 of file json.hpp.

static basic_json from_ubjson(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in UBJSON format
Definition: json.hpp:21726
basic_json<> json
default JSON class
Definition: json.hpp:2445