Commit 543745a1 by Niels

starting to integrate MessagePack (#358)

parent 9ca00e48
......@@ -6142,6 +6142,499 @@ class basic_json
/// @}
/////////////////////////
// MessagePack support //
/////////////////////////
/// @name MessagePack support
/// @{
private:
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v)
{
switch (j.type())
{
case value_t::null:
{
// nil
v.push_back(0xc0);
break;
}
case value_t::boolean:
{
// true and false
v.push_back(j.m_value.boolean ? 0xc3 : 0xc2);
break;
}
case value_t::number_integer:
{
if (j.m_value.number_integer >= -32 and j.m_value.number_integer < 128)
{
// negative fixnum and positive fixnum
v.push_back(static_cast<uint8_t>(j.m_value.number_integer));
}
else if (j.m_value.number_integer >= INT8_MIN and j.m_value.number_integer <= INT8_MAX)
{
// int 8
v.push_back(0xd0);
v.push_back(static_cast<uint8_t>(j.m_value.number_integer));
}
else if (j.m_value.number_integer >= INT16_MIN and j.m_value.number_integer <= INT16_MAX)
{
// int 16
v.push_back(0xd1);
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_integer & 0xff));
}
else if (j.m_value.number_integer >= INT32_MIN and j.m_value.number_integer <= INT32_MAX)
{
// int 32
v.push_back(0xd2);
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_integer & 0xff));
}
else if (j.m_value.number_integer >= INT64_MIN and j.m_value.number_integer <= INT64_MAX)
{
// int 64
v.push_back(0xd3);
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 070) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 060) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 050) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 040) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_integer & 0xff));
}
break;
}
case value_t::number_unsigned:
{
if (j.m_value.number_unsigned < 128)
{
// positive fixnum
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned));
}
else if (j.m_value.number_unsigned <= UINT8_MAX)
{
// uint 8
v.push_back(0xcc);
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned));
}
else if (j.m_value.number_unsigned <= UINT16_MAX)
{
// uint 16
v.push_back(0xcd);
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned & 0xff));
}
else if (j.m_value.number_unsigned <= UINT32_MAX)
{
// uint 32
v.push_back(0xce);
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned & 0xff));
}
else if (j.m_value.number_unsigned <= UINT64_MAX)
{
// uint 64
v.push_back(0xcf);
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 070) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 060) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 050) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 040) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned & 0xff));
}
break;
}
case value_t::number_float:
{
// float 64
v.push_back(0xcb);
const uint8_t* helper = reinterpret_cast<const uint8_t*>(&(j.m_value.number_float));
for (size_t i = 0; i < 8; ++i)
{
v.push_back(helper[7 - i]);
}
break;
}
case value_t::string:
{
const auto N = j.m_value.string->size();
if (N <= 31)
{
// fixstr
v.push_back(static_cast<uint8_t>(0xa0 | N));
}
else if (N <= 255)
{
// str 8
v.push_back(0xd9);
v.push_back(static_cast<uint8_t>(N));
}
else if (N <= 65535)
{
// str 16
v.push_back(0xda);
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
else if (N <= 4294967295)
{
// str 32
v.push_back(0xdb);
v.push_back(static_cast<uint8_t>((N >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
// append string
std::copy(j.m_value.string->begin(), j.m_value.string->end(),
std::back_inserter(v));
break;
}
case value_t::array:
{
const auto N = j.m_value.array->size();
if (N <= 15)
{
// fixarray
v.push_back(static_cast<uint8_t>(0x90 | N));
}
else if (N <= 0xffff)
{
// array 16
v.push_back(0xdc);
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
else if (N <= 0xffffffff)
{
// array 32
v.push_back(0xdd);
v.push_back(static_cast<uint8_t>((N >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
// append each element
for (const auto& el : *j.m_value.array)
{
to_msgpack_internal(el, v);
}
break;
}
case value_t::object:
{
const auto N = j.m_value.object->size();
if (N <= 15)
{
// fixmap
v.push_back(static_cast<uint8_t>(0x80 | (N & 0xf)));
}
else if (N <= 65535)
{
// map 16
v.push_back(0xde);
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
else if (N <= 4294967295)
{
// map 32
v.push_back(0xdf);
v.push_back(static_cast<uint8_t>((N >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
// append each element
for (const auto& el : *j.m_value.object)
{
to_msgpack_internal(el.first, v);
to_msgpack_internal(el.second, v);
}
break;
}
default:
{
break;
}
}
}
/*!
@param[in] v MessagePack serialization
@param[in] idx byte index to start reading from @a v
*/
static basic_json from_msgpack_internal(const std::vector<uint8_t>& v, size_t& idx)
{
// store and increment index
const size_t current_idx = idx++;
if (v[current_idx] <= 0x7f) // positive fixint
{
return v[current_idx];
}
else if (v[current_idx] <= 0x8f) // fixmap
{
basic_json result = value_t::object;
const size_t len = v[current_idx] & 0x0f;
for (size_t i = 0; i < len; ++i)
{
std::string key = from_msgpack_internal(v, idx);
result[key] = from_msgpack_internal(v, idx);
}
return result;
}
else if (v[current_idx] <= 0x9f) // fixarray
{
basic_json result = value_t::array;
const size_t len = v[current_idx] & 0x0f;
for (size_t i = 0; i < len; ++i)
{
result.push_back(from_msgpack_internal(v, idx));
}
return result;
}
else if (v[current_idx] <= 0xbf) // fixstr
{
const size_t len = v[current_idx] & 0x1f;
const size_t offset = current_idx + 1;
idx += len; // skip content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xc0) // nil
{
return value_t::null;
}
else if (v[current_idx] == 0xc1) // never used
{
throw std::invalid_argument("value 0x31 must not be used@ " + std::to_string(current_idx));
}
else if (v[current_idx] == 0xc2) // false
{
return false;
}
else if (v[current_idx] == 0xc3) // true
{
return true;
}
else if (v[current_idx] >= 0xc4 and v[current_idx] <= 0xc9) // bin/ext
{
throw std::invalid_argument("bin/ext are not supported @ " + std::to_string(current_idx));
}
else if (v[current_idx] == 0xca) // float 32
{
// copy bytes in reverse order into the double variable
float res;
for (size_t byte = 0; byte < sizeof(float); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(float) - byte - 1] = v[current_idx + 1 + byte];
}
idx += sizeof(float); // skip content bytes
return res;
}
else if (v[current_idx] == 0xcb) // float 64
{
// copy bytes in reverse order into the double variable
double res;
for (size_t byte = 0; byte < sizeof(double); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(double) - byte - 1] = v[current_idx + 1 + byte];
}
idx += sizeof(double); // skip content bytes
return res;
}
else if (v[current_idx] == 0xcc) // uint 8
{
idx += 1; // skip content byte
return v[current_idx + 1];
}
else if (v[current_idx] == 0xcd) // uint 16
{
idx += 2; // skip 2 content bytes
return static_cast<uint16_t>((static_cast<uint16_t>(v[current_idx + 1]) << 010) +
static_cast<uint16_t>(v[current_idx + 2]));
}
else if (v[current_idx] == 0xce) // uint 32
{
idx += 4; // skip 4 content bytes
return static_cast<uint32_t>((static_cast<uint32_t>(v[current_idx + 1]) << 030) +
(static_cast<uint32_t>(v[current_idx + 2]) << 020) +
(static_cast<uint32_t>(v[current_idx + 3]) << 010) +
static_cast<uint32_t>(v[current_idx + 4]));
}
else if (v[current_idx] == 0xcf) // uint 64
{
idx += 8; // skip 8 content bytes
return static_cast<uint64_t>((static_cast<uint64_t>(v[current_idx + 1]) << 070) +
(static_cast<uint64_t>(v[current_idx + 2]) << 060) +
(static_cast<uint64_t>(v[current_idx + 3]) << 050) +
(static_cast<uint64_t>(v[current_idx + 4]) << 040) +
(static_cast<uint64_t>(v[current_idx + 5]) << 030) +
(static_cast<uint64_t>(v[current_idx + 6]) << 020) +
(static_cast<uint64_t>(v[current_idx + 7]) << 010) +
static_cast<uint64_t>(v[current_idx + 8]));
}
else if (v[current_idx] == 0xd0) // int 8
{
idx += 1; // skip content byte
return static_cast<int8_t>(v[current_idx + 1]);
}
else if (v[current_idx] == 0xd1) // int 16
{
idx += 2; // skip 2 content bytes
return static_cast<int16_t>((static_cast<int16_t>(v[current_idx + 1]) << 010) +
static_cast<int16_t>(v[current_idx + 2]));
}
else if (v[current_idx] == 0xd2) // int 32
{
idx += 4; // skip 4 content bytes
return static_cast<int32_t>((static_cast<int32_t>(v[current_idx + 1]) << 030) +
(static_cast<int32_t>(v[current_idx + 2]) << 020) +
(static_cast<int32_t>(v[current_idx + 3]) << 010) +
static_cast<int32_t>(v[current_idx + 4]));
}
else if (v[current_idx] == 0xd3) // int 64
{
idx += 8; // skip 8 content bytes
return static_cast<int64_t>((static_cast<int64_t>(v[current_idx + 1]) << 070) +
(static_cast<int64_t>(v[current_idx + 2]) << 060) +
(static_cast<int64_t>(v[current_idx + 3]) << 050) +
(static_cast<int64_t>(v[current_idx + 4]) << 040) +
(static_cast<int64_t>(v[current_idx + 5]) << 030) +
(static_cast<int64_t>(v[current_idx + 6]) << 020) +
(static_cast<int64_t>(v[current_idx + 7]) << 010) +
static_cast<int64_t>(v[current_idx + 8]));
}
else if (v[current_idx] >= 0xd4 and v[current_idx] <= 0xd8) // fixext
{
throw std::invalid_argument("bin/ext are not supported @ " + std::to_string(current_idx));
}
else if (v[current_idx] == 0xd9) // str 8
{
const size_t len = v[current_idx + 1];
const size_t offset = current_idx + 2;
idx += len + 1; // skip size byte + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xda) // str 16
{
const size_t len = static_cast<size_t>((v[current_idx + 1] << 010) + v[current_idx + 2]);
const size_t offset = current_idx + 3;
idx += len + 2; // skip 2 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xdb) // str 32
{
const size_t len = static_cast<size_t>((v[current_idx + 1] << 030) +
(v[current_idx + 2] << 020) +
(v[current_idx + 3] << 010) +
v[current_idx + 4]);
const size_t offset = current_idx + 5;
idx += len + 4; // skip 4 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xdc) // array 16
{
basic_json result = value_t::array;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 010) +
v[current_idx + 2]);
idx += 2; // skip 2 size bytes
for (size_t i = 0; i < len; ++i)
{
result.push_back(from_msgpack_internal(v, idx));
}
return result;
}
else if (v[current_idx] == 0xdd) // array 32
{
basic_json result = value_t::array;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 030) +
(v[current_idx + 2] << 020) +
(v[current_idx + 3] << 010) +
v[current_idx + 4]);
idx += 4; // skip 4 size bytes
for (size_t i = 0; i < len; ++i)
{
result.push_back(from_msgpack_internal(v, idx));
}
return result;
}
else if (v[current_idx] == 0xde) // map 16
{
basic_json result = value_t::object;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 010) +
v[current_idx + 2]);
idx += 2; // skip 2 size bytes
for (size_t i = 0; i < len; ++i)
{
std::string key = from_msgpack_internal(v, idx);
result[key] = from_msgpack_internal(v, idx);
}
return result;
}
else if (v[current_idx] == 0xdf) // map 32
{
basic_json result = value_t::object;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 030) +
(v[current_idx + 2] << 020) +
(v[current_idx + 3] << 010) +
v[current_idx + 4]);
idx += 4; // skip 4 size bytes
for (size_t i = 0; i < len; ++i)
{
std::string key = from_msgpack_internal(v, idx);
result[key] = from_msgpack_internal(v, idx);
}
return result;
}
else if (v[current_idx] >= 0xe0) // negative fixint
{
return static_cast<int8_t>(v[current_idx]);
}
throw std::invalid_argument("error parsing a msgpack @ " + std::to_string(current_idx));
}
public:
/*!
@param[in] j JSON value to serialize
@retuen MessagePack serialization as char vector
*/
static std::vector<uint8_t> to_msgpack(const basic_json& j)
{
std::vector<uint8_t> result;
to_msgpack_internal(j, result);
return result;
}
static basic_json from_msgpack(const std::vector<uint8_t>& v)
{
size_t i = 0;
return from_msgpack_internal(v, i);
}
/// @}
private:
///////////////////////////
......
......@@ -6142,6 +6142,499 @@ class basic_json
/// @}
/////////////////////////
// MessagePack support //
/////////////////////////
/// @name MessagePack support
/// @{
private:
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v)
{
switch (j.type())
{
case value_t::null:
{
// nil
v.push_back(0xc0);
break;
}
case value_t::boolean:
{
// true and false
v.push_back(j.m_value.boolean ? 0xc3 : 0xc2);
break;
}
case value_t::number_integer:
{
if (j.m_value.number_integer >= -32 and j.m_value.number_integer < 128)
{
// negative fixnum and positive fixnum
v.push_back(static_cast<uint8_t>(j.m_value.number_integer));
}
else if (j.m_value.number_integer >= INT8_MIN and j.m_value.number_integer <= INT8_MAX)
{
// int 8
v.push_back(0xd0);
v.push_back(static_cast<uint8_t>(j.m_value.number_integer));
}
else if (j.m_value.number_integer >= INT16_MIN and j.m_value.number_integer <= INT16_MAX)
{
// int 16
v.push_back(0xd1);
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_integer & 0xff));
}
else if (j.m_value.number_integer >= INT32_MIN and j.m_value.number_integer <= INT32_MAX)
{
// int 32
v.push_back(0xd2);
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_integer & 0xff));
}
else if (j.m_value.number_integer >= INT64_MIN and j.m_value.number_integer <= INT64_MAX)
{
// int 64
v.push_back(0xd3);
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 070) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 060) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 050) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 040) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_integer >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_integer & 0xff));
}
break;
}
case value_t::number_unsigned:
{
if (j.m_value.number_unsigned < 128)
{
// positive fixnum
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned));
}
else if (j.m_value.number_unsigned <= UINT8_MAX)
{
// uint 8
v.push_back(0xcc);
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned));
}
else if (j.m_value.number_unsigned <= UINT16_MAX)
{
// uint 16
v.push_back(0xcd);
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned & 0xff));
}
else if (j.m_value.number_unsigned <= UINT32_MAX)
{
// uint 32
v.push_back(0xce);
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned & 0xff));
}
else if (j.m_value.number_unsigned <= UINT64_MAX)
{
// uint 64
v.push_back(0xcf);
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 070) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 060) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 050) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 040) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((j.m_value.number_unsigned >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(j.m_value.number_unsigned & 0xff));
}
break;
}
case value_t::number_float:
{
// float 64
v.push_back(0xcb);
const uint8_t* helper = reinterpret_cast<const uint8_t*>(&(j.m_value.number_float));
for (size_t i = 0; i < 8; ++i)
{
v.push_back(helper[7 - i]);
}
break;
}
case value_t::string:
{
const auto N = j.m_value.string->size();
if (N <= 31)
{
// fixstr
v.push_back(static_cast<uint8_t>(0xa0 | N));
}
else if (N <= 255)
{
// str 8
v.push_back(0xd9);
v.push_back(static_cast<uint8_t>(N));
}
else if (N <= 65535)
{
// str 16
v.push_back(0xda);
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
else if (N <= 4294967295)
{
// str 32
v.push_back(0xdb);
v.push_back(static_cast<uint8_t>((N >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
// append string
std::copy(j.m_value.string->begin(), j.m_value.string->end(),
std::back_inserter(v));
break;
}
case value_t::array:
{
const auto N = j.m_value.array->size();
if (N <= 15)
{
// fixarray
v.push_back(static_cast<uint8_t>(0x90 | N));
}
else if (N <= 0xffff)
{
// array 16
v.push_back(0xdc);
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
else if (N <= 0xffffffff)
{
// array 32
v.push_back(0xdd);
v.push_back(static_cast<uint8_t>((N >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
// append each element
for (const auto& el : *j.m_value.array)
{
to_msgpack_internal(el, v);
}
break;
}
case value_t::object:
{
const auto N = j.m_value.object->size();
if (N <= 15)
{
// fixmap
v.push_back(static_cast<uint8_t>(0x80 | (N & 0xf)));
}
else if (N <= 65535)
{
// map 16
v.push_back(0xde);
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
else if (N <= 4294967295)
{
// map 32
v.push_back(0xdf);
v.push_back(static_cast<uint8_t>((N >> 030) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 020) & 0xff));
v.push_back(static_cast<uint8_t>((N >> 010) & 0xff));
v.push_back(static_cast<uint8_t>(N & 0xff));
}
// append each element
for (const auto& el : *j.m_value.object)
{
to_msgpack_internal(el.first, v);
to_msgpack_internal(el.second, v);
}
break;
}
default:
{
break;
}
}
}
/*!
@param[in] v MessagePack serialization
@param[in] idx byte index to start reading from @a v
*/
static basic_json from_msgpack_internal(const std::vector<uint8_t>& v, size_t& idx)
{
// store and increment index
const size_t current_idx = idx++;
if (v[current_idx] <= 0x7f) // positive fixint
{
return v[current_idx];
}
else if (v[current_idx] <= 0x8f) // fixmap
{
basic_json result = value_t::object;
const size_t len = v[current_idx] & 0x0f;
for (size_t i = 0; i < len; ++i)
{
std::string key = from_msgpack_internal(v, idx);
result[key] = from_msgpack_internal(v, idx);
}
return result;
}
else if (v[current_idx] <= 0x9f) // fixarray
{
basic_json result = value_t::array;
const size_t len = v[current_idx] & 0x0f;
for (size_t i = 0; i < len; ++i)
{
result.push_back(from_msgpack_internal(v, idx));
}
return result;
}
else if (v[current_idx] <= 0xbf) // fixstr
{
const size_t len = v[current_idx] & 0x1f;
const size_t offset = current_idx + 1;
idx += len; // skip content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xc0) // nil
{
return value_t::null;
}
else if (v[current_idx] == 0xc1) // never used
{
throw std::invalid_argument("value 0x31 must not be used@ " + std::to_string(current_idx));
}
else if (v[current_idx] == 0xc2) // false
{
return false;
}
else if (v[current_idx] == 0xc3) // true
{
return true;
}
else if (v[current_idx] >= 0xc4 and v[current_idx] <= 0xc9) // bin/ext
{
throw std::invalid_argument("bin/ext are not supported @ " + std::to_string(current_idx));
}
else if (v[current_idx] == 0xca) // float 32
{
// copy bytes in reverse order into the double variable
float res;
for (size_t byte = 0; byte < sizeof(float); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(float) - byte - 1] = v[current_idx + 1 + byte];
}
idx += sizeof(float); // skip content bytes
return res;
}
else if (v[current_idx] == 0xcb) // float 64
{
// copy bytes in reverse order into the double variable
double res;
for (size_t byte = 0; byte < sizeof(double); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(double) - byte - 1] = v[current_idx + 1 + byte];
}
idx += sizeof(double); // skip content bytes
return res;
}
else if (v[current_idx] == 0xcc) // uint 8
{
idx += 1; // skip content byte
return v[current_idx + 1];
}
else if (v[current_idx] == 0xcd) // uint 16
{
idx += 2; // skip 2 content bytes
return static_cast<uint16_t>((static_cast<uint16_t>(v[current_idx + 1]) << 010) +
static_cast<uint16_t>(v[current_idx + 2]));
}
else if (v[current_idx] == 0xce) // uint 32
{
idx += 4; // skip 4 content bytes
return static_cast<uint32_t>((static_cast<uint32_t>(v[current_idx + 1]) << 030) +
(static_cast<uint32_t>(v[current_idx + 2]) << 020) +
(static_cast<uint32_t>(v[current_idx + 3]) << 010) +
static_cast<uint32_t>(v[current_idx + 4]));
}
else if (v[current_idx] == 0xcf) // uint 64
{
idx += 8; // skip 8 content bytes
return static_cast<uint64_t>((static_cast<uint64_t>(v[current_idx + 1]) << 070) +
(static_cast<uint64_t>(v[current_idx + 2]) << 060) +
(static_cast<uint64_t>(v[current_idx + 3]) << 050) +
(static_cast<uint64_t>(v[current_idx + 4]) << 040) +
(static_cast<uint64_t>(v[current_idx + 5]) << 030) +
(static_cast<uint64_t>(v[current_idx + 6]) << 020) +
(static_cast<uint64_t>(v[current_idx + 7]) << 010) +
static_cast<uint64_t>(v[current_idx + 8]));
}
else if (v[current_idx] == 0xd0) // int 8
{
idx += 1; // skip content byte
return static_cast<int8_t>(v[current_idx + 1]);
}
else if (v[current_idx] == 0xd1) // int 16
{
idx += 2; // skip 2 content bytes
return static_cast<int16_t>((static_cast<int16_t>(v[current_idx + 1]) << 010) +
static_cast<int16_t>(v[current_idx + 2]));
}
else if (v[current_idx] == 0xd2) // int 32
{
idx += 4; // skip 4 content bytes
return static_cast<int32_t>((static_cast<int32_t>(v[current_idx + 1]) << 030) +
(static_cast<int32_t>(v[current_idx + 2]) << 020) +
(static_cast<int32_t>(v[current_idx + 3]) << 010) +
static_cast<int32_t>(v[current_idx + 4]));
}
else if (v[current_idx] == 0xd3) // int 64
{
idx += 8; // skip 8 content bytes
return static_cast<int64_t>((static_cast<int64_t>(v[current_idx + 1]) << 070) +
(static_cast<int64_t>(v[current_idx + 2]) << 060) +
(static_cast<int64_t>(v[current_idx + 3]) << 050) +
(static_cast<int64_t>(v[current_idx + 4]) << 040) +
(static_cast<int64_t>(v[current_idx + 5]) << 030) +
(static_cast<int64_t>(v[current_idx + 6]) << 020) +
(static_cast<int64_t>(v[current_idx + 7]) << 010) +
static_cast<int64_t>(v[current_idx + 8]));
}
else if (v[current_idx] >= 0xd4 and v[current_idx] <= 0xd8) // fixext
{
throw std::invalid_argument("bin/ext are not supported @ " + std::to_string(current_idx));
}
else if (v[current_idx] == 0xd9) // str 8
{
const size_t len = v[current_idx + 1];
const size_t offset = current_idx + 2;
idx += len + 1; // skip size byte + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xda) // str 16
{
const size_t len = static_cast<size_t>((v[current_idx + 1] << 010) + v[current_idx + 2]);
const size_t offset = current_idx + 3;
idx += len + 2; // skip 2 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xdb) // str 32
{
const size_t len = static_cast<size_t>((v[current_idx + 1] << 030) +
(v[current_idx + 2] << 020) +
(v[current_idx + 3] << 010) +
v[current_idx + 4]);
const size_t offset = current_idx + 5;
idx += len + 4; // skip 4 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0xdc) // array 16
{
basic_json result = value_t::array;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 010) +
v[current_idx + 2]);
idx += 2; // skip 2 size bytes
for (size_t i = 0; i < len; ++i)
{
result.push_back(from_msgpack_internal(v, idx));
}
return result;
}
else if (v[current_idx] == 0xdd) // array 32
{
basic_json result = value_t::array;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 030) +
(v[current_idx + 2] << 020) +
(v[current_idx + 3] << 010) +
v[current_idx + 4]);
idx += 4; // skip 4 size bytes
for (size_t i = 0; i < len; ++i)
{
result.push_back(from_msgpack_internal(v, idx));
}
return result;
}
else if (v[current_idx] == 0xde) // map 16
{
basic_json result = value_t::object;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 010) +
v[current_idx + 2]);
idx += 2; // skip 2 size bytes
for (size_t i = 0; i < len; ++i)
{
std::string key = from_msgpack_internal(v, idx);
result[key] = from_msgpack_internal(v, idx);
}
return result;
}
else if (v[current_idx] == 0xdf) // map 32
{
basic_json result = value_t::object;
const size_t len = static_cast<size_t>((v[current_idx + 1] << 030) +
(v[current_idx + 2] << 020) +
(v[current_idx + 3] << 010) +
v[current_idx + 4]);
idx += 4; // skip 4 size bytes
for (size_t i = 0; i < len; ++i)
{
std::string key = from_msgpack_internal(v, idx);
result[key] = from_msgpack_internal(v, idx);
}
return result;
}
else if (v[current_idx] >= 0xe0) // negative fixint
{
return static_cast<int8_t>(v[current_idx]);
}
throw std::invalid_argument("error parsing a msgpack @ " + std::to_string(current_idx));
}
public:
/*!
@param[in] j JSON value to serialize
@retuen MessagePack serialization as char vector
*/
static std::vector<uint8_t> to_msgpack(const basic_json& j)
{
std::vector<uint8_t> result;
to_msgpack_internal(j, result);
return result;
}
static basic_json from_msgpack(const std::vector<uint8_t>& v)
{
size_t i = 0;
return from_msgpack_internal(v, i);
}
/// @}
private:
///////////////////////////
......@@ -7802,6 +8295,7 @@ class basic_json
re2c:define:YYMARKER = m_marker;
re2c:define:YYFILL = "fill_line_buffer(@@); // LCOV_EXCL_LINE";
re2c:define:YYFILL:naked = 1;
re2c:yyfill:enable = 1;
re2c:indent:string = " ";
re2c:indent:top = 1;
re2c:labelprefix = "basic_json_parser_";
......
......@@ -26,6 +26,7 @@ add_executable(${JSON_UNITTEST_TARGET_NAME}
"src/unit-json_patch.cpp"
"src/unit-json_pointer.cpp"
"src/unit-modifiers.cpp"
"src/unit-msgpack.cpp"
"src/unit-pointer_access.cpp"
"src/unit-readme.cpp"
"src/unit-reference_access.cpp"
......
......@@ -30,6 +30,7 @@ SOURCES = src/unit.cpp \
src/unit-json_patch.cpp \
src/unit-json_pointer.cpp \
src/unit-modifiers.cpp \
src/unit-msgpack.cpp \
src/unit-pointer_access.cpp \
src/unit-readme.cpp \
src/unit-reference_access.cpp \
......
menupopupmenuitemonclickCreateNewDoc()valueNewonclickOpenDoc()valueOpenonclickCloseDoc()valueCloseidfilevalueFile
\ No newline at end of file
‘£foo
\ No newline at end of file
£foo£bar
\ No newline at end of file
afoobar
\ No newline at end of file
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘¬Not too deep
\ No newline at end of file
·JSON Test Pattern pass3‚¬In this test°It is an object.³The outermost value»must be an object or array.
\ No newline at end of file
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 2.0.7
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright (c) 2013-2016 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "catch.hpp"
#include "json.hpp"
using nlohmann::json;
#include <fstream>
TEST_CASE("MessagePack")
{
SECTION("individual values")
{
SECTION("null")
{
json j = nullptr;
std::vector<uint8_t> expected = {0xc0};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("boolean")
{
SECTION("true")
{
json j = true;
std::vector<uint8_t> expected = {0xc3};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("false")
{
json j = false;
std::vector<uint8_t> expected = {0xc2};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("number")
{
SECTION("signed")
{
SECTION("-32..-1 (negative fixnum)")
{
for (auto i = -32; i <= -1; ++i)
{
CAPTURE(i);
// create JSON value with integer number
json j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 1);
// check individual bytes
CHECK(static_cast<int8_t>(result[0]) == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("0..127 (positive fixnum)")
{
for (size_t i = 0; i <= 127; ++i)
{
CAPTURE(i);
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 1);
// check individual bytes
CHECK(result[0] == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("-128..-33 (int 8)")
{
for (auto i = -128; i <= -33; ++i)
{
CAPTURE(i);
// create JSON value with integer number
json j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd0);
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 2);
// check individual bytes
CHECK(result[0] == 0xd0);
CHECK(static_cast<int8_t>(result[1]) == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("-9263 (int 16)")
{
json j = -9263;
std::vector<uint8_t> expected = {0xd1, 0xdb, 0xd1};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
int16_t restored = (result[1] << 8) + result[2];
CHECK(restored == -9263);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("-32768..-129 (int 16)")
{
for (int16_t i = -32768; i <= -129; ++i)
{
CAPTURE(i);
// create JSON value with integer number
json j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd1);
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 3);
// check individual bytes
CHECK(result[0] == 0xd1);
int16_t restored = (result[1] << 8) + result[2];
CHECK(restored == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
}
SECTION("unsigned")
{
SECTION("0..127 (positive fixnum)")
{
for (size_t i = 0; i <= 127; ++i)
{
CAPTURE(i);
// create JSON value with unsigned integer number
json j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 1);
// check individual bytes
CHECK(result[0] == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("128..255 (uint 8)")
{
for (size_t i = 128; i <= 255; ++i)
{
CAPTURE(i);
// create JSON value with unsigned integer number
json j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcc);
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 2);
// check individual bytes
CHECK(result[0] == 0xcc);
uint8_t restored = static_cast<uint8_t>(result[1]);
CHECK(restored == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("256..65535 (uint 16)")
{
for (size_t i = 256; i <= 65535; ++i)
{
CAPTURE(i);
// create JSON value with unsigned integer number
json j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcd);
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 3);
// check individual bytes
CHECK(result[0] == 0xcd);
uint16_t restored = static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]);
CHECK(restored == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("65536..4294967295 (uint 32)")
{
for (uint32_t i :
{
65536u, 77777u, 1048576u
})
{
CAPTURE(i);
// create JSON value with unsigned integer number
json j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xce);
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == 5);
// check individual bytes
CHECK(result[0] == 0xce);
uint32_t restored = static_cast<uint32_t>((static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]));
CHECK(restored == i);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
}
SECTION("float")
{
SECTION("3.1415925")
{
double v = 3.1415925;
json j = v;
std::vector<uint8_t> expected =
{
0xcb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// restore value (reverse array for endianess)
double restored;
std::reverse(expected.begin(), expected.end());
memcpy(&restored, expected.data(), sizeof(double));
CHECK(restored == v);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
}
SECTION("string")
{
SECTION("N = 0..31")
{
// explicitly enumerate the first byte for all 32 strings
const std::vector<uint8_t> first_bytes =
{
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
0xbb, 0xbc, 0xbd, 0xbe, 0xbf
};
for (size_t N = 0; N < first_bytes.size(); ++N)
{
CAPTURE(N);
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(first_bytes[N]);
for (size_t i = 0; i < N; ++i)
{
expected.push_back('x');
}
// check first byte
CHECK((first_bytes[N] & 0x1f) == N);
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == N + 1);
// check that no null byte is appended
if (N > 0)
{
CHECK(result.back() != '\x00');
}
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("N = 32..255")
{
for (size_t N = 32; N <= 255; ++N)
{
CAPTURE(N);
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd9);
expected.push_back(N);
for (size_t i = 0; i < N; ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == N + 2);
// check that no null byte is appended
CHECK(result.back() != '\x00');
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("N = 256..65535")
{
for (size_t N :
{
256u, 999u, 1025u, 3333u, 2048u, 65535u
})
{
CAPTURE(N);
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
// create expected byte vector (hack: create string first)
std::vector<uint8_t> expected(N, 'x');
// reverse order of commands, because we insert at begin()
expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
expected.insert(expected.begin(), 0xda);
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == N + 3);
// check that no null byte is appended
CHECK(result.back() != '\x00');
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("N = 65536..4294967295")
{
for (size_t N :
{
65536u, 77777u, 1048576u
})
{
CAPTURE(N);
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
// create expected byte vector (hack: create string first)
std::vector<uint8_t> expected(N, 'x');
// reverse order of commands, because we insert at begin()
expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
expected.insert(expected.begin(), 0xdb);
// compare result + size
const auto result = json::to_msgpack(j);
CHECK(result == expected);
CHECK(result.size() == N + 5);
// check that no null byte is appended
CHECK(result.back() != '\x00');
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
}
SECTION("array")
{
SECTION("empty")
{
json j = json::array();
std::vector<uint8_t> expected = {0x90};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("[null]")
{
json j = {nullptr};
std::vector<uint8_t> expected = {0x91, 0xc0};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("[1,2,3,4,5]")
{
json j = json::parse("[1,2,3,4,5]");
std::vector<uint8_t> expected = {0x95, 0x01, 0x02, 0x03, 0x04, 0x05};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("[[[[]]]]")
{
json j = json::parse("[[[[]]]]");
std::vector<uint8_t> expected = {0x91, 0x91, 0x91, 0x90};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
SECTION("object")
{
SECTION("empty")
{
json j = json::object();
std::vector<uint8_t> expected = {0x80};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("{\"\":null}")
{
json j = {{"", nullptr}};
std::vector<uint8_t> expected = {0x81, 0xa0, 0xc0};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
{
json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}");
std::vector<uint8_t> expected =
{
0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80
};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
}
}
}
SECTION("input from msgpack-python")
{
for (std::string filename :
{
"test/data/json_nlohmann_tests/all_unicode.json",
"test/data/json.org/1.json",
"test/data/json.org/2.json",
"test/data/json.org/3.json",
"test/data/json.org/4.json",
"test/data/json.org/5.json",
"test/data/json_roundtrip/roundtrip01.json",
"test/data/json_roundtrip/roundtrip02.json",
"test/data/json_roundtrip/roundtrip03.json",
"test/data/json_roundtrip/roundtrip04.json",
"test/data/json_roundtrip/roundtrip05.json",
"test/data/json_roundtrip/roundtrip06.json",
"test/data/json_roundtrip/roundtrip07.json",
"test/data/json_roundtrip/roundtrip08.json",
"test/data/json_roundtrip/roundtrip09.json",
"test/data/json_roundtrip/roundtrip10.json",
"test/data/json_roundtrip/roundtrip11.json",
"test/data/json_roundtrip/roundtrip12.json",
"test/data/json_roundtrip/roundtrip13.json",
"test/data/json_roundtrip/roundtrip14.json",
"test/data/json_roundtrip/roundtrip15.json",
"test/data/json_roundtrip/roundtrip16.json",
"test/data/json_roundtrip/roundtrip17.json",
"test/data/json_roundtrip/roundtrip18.json",
"test/data/json_roundtrip/roundtrip19.json",
"test/data/json_roundtrip/roundtrip20.json",
"test/data/json_roundtrip/roundtrip21.json",
"test/data/json_roundtrip/roundtrip22.json",
"test/data/json_roundtrip/roundtrip23.json",
"test/data/json_roundtrip/roundtrip24.json",
"test/data/json_roundtrip/roundtrip25.json",
"test/data/json_roundtrip/roundtrip26.json",
"test/data/json_roundtrip/roundtrip27.json",
"test/data/json_roundtrip/roundtrip28.json",
"test/data/json_roundtrip/roundtrip29.json",
"test/data/json_roundtrip/roundtrip30.json",
"test/data/json_roundtrip/roundtrip31.json",
"test/data/json_roundtrip/roundtrip32.json",
"test/data/json_testsuite/sample.json",
"test/data/json_tests/pass1.json",
"test/data/json_tests/pass2.json",
"test/data/json_tests/pass3.json",
"test/data/regression/floats.json",
"test/data/regression/signed_ints.json",
"test/data/regression/unsigned_ints.json",
"test/data/regression/working_file.json",
"test/data/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json",
"test/data/nst_json_testsuite/test_parsing/y_array_empty-string.json",
"test/data/nst_json_testsuite/test_parsing/y_array_empty.json",
"test/data/nst_json_testsuite/test_parsing/y_array_ending_with_newline.json",
"test/data/nst_json_testsuite/test_parsing/y_array_false.json",
"test/data/nst_json_testsuite/test_parsing/y_array_heterogeneous.json",
"test/data/nst_json_testsuite/test_parsing/y_array_null.json",
"test/data/nst_json_testsuite/test_parsing/y_array_with_1_and_newline.json",
"test/data/nst_json_testsuite/test_parsing/y_array_with_leading_space.json",
"test/data/nst_json_testsuite/test_parsing/y_array_with_several_null.json",
"test/data/nst_json_testsuite/test_parsing/y_array_with_trailing_space.json",
"test/data/nst_json_testsuite/test_parsing/y_number.json",
"test/data/nst_json_testsuite/test_parsing/y_number_0e+1.json",
"test/data/nst_json_testsuite/test_parsing/y_number_0e1.json",
"test/data/nst_json_testsuite/test_parsing/y_number_after_space.json",
"test/data/nst_json_testsuite/test_parsing/y_number_double_close_to_zero.json",
"test/data/nst_json_testsuite/test_parsing/y_number_double_huge_neg_exp.json",
"test/data/nst_json_testsuite/test_parsing/y_number_huge_exp.json",
"test/data/nst_json_testsuite/test_parsing/y_number_int_with_exp.json",
"test/data/nst_json_testsuite/test_parsing/y_number_minus_zero.json",
"test/data/nst_json_testsuite/test_parsing/y_number_negative_int.json",
"test/data/nst_json_testsuite/test_parsing/y_number_negative_one.json",
"test/data/nst_json_testsuite/test_parsing/y_number_negative_zero.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e_neg_exp.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e_pos_exp.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_exponent.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_fraction_exponent.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_neg_exp.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_neg_overflow.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_pos_exponent.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_pos_overflow.json",
"test/data/nst_json_testsuite/test_parsing/y_number_real_underflow.json",
"test/data/nst_json_testsuite/test_parsing/y_number_simple_int.json",
"test/data/nst_json_testsuite/test_parsing/y_number_simple_real.json",
//"test/data/nst_json_testsuite/test_parsing/y_number_too_big_neg_int.json",
//"test/data/nst_json_testsuite/test_parsing/y_number_too_big_pos_int.json",
//"test/data/nst_json_testsuite/test_parsing/y_number_very_big_negative_int.json",
"test/data/nst_json_testsuite/test_parsing/y_object.json",
"test/data/nst_json_testsuite/test_parsing/y_object_basic.json",
"test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
"test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key_and_value.json",
"test/data/nst_json_testsuite/test_parsing/y_object_empty.json",
"test/data/nst_json_testsuite/test_parsing/y_object_empty_key.json",
"test/data/nst_json_testsuite/test_parsing/y_object_escaped_null_in_key.json",
"test/data/nst_json_testsuite/test_parsing/y_object_extreme_numbers.json",
"test/data/nst_json_testsuite/test_parsing/y_object_long_strings.json",
"test/data/nst_json_testsuite/test_parsing/y_object_simple.json",
"test/data/nst_json_testsuite/test_parsing/y_object_string_unicode.json",
"test/data/nst_json_testsuite/test_parsing/y_object_with_newlines.json",
"test/data/nst_json_testsuite/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json",
"test/data/nst_json_testsuite/test_parsing/y_string_UTF-16_Surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json",
"test/data/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pair.json",
"test/data/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pairs.json",
"test/data/nst_json_testsuite/test_parsing/y_string_allowed_escapes.json",
"test/data/nst_json_testsuite/test_parsing/y_string_backslash_and_u_escaped_zero.json",
"test/data/nst_json_testsuite/test_parsing/y_string_backslash_doublequotes.json",
"test/data/nst_json_testsuite/test_parsing/y_string_comments.json",
"test/data/nst_json_testsuite/test_parsing/y_string_double_escape_a.json",
"test/data/nst_json_testsuite/test_parsing/y_string_double_escape_n.json",
"test/data/nst_json_testsuite/test_parsing/y_string_escaped_control_character.json",
"test/data/nst_json_testsuite/test_parsing/y_string_escaped_noncharacter.json",
"test/data/nst_json_testsuite/test_parsing/y_string_in_array.json",
"test/data/nst_json_testsuite/test_parsing/y_string_in_array_with_leading_space.json",
"test/data/nst_json_testsuite/test_parsing/y_string_last_surrogates_1_and_2.json",
"test/data/nst_json_testsuite/test_parsing/y_string_newline_uescaped.json",
"test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json",
"test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+1FFFF.json",
"test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json",
"test/data/nst_json_testsuite/test_parsing/y_string_null_escape.json",
"test/data/nst_json_testsuite/test_parsing/y_string_one-byte-utf-8.json",
"test/data/nst_json_testsuite/test_parsing/y_string_pi.json",
"test/data/nst_json_testsuite/test_parsing/y_string_simple_ascii.json",
"test/data/nst_json_testsuite/test_parsing/y_string_space.json",
"test/data/nst_json_testsuite/test_parsing/y_string_three-byte-utf-8.json",
"test/data/nst_json_testsuite/test_parsing/y_string_two-byte-utf-8.json",
"test/data/nst_json_testsuite/test_parsing/y_string_u+2028_line_sep.json",
"test/data/nst_json_testsuite/test_parsing/y_string_u+2029_par_sep.json",
"test/data/nst_json_testsuite/test_parsing/y_string_uEscape.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unescaped_char_delete.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unicode.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unicodeEscapedBackslash.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_2.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_U+2064_invisible_plus.json",
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_escaped_double_quote.json",
// "test/data/nst_json_testsuite/test_parsing/y_string_utf16.json",
"test/data/nst_json_testsuite/test_parsing/y_string_utf8.json",
"test/data/nst_json_testsuite/test_parsing/y_string_with_del_character.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_false.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_int.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_negative_real.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_null.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_string.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_true.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_string_empty.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_trailing_newline.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_true_in_array.json",
"test/data/nst_json_testsuite/test_parsing/y_structure_whitespace_array.json"
})
{
CAPTURE(filename);
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
// parse MessagePack file
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_msgpack)),
std::istreambuf_iterator<char>());
json j2;
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
// compare parsed JSON values
CHECK(j1 == j2);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment