JSON for Modern C++  2.1.1

◆ from_msgpack()

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_msgpack ( const std::vector< uint8_t > &  v,
const size_t  start_index = 0 
)
inlinestatic

Deserializes a given byte vector v to a JSON value using the MessagePack serialization format.

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

MessagePack type JSON value type first byte
positive fixint number_unsigned 0x00..0x7f
fixmap object 0x80..0x8f
fixarray array 0x90..0x9f
fixstr string 0xa0..0xbf
nil null 0xc0
false false 0xc2
true true 0xc3
float 32 number_float 0xca
float 64 number_float 0xcb
uint 8 number_unsigned 0xcc
uint 16 number_unsigned 0xcd
uint 32 number_unsigned 0xce
uint 64 number_unsigned 0xcf
int 8 number_integer 0xd0
int 16 number_integer 0xd1
int 32 number_integer 0xd2
int 64 number_integer 0xd3
str 8 string 0xd9
str 16 string 0xda
str 32 string 0xdb
array 16 array 0xdc
array 32 array 0xdd
map 16 object 0xde
map 32 object 0xdf
negative fixint number_integer 0xe0-0xff
Warning
The mapping is incomplete in the sense that not all MessagePack types can be converted to a JSON value. The following MessagePack types are not supported and will yield parse errors:
  • bin 8 - bin 32 (0xc4..0xc6)
  • ext 8 - ext 32 (0xc7..0xc9)
  • fixext 1 - fixext 16 (0xd4..0xd8)
Note
Any MessagePack output created to_msgpack can be successfully parsed by from_msgpack.
Parameters
[in]va byte vector in MessagePack format
[in]start_indexthe index to start reading from v (0 by default)
Returns
deserialized JSON value
Exceptions
parse_error.110if the given vector ends prematurely
parse_error.112if unsupported features from MessagePack were used in the given vector v or if the input is not valid MessagePack
parse_error.113if a string was expected as map key, but not found
Complexity
Linear in the size of the byte vector v.
Example
The example shows the deserialization of a byte vector in MessagePack format to a JSON value.
1 #include "json.hpp"
2 #include <iomanip> // for std::setw
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  // create byte vector
9  std::vector<uint8_t> v = {0x82, 0xa7, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
10  0x74, 0xc3, 0xa6, 0x73, 0x63, 0x68, 0x65, 0x6d,
11  0x61, 0x00
12  };
13 
14  // deserialize it with MessagePack
15  json j = json::from_msgpack(v);
16 
17  // print the deserialized JSON value
18  std::cout << std::setw(2) << j << std::endl;
19 }
basic_json<> json
default JSON class
Definition: json.hpp:13933
static basic_json from_msgpack(const std::vector< uint8_t > &v, const size_t start_index=0)
create a JSON value from a byte vector in MessagePack format
Definition: json.hpp:9178
Output (play with this example online):
{
  "compact": true,
  "schema": 0
}
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/from_msgpack.cpp -o from_msgpack 
See also
http://msgpack.org
to_msgpack(const basic_json&) for the analogous serialization
from_cbor(const std::vector<uint8_t>&, const size_t) for the related CBOR format
Since
version 2.0.9, parameter start_index since 2.1.1

Definition at line 9178 of file json.hpp.