Merge pull request #1315 from nlohmann/feature/convert_char

Properly convert constants to CharType
This commit is contained in:
Niels Lohmann 2018-10-27 16:56:00 +02:00 committed by GitHub
commit 1308ea055d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 238 additions and 160 deletions

View file

@ -69,15 +69,15 @@ class binary_writer
{ {
case value_t::null: case value_t::null:
{ {
oa->write_character(static_cast<CharType>(0xF6)); oa->write_character(to_char_type(0xF6));
break; break;
} }
case value_t::boolean: case value_t::boolean:
{ {
oa->write_character(j.m_value.boolean oa->write_character(j.m_value.boolean
? static_cast<CharType>(0xF5) ? to_char_type(0xF5)
: static_cast<CharType>(0xF4)); : to_char_type(0xF4));
break; break;
} }
@ -94,22 +94,22 @@ class binary_writer
} }
else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x18)); oa->write_character(to_char_type(0x18));
write_number(static_cast<uint8_t>(j.m_value.number_integer)); write_number(static_cast<uint8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x19)); oa->write_character(to_char_type(0x19));
write_number(static_cast<uint16_t>(j.m_value.number_integer)); write_number(static_cast<uint16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x1A)); oa->write_character(to_char_type(0x1A));
write_number(static_cast<uint32_t>(j.m_value.number_integer)); write_number(static_cast<uint32_t>(j.m_value.number_integer));
} }
else else
{ {
oa->write_character(static_cast<CharType>(0x1B)); oa->write_character(to_char_type(0x1B));
write_number(static_cast<uint64_t>(j.m_value.number_integer)); write_number(static_cast<uint64_t>(j.m_value.number_integer));
} }
} }
@ -124,22 +124,22 @@ class binary_writer
} }
else if (positive_number <= (std::numeric_limits<uint8_t>::max)()) else if (positive_number <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x38)); oa->write_character(to_char_type(0x38));
write_number(static_cast<uint8_t>(positive_number)); write_number(static_cast<uint8_t>(positive_number));
} }
else if (positive_number <= (std::numeric_limits<uint16_t>::max)()) else if (positive_number <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x39)); oa->write_character(to_char_type(0x39));
write_number(static_cast<uint16_t>(positive_number)); write_number(static_cast<uint16_t>(positive_number));
} }
else if (positive_number <= (std::numeric_limits<uint32_t>::max)()) else if (positive_number <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x3A)); oa->write_character(to_char_type(0x3A));
write_number(static_cast<uint32_t>(positive_number)); write_number(static_cast<uint32_t>(positive_number));
} }
else else
{ {
oa->write_character(static_cast<CharType>(0x3B)); oa->write_character(to_char_type(0x3B));
write_number(static_cast<uint64_t>(positive_number)); write_number(static_cast<uint64_t>(positive_number));
} }
} }
@ -154,22 +154,22 @@ class binary_writer
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x18)); oa->write_character(to_char_type(0x18));
write_number(static_cast<uint8_t>(j.m_value.number_unsigned)); write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x19)); oa->write_character(to_char_type(0x19));
write_number(static_cast<uint16_t>(j.m_value.number_unsigned)); write_number(static_cast<uint16_t>(j.m_value.number_unsigned));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x1A)); oa->write_character(to_char_type(0x1A));
write_number(static_cast<uint32_t>(j.m_value.number_unsigned)); write_number(static_cast<uint32_t>(j.m_value.number_unsigned));
} }
else else
{ {
oa->write_character(static_cast<CharType>(0x1B)); oa->write_character(to_char_type(0x1B));
write_number(static_cast<uint64_t>(j.m_value.number_unsigned)); write_number(static_cast<uint64_t>(j.m_value.number_unsigned));
} }
break; break;
@ -192,23 +192,23 @@ class binary_writer
} }
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x78)); oa->write_character(to_char_type(0x78));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x79)); oa->write_character(to_char_type(0x79));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x7A)); oa->write_character(to_char_type(0x7A));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
// LCOV_EXCL_START // LCOV_EXCL_START
else if (N <= (std::numeric_limits<uint64_t>::max)()) else if (N <= (std::numeric_limits<uint64_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x7B)); oa->write_character(to_char_type(0x7B));
write_number(static_cast<uint64_t>(N)); write_number(static_cast<uint64_t>(N));
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@ -230,23 +230,23 @@ class binary_writer
} }
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x98)); oa->write_character(to_char_type(0x98));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x99)); oa->write_character(to_char_type(0x99));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x9A)); oa->write_character(to_char_type(0x9A));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
// LCOV_EXCL_START // LCOV_EXCL_START
else if (N <= (std::numeric_limits<uint64_t>::max)()) else if (N <= (std::numeric_limits<uint64_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x9B)); oa->write_character(to_char_type(0x9B));
write_number(static_cast<uint64_t>(N)); write_number(static_cast<uint64_t>(N));
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@ -269,23 +269,23 @@ class binary_writer
} }
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xB8)); oa->write_character(to_char_type(0xB8));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xB9)); oa->write_character(to_char_type(0xB9));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xBA)); oa->write_character(to_char_type(0xBA));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
// LCOV_EXCL_START // LCOV_EXCL_START
else if (N <= (std::numeric_limits<uint64_t>::max)()) else if (N <= (std::numeric_limits<uint64_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xBB)); oa->write_character(to_char_type(0xBB));
write_number(static_cast<uint64_t>(N)); write_number(static_cast<uint64_t>(N));
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@ -313,15 +313,15 @@ class binary_writer
{ {
case value_t::null: // nil case value_t::null: // nil
{ {
oa->write_character(static_cast<CharType>(0xC0)); oa->write_character(to_char_type(0xC0));
break; break;
} }
case value_t::boolean: // true and false case value_t::boolean: // true and false
{ {
oa->write_character(j.m_value.boolean oa->write_character(j.m_value.boolean
? static_cast<CharType>(0xC3) ? to_char_type(0xC3)
: static_cast<CharType>(0xC2)); : to_char_type(0xC2));
break; break;
} }
@ -340,25 +340,25 @@ class binary_writer
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
// uint 8 // uint 8
oa->write_character(static_cast<CharType>(0xCC)); oa->write_character(to_char_type(0xCC));
write_number(static_cast<uint8_t>(j.m_value.number_integer)); write_number(static_cast<uint8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
{ {
// uint 16 // uint 16
oa->write_character(static_cast<CharType>(0xCD)); oa->write_character(to_char_type(0xCD));
write_number(static_cast<uint16_t>(j.m_value.number_integer)); write_number(static_cast<uint16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
{ {
// uint 32 // uint 32
oa->write_character(static_cast<CharType>(0xCE)); oa->write_character(to_char_type(0xCE));
write_number(static_cast<uint32_t>(j.m_value.number_integer)); write_number(static_cast<uint32_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
{ {
// uint 64 // uint 64
oa->write_character(static_cast<CharType>(0xCF)); oa->write_character(to_char_type(0xCF));
write_number(static_cast<uint64_t>(j.m_value.number_integer)); write_number(static_cast<uint64_t>(j.m_value.number_integer));
} }
} }
@ -373,28 +373,28 @@ class binary_writer
j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
{ {
// int 8 // int 8
oa->write_character(static_cast<CharType>(0xD0)); oa->write_character(to_char_type(0xD0));
write_number(static_cast<int8_t>(j.m_value.number_integer)); write_number(static_cast<int8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and
j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
{ {
// int 16 // int 16
oa->write_character(static_cast<CharType>(0xD1)); oa->write_character(to_char_type(0xD1));
write_number(static_cast<int16_t>(j.m_value.number_integer)); write_number(static_cast<int16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and
j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
{ {
// int 32 // int 32
oa->write_character(static_cast<CharType>(0xD2)); oa->write_character(to_char_type(0xD2));
write_number(static_cast<int32_t>(j.m_value.number_integer)); write_number(static_cast<int32_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and
j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)())
{ {
// int 64 // int 64
oa->write_character(static_cast<CharType>(0xD3)); oa->write_character(to_char_type(0xD3));
write_number(static_cast<int64_t>(j.m_value.number_integer)); write_number(static_cast<int64_t>(j.m_value.number_integer));
} }
} }
@ -411,25 +411,25 @@ class binary_writer
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
// uint 8 // uint 8
oa->write_character(static_cast<CharType>(0xCC)); oa->write_character(to_char_type(0xCC));
write_number(static_cast<uint8_t>(j.m_value.number_integer)); write_number(static_cast<uint8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
{ {
// uint 16 // uint 16
oa->write_character(static_cast<CharType>(0xCD)); oa->write_character(to_char_type(0xCD));
write_number(static_cast<uint16_t>(j.m_value.number_integer)); write_number(static_cast<uint16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
{ {
// uint 32 // uint 32
oa->write_character(static_cast<CharType>(0xCE)); oa->write_character(to_char_type(0xCE));
write_number(static_cast<uint32_t>(j.m_value.number_integer)); write_number(static_cast<uint32_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
{ {
// uint 64 // uint 64
oa->write_character(static_cast<CharType>(0xCF)); oa->write_character(to_char_type(0xCF));
write_number(static_cast<uint64_t>(j.m_value.number_integer)); write_number(static_cast<uint64_t>(j.m_value.number_integer));
} }
break; break;
@ -454,19 +454,19 @@ class binary_writer
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
// str 8 // str 8
oa->write_character(static_cast<CharType>(0xD9)); oa->write_character(to_char_type(0xD9));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
// str 16 // str 16
oa->write_character(static_cast<CharType>(0xDA)); oa->write_character(to_char_type(0xDA));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
// str 32 // str 32
oa->write_character(static_cast<CharType>(0xDB)); oa->write_character(to_char_type(0xDB));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
@ -489,13 +489,13 @@ class binary_writer
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
// array 16 // array 16
oa->write_character(static_cast<CharType>(0xDC)); oa->write_character(to_char_type(0xDC));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
// array 32 // array 32
oa->write_character(static_cast<CharType>(0xDD)); oa->write_character(to_char_type(0xDD));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
@ -519,13 +519,13 @@ class binary_writer
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
// map 16 // map 16
oa->write_character(static_cast<CharType>(0xDE)); oa->write_character(to_char_type(0xDE));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
// map 32 // map 32
oa->write_character(static_cast<CharType>(0xDF)); oa->write_character(to_char_type(0xDF));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
@ -558,7 +558,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('Z')); oa->write_character(to_char_type('Z'));
} }
break; break;
} }
@ -568,8 +568,8 @@ class binary_writer
if (add_prefix) if (add_prefix)
{ {
oa->write_character(j.m_value.boolean oa->write_character(j.m_value.boolean
? static_cast<CharType>('T') ? to_char_type('T')
: static_cast<CharType>('F')); : to_char_type('F'));
} }
break; break;
} }
@ -596,7 +596,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('S')); oa->write_character(to_char_type('S'));
} }
write_number_with_ubjson_prefix(j.m_value.string->size(), true); write_number_with_ubjson_prefix(j.m_value.string->size(), true);
oa->write_characters( oa->write_characters(
@ -609,7 +609,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('[')); oa->write_character(to_char_type('['));
} }
bool prefix_required = true; bool prefix_required = true;
@ -626,14 +626,14 @@ class binary_writer
if (same_prefix) if (same_prefix)
{ {
prefix_required = false; prefix_required = false;
oa->write_character(static_cast<CharType>('$')); oa->write_character(to_char_type('$'));
oa->write_character(first_prefix); oa->write_character(first_prefix);
} }
} }
if (use_count) if (use_count)
{ {
oa->write_character(static_cast<CharType>('#')); oa->write_character(to_char_type('#'));
write_number_with_ubjson_prefix(j.m_value.array->size(), true); write_number_with_ubjson_prefix(j.m_value.array->size(), true);
} }
@ -644,7 +644,7 @@ class binary_writer
if (not use_count) if (not use_count)
{ {
oa->write_character(static_cast<CharType>(']')); oa->write_character(to_char_type(']'));
} }
break; break;
@ -654,7 +654,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('{')); oa->write_character(to_char_type('{'));
} }
bool prefix_required = true; bool prefix_required = true;
@ -671,14 +671,14 @@ class binary_writer
if (same_prefix) if (same_prefix)
{ {
prefix_required = false; prefix_required = false;
oa->write_character(static_cast<CharType>('$')); oa->write_character(to_char_type('$'));
oa->write_character(first_prefix); oa->write_character(first_prefix);
} }
} }
if (use_count) if (use_count)
{ {
oa->write_character(static_cast<CharType>('#')); oa->write_character(to_char_type('#'));
write_number_with_ubjson_prefix(j.m_value.object->size(), true); write_number_with_ubjson_prefix(j.m_value.object->size(), true);
} }
@ -693,7 +693,7 @@ class binary_writer
if (not use_count) if (not use_count)
{ {
oa->write_character(static_cast<CharType>('}')); oa->write_character(to_char_type('}'));
} }
break; break;
@ -731,7 +731,7 @@ class binary_writer
void write_bson_entry_header(const typename BasicJsonType::string_t& name, void write_bson_entry_header(const typename BasicJsonType::string_t& name,
std::uint8_t element_type) std::uint8_t element_type)
{ {
oa->write_character(static_cast<CharType>(element_type)); // boolean oa->write_character(to_char_type(element_type)); // boolean
oa->write_characters( oa->write_characters(
reinterpret_cast<const CharType*>(name.c_str()), reinterpret_cast<const CharType*>(name.c_str()),
name.size() + 1u); name.size() + 1u);
@ -744,7 +744,7 @@ class binary_writer
const bool value) const bool value)
{ {
write_bson_entry_header(name, 0x08); write_bson_entry_header(name, 0x08);
oa->write_character(value ? static_cast<CharType>(0x01) : static_cast<CharType>(0x00)); oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
} }
/*! /*!
@ -900,7 +900,7 @@ class binary_writer
write_bson_element(std::to_string(array_index++), el); write_bson_element(std::to_string(array_index++), el);
} }
oa->write_character(static_cast<CharType>(0x00)); oa->write_character(to_char_type(0x00));
} }
/*! /*!
@ -1026,7 +1026,7 @@ class binary_writer
write_bson_element(el.first, el.second); write_bson_element(el.first, el.second);
} }
oa->write_character(static_cast<CharType>(0x00)); oa->write_character(to_char_type(0x00));
} }
////////// //////////
@ -1035,12 +1035,12 @@ class binary_writer
static constexpr CharType get_cbor_float_prefix(float /*unused*/) static constexpr CharType get_cbor_float_prefix(float /*unused*/)
{ {
return static_cast<CharType>(0xFA); // Single-Precision Float return to_char_type(0xFA); // Single-Precision Float
} }
static constexpr CharType get_cbor_float_prefix(double /*unused*/) static constexpr CharType get_cbor_float_prefix(double /*unused*/)
{ {
return static_cast<CharType>(0xFB); // Double-Precision Float return to_char_type(0xFB); // Double-Precision Float
} }
///////////// /////////////
@ -1049,12 +1049,12 @@ class binary_writer
static constexpr CharType get_msgpack_float_prefix(float /*unused*/) static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
{ {
return static_cast<CharType>(0xCA); // float 32 return to_char_type(0xCA); // float 32
} }
static constexpr CharType get_msgpack_float_prefix(double /*unused*/) static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
{ {
return static_cast<CharType>(0xCB); // float 64 return to_char_type(0xCB); // float 64
} }
//////////// ////////////
@ -1084,7 +1084,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('i')); // int8 oa->write_character(to_char_type('i')); // int8
} }
write_number(static_cast<uint8_t>(n)); write_number(static_cast<uint8_t>(n));
} }
@ -1092,7 +1092,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('U')); // uint8 oa->write_character(to_char_type('U')); // uint8
} }
write_number(static_cast<uint8_t>(n)); write_number(static_cast<uint8_t>(n));
} }
@ -1100,7 +1100,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('I')); // int16 oa->write_character(to_char_type('I')); // int16
} }
write_number(static_cast<int16_t>(n)); write_number(static_cast<int16_t>(n));
} }
@ -1108,7 +1108,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('l')); // int32 oa->write_character(to_char_type('l')); // int32
} }
write_number(static_cast<int32_t>(n)); write_number(static_cast<int32_t>(n));
} }
@ -1116,7 +1116,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('L')); // int64 oa->write_character(to_char_type('L')); // int64
} }
write_number(static_cast<int64_t>(n)); write_number(static_cast<int64_t>(n));
} }
@ -1137,7 +1137,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('i')); // int8 oa->write_character(to_char_type('i')); // int8
} }
write_number(static_cast<int8_t>(n)); write_number(static_cast<int8_t>(n));
} }
@ -1145,7 +1145,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('U')); // uint8 oa->write_character(to_char_type('U')); // uint8
} }
write_number(static_cast<uint8_t>(n)); write_number(static_cast<uint8_t>(n));
} }
@ -1153,7 +1153,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('I')); // int16 oa->write_character(to_char_type('I')); // int16
} }
write_number(static_cast<int16_t>(n)); write_number(static_cast<int16_t>(n));
} }
@ -1161,7 +1161,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('l')); // int32 oa->write_character(to_char_type('l')); // int32
} }
write_number(static_cast<int32_t>(n)); write_number(static_cast<int32_t>(n));
} }
@ -1169,7 +1169,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('L')); // int64 oa->write_character(to_char_type('L')); // int64
} }
write_number(static_cast<int64_t>(n)); write_number(static_cast<int64_t>(n));
} }
@ -1277,7 +1277,6 @@ class binary_writer
/* /*
@brief write a number to output input @brief write a number to output input
@param[in] n number of type @a NumberType @param[in] n number of type @a NumberType
@tparam NumberType the type of the number @tparam NumberType the type of the number
@tparam OutputIsLittleEndian Set to true if output data is @tparam OutputIsLittleEndian Set to true if output data is
@ -1304,6 +1303,46 @@ class binary_writer
oa->write_characters(vec.data(), sizeof(NumberType)); oa->write_characters(vec.data(), sizeof(NumberType));
} }
// The following to_char_type functions are implement the conversion
// between uint8_t and CharType. In case CharType is not unsigned,
// such a conversion is required to allow values greater than 128.
// See <https://github.com/nlohmann/json/issues/1286> for a discussion.
template < typename C = CharType,
enable_if_t < std::is_signed<C>::value and std::is_signed<char>::value > * = nullptr >
static constexpr CharType to_char_type(std::uint8_t x) noexcept
{
return *reinterpret_cast<char*>(&x);
}
template < typename C = CharType,
enable_if_t < std::is_signed<C>::value and std::is_unsigned<char>::value > * = nullptr >
static CharType to_char_type(std::uint8_t x) noexcept
{
static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
static_assert(std::is_pod<CharType>::value, "CharType must be POD");
CharType result;
std::memcpy(&result, &x, sizeof(x));
return result;
}
template<typename C = CharType,
enable_if_t<std::is_unsigned<C>::value>* = nullptr>
static constexpr CharType to_char_type(std::uint8_t x) noexcept
{
return x;
}
template < typename InputCharType, typename C = CharType,
enable_if_t <
std::is_signed<C>::value and
std::is_signed<char>::value and
std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
> * = nullptr >
static constexpr CharType to_char_type(InputCharType x) noexcept
{
return x;
}
private: private:
/// whether we can assume little endianess /// whether we can assume little endianess
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess(); const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();

View file

@ -8380,15 +8380,15 @@ class binary_writer
{ {
case value_t::null: case value_t::null:
{ {
oa->write_character(static_cast<CharType>(0xF6)); oa->write_character(to_char_type(0xF6));
break; break;
} }
case value_t::boolean: case value_t::boolean:
{ {
oa->write_character(j.m_value.boolean oa->write_character(j.m_value.boolean
? static_cast<CharType>(0xF5) ? to_char_type(0xF5)
: static_cast<CharType>(0xF4)); : to_char_type(0xF4));
break; break;
} }
@ -8405,22 +8405,22 @@ class binary_writer
} }
else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x18)); oa->write_character(to_char_type(0x18));
write_number(static_cast<uint8_t>(j.m_value.number_integer)); write_number(static_cast<uint8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x19)); oa->write_character(to_char_type(0x19));
write_number(static_cast<uint16_t>(j.m_value.number_integer)); write_number(static_cast<uint16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x1A)); oa->write_character(to_char_type(0x1A));
write_number(static_cast<uint32_t>(j.m_value.number_integer)); write_number(static_cast<uint32_t>(j.m_value.number_integer));
} }
else else
{ {
oa->write_character(static_cast<CharType>(0x1B)); oa->write_character(to_char_type(0x1B));
write_number(static_cast<uint64_t>(j.m_value.number_integer)); write_number(static_cast<uint64_t>(j.m_value.number_integer));
} }
} }
@ -8435,22 +8435,22 @@ class binary_writer
} }
else if (positive_number <= (std::numeric_limits<uint8_t>::max)()) else if (positive_number <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x38)); oa->write_character(to_char_type(0x38));
write_number(static_cast<uint8_t>(positive_number)); write_number(static_cast<uint8_t>(positive_number));
} }
else if (positive_number <= (std::numeric_limits<uint16_t>::max)()) else if (positive_number <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x39)); oa->write_character(to_char_type(0x39));
write_number(static_cast<uint16_t>(positive_number)); write_number(static_cast<uint16_t>(positive_number));
} }
else if (positive_number <= (std::numeric_limits<uint32_t>::max)()) else if (positive_number <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x3A)); oa->write_character(to_char_type(0x3A));
write_number(static_cast<uint32_t>(positive_number)); write_number(static_cast<uint32_t>(positive_number));
} }
else else
{ {
oa->write_character(static_cast<CharType>(0x3B)); oa->write_character(to_char_type(0x3B));
write_number(static_cast<uint64_t>(positive_number)); write_number(static_cast<uint64_t>(positive_number));
} }
} }
@ -8465,22 +8465,22 @@ class binary_writer
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x18)); oa->write_character(to_char_type(0x18));
write_number(static_cast<uint8_t>(j.m_value.number_unsigned)); write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x19)); oa->write_character(to_char_type(0x19));
write_number(static_cast<uint16_t>(j.m_value.number_unsigned)); write_number(static_cast<uint16_t>(j.m_value.number_unsigned));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x1A)); oa->write_character(to_char_type(0x1A));
write_number(static_cast<uint32_t>(j.m_value.number_unsigned)); write_number(static_cast<uint32_t>(j.m_value.number_unsigned));
} }
else else
{ {
oa->write_character(static_cast<CharType>(0x1B)); oa->write_character(to_char_type(0x1B));
write_number(static_cast<uint64_t>(j.m_value.number_unsigned)); write_number(static_cast<uint64_t>(j.m_value.number_unsigned));
} }
break; break;
@ -8503,23 +8503,23 @@ class binary_writer
} }
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x78)); oa->write_character(to_char_type(0x78));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x79)); oa->write_character(to_char_type(0x79));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x7A)); oa->write_character(to_char_type(0x7A));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
// LCOV_EXCL_START // LCOV_EXCL_START
else if (N <= (std::numeric_limits<uint64_t>::max)()) else if (N <= (std::numeric_limits<uint64_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x7B)); oa->write_character(to_char_type(0x7B));
write_number(static_cast<uint64_t>(N)); write_number(static_cast<uint64_t>(N));
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@ -8541,23 +8541,23 @@ class binary_writer
} }
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x98)); oa->write_character(to_char_type(0x98));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x99)); oa->write_character(to_char_type(0x99));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x9A)); oa->write_character(to_char_type(0x9A));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
// LCOV_EXCL_START // LCOV_EXCL_START
else if (N <= (std::numeric_limits<uint64_t>::max)()) else if (N <= (std::numeric_limits<uint64_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0x9B)); oa->write_character(to_char_type(0x9B));
write_number(static_cast<uint64_t>(N)); write_number(static_cast<uint64_t>(N));
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@ -8580,23 +8580,23 @@ class binary_writer
} }
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xB8)); oa->write_character(to_char_type(0xB8));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xB9)); oa->write_character(to_char_type(0xB9));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xBA)); oa->write_character(to_char_type(0xBA));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
// LCOV_EXCL_START // LCOV_EXCL_START
else if (N <= (std::numeric_limits<uint64_t>::max)()) else if (N <= (std::numeric_limits<uint64_t>::max)())
{ {
oa->write_character(static_cast<CharType>(0xBB)); oa->write_character(to_char_type(0xBB));
write_number(static_cast<uint64_t>(N)); write_number(static_cast<uint64_t>(N));
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@ -8624,15 +8624,15 @@ class binary_writer
{ {
case value_t::null: // nil case value_t::null: // nil
{ {
oa->write_character(static_cast<CharType>(0xC0)); oa->write_character(to_char_type(0xC0));
break; break;
} }
case value_t::boolean: // true and false case value_t::boolean: // true and false
{ {
oa->write_character(j.m_value.boolean oa->write_character(j.m_value.boolean
? static_cast<CharType>(0xC3) ? to_char_type(0xC3)
: static_cast<CharType>(0xC2)); : to_char_type(0xC2));
break; break;
} }
@ -8651,25 +8651,25 @@ class binary_writer
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
// uint 8 // uint 8
oa->write_character(static_cast<CharType>(0xCC)); oa->write_character(to_char_type(0xCC));
write_number(static_cast<uint8_t>(j.m_value.number_integer)); write_number(static_cast<uint8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
{ {
// uint 16 // uint 16
oa->write_character(static_cast<CharType>(0xCD)); oa->write_character(to_char_type(0xCD));
write_number(static_cast<uint16_t>(j.m_value.number_integer)); write_number(static_cast<uint16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
{ {
// uint 32 // uint 32
oa->write_character(static_cast<CharType>(0xCE)); oa->write_character(to_char_type(0xCE));
write_number(static_cast<uint32_t>(j.m_value.number_integer)); write_number(static_cast<uint32_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
{ {
// uint 64 // uint 64
oa->write_character(static_cast<CharType>(0xCF)); oa->write_character(to_char_type(0xCF));
write_number(static_cast<uint64_t>(j.m_value.number_integer)); write_number(static_cast<uint64_t>(j.m_value.number_integer));
} }
} }
@ -8684,28 +8684,28 @@ class binary_writer
j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
{ {
// int 8 // int 8
oa->write_character(static_cast<CharType>(0xD0)); oa->write_character(to_char_type(0xD0));
write_number(static_cast<int8_t>(j.m_value.number_integer)); write_number(static_cast<int8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and
j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
{ {
// int 16 // int 16
oa->write_character(static_cast<CharType>(0xD1)); oa->write_character(to_char_type(0xD1));
write_number(static_cast<int16_t>(j.m_value.number_integer)); write_number(static_cast<int16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and
j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
{ {
// int 32 // int 32
oa->write_character(static_cast<CharType>(0xD2)); oa->write_character(to_char_type(0xD2));
write_number(static_cast<int32_t>(j.m_value.number_integer)); write_number(static_cast<int32_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and
j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)()) j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)())
{ {
// int 64 // int 64
oa->write_character(static_cast<CharType>(0xD3)); oa->write_character(to_char_type(0xD3));
write_number(static_cast<int64_t>(j.m_value.number_integer)); write_number(static_cast<int64_t>(j.m_value.number_integer));
} }
} }
@ -8722,25 +8722,25 @@ class binary_writer
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
// uint 8 // uint 8
oa->write_character(static_cast<CharType>(0xCC)); oa->write_character(to_char_type(0xCC));
write_number(static_cast<uint8_t>(j.m_value.number_integer)); write_number(static_cast<uint8_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
{ {
// uint 16 // uint 16
oa->write_character(static_cast<CharType>(0xCD)); oa->write_character(to_char_type(0xCD));
write_number(static_cast<uint16_t>(j.m_value.number_integer)); write_number(static_cast<uint16_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
{ {
// uint 32 // uint 32
oa->write_character(static_cast<CharType>(0xCE)); oa->write_character(to_char_type(0xCE));
write_number(static_cast<uint32_t>(j.m_value.number_integer)); write_number(static_cast<uint32_t>(j.m_value.number_integer));
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
{ {
// uint 64 // uint 64
oa->write_character(static_cast<CharType>(0xCF)); oa->write_character(to_char_type(0xCF));
write_number(static_cast<uint64_t>(j.m_value.number_integer)); write_number(static_cast<uint64_t>(j.m_value.number_integer));
} }
break; break;
@ -8765,19 +8765,19 @@ class binary_writer
else if (N <= (std::numeric_limits<uint8_t>::max)()) else if (N <= (std::numeric_limits<uint8_t>::max)())
{ {
// str 8 // str 8
oa->write_character(static_cast<CharType>(0xD9)); oa->write_character(to_char_type(0xD9));
write_number(static_cast<uint8_t>(N)); write_number(static_cast<uint8_t>(N));
} }
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
// str 16 // str 16
oa->write_character(static_cast<CharType>(0xDA)); oa->write_character(to_char_type(0xDA));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
// str 32 // str 32
oa->write_character(static_cast<CharType>(0xDB)); oa->write_character(to_char_type(0xDB));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
@ -8800,13 +8800,13 @@ class binary_writer
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
// array 16 // array 16
oa->write_character(static_cast<CharType>(0xDC)); oa->write_character(to_char_type(0xDC));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
// array 32 // array 32
oa->write_character(static_cast<CharType>(0xDD)); oa->write_character(to_char_type(0xDD));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
@ -8830,13 +8830,13 @@ class binary_writer
else if (N <= (std::numeric_limits<uint16_t>::max)()) else if (N <= (std::numeric_limits<uint16_t>::max)())
{ {
// map 16 // map 16
oa->write_character(static_cast<CharType>(0xDE)); oa->write_character(to_char_type(0xDE));
write_number(static_cast<uint16_t>(N)); write_number(static_cast<uint16_t>(N));
} }
else if (N <= (std::numeric_limits<uint32_t>::max)()) else if (N <= (std::numeric_limits<uint32_t>::max)())
{ {
// map 32 // map 32
oa->write_character(static_cast<CharType>(0xDF)); oa->write_character(to_char_type(0xDF));
write_number(static_cast<uint32_t>(N)); write_number(static_cast<uint32_t>(N));
} }
@ -8869,7 +8869,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('Z')); oa->write_character(to_char_type('Z'));
} }
break; break;
} }
@ -8879,8 +8879,8 @@ class binary_writer
if (add_prefix) if (add_prefix)
{ {
oa->write_character(j.m_value.boolean oa->write_character(j.m_value.boolean
? static_cast<CharType>('T') ? to_char_type('T')
: static_cast<CharType>('F')); : to_char_type('F'));
} }
break; break;
} }
@ -8907,7 +8907,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('S')); oa->write_character(to_char_type('S'));
} }
write_number_with_ubjson_prefix(j.m_value.string->size(), true); write_number_with_ubjson_prefix(j.m_value.string->size(), true);
oa->write_characters( oa->write_characters(
@ -8920,7 +8920,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('[')); oa->write_character(to_char_type('['));
} }
bool prefix_required = true; bool prefix_required = true;
@ -8937,14 +8937,14 @@ class binary_writer
if (same_prefix) if (same_prefix)
{ {
prefix_required = false; prefix_required = false;
oa->write_character(static_cast<CharType>('$')); oa->write_character(to_char_type('$'));
oa->write_character(first_prefix); oa->write_character(first_prefix);
} }
} }
if (use_count) if (use_count)
{ {
oa->write_character(static_cast<CharType>('#')); oa->write_character(to_char_type('#'));
write_number_with_ubjson_prefix(j.m_value.array->size(), true); write_number_with_ubjson_prefix(j.m_value.array->size(), true);
} }
@ -8955,7 +8955,7 @@ class binary_writer
if (not use_count) if (not use_count)
{ {
oa->write_character(static_cast<CharType>(']')); oa->write_character(to_char_type(']'));
} }
break; break;
@ -8965,7 +8965,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('{')); oa->write_character(to_char_type('{'));
} }
bool prefix_required = true; bool prefix_required = true;
@ -8982,14 +8982,14 @@ class binary_writer
if (same_prefix) if (same_prefix)
{ {
prefix_required = false; prefix_required = false;
oa->write_character(static_cast<CharType>('$')); oa->write_character(to_char_type('$'));
oa->write_character(first_prefix); oa->write_character(first_prefix);
} }
} }
if (use_count) if (use_count)
{ {
oa->write_character(static_cast<CharType>('#')); oa->write_character(to_char_type('#'));
write_number_with_ubjson_prefix(j.m_value.object->size(), true); write_number_with_ubjson_prefix(j.m_value.object->size(), true);
} }
@ -9004,7 +9004,7 @@ class binary_writer
if (not use_count) if (not use_count)
{ {
oa->write_character(static_cast<CharType>('}')); oa->write_character(to_char_type('}'));
} }
break; break;
@ -9042,7 +9042,7 @@ class binary_writer
void write_bson_entry_header(const typename BasicJsonType::string_t& name, void write_bson_entry_header(const typename BasicJsonType::string_t& name,
std::uint8_t element_type) std::uint8_t element_type)
{ {
oa->write_character(static_cast<CharType>(element_type)); // boolean oa->write_character(to_char_type(element_type)); // boolean
oa->write_characters( oa->write_characters(
reinterpret_cast<const CharType*>(name.c_str()), reinterpret_cast<const CharType*>(name.c_str()),
name.size() + 1u); name.size() + 1u);
@ -9055,7 +9055,7 @@ class binary_writer
const bool value) const bool value)
{ {
write_bson_entry_header(name, 0x08); write_bson_entry_header(name, 0x08);
oa->write_character(value ? static_cast<CharType>(0x01) : static_cast<CharType>(0x00)); oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
} }
/*! /*!
@ -9211,7 +9211,7 @@ class binary_writer
write_bson_element(std::to_string(array_index++), el); write_bson_element(std::to_string(array_index++), el);
} }
oa->write_character(static_cast<CharType>(0x00)); oa->write_character(to_char_type(0x00));
} }
/*! /*!
@ -9337,7 +9337,7 @@ class binary_writer
write_bson_element(el.first, el.second); write_bson_element(el.first, el.second);
} }
oa->write_character(static_cast<CharType>(0x00)); oa->write_character(to_char_type(0x00));
} }
////////// //////////
@ -9346,12 +9346,12 @@ class binary_writer
static constexpr CharType get_cbor_float_prefix(float /*unused*/) static constexpr CharType get_cbor_float_prefix(float /*unused*/)
{ {
return static_cast<CharType>(0xFA); // Single-Precision Float return to_char_type(0xFA); // Single-Precision Float
} }
static constexpr CharType get_cbor_float_prefix(double /*unused*/) static constexpr CharType get_cbor_float_prefix(double /*unused*/)
{ {
return static_cast<CharType>(0xFB); // Double-Precision Float return to_char_type(0xFB); // Double-Precision Float
} }
///////////// /////////////
@ -9360,12 +9360,12 @@ class binary_writer
static constexpr CharType get_msgpack_float_prefix(float /*unused*/) static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
{ {
return static_cast<CharType>(0xCA); // float 32 return to_char_type(0xCA); // float 32
} }
static constexpr CharType get_msgpack_float_prefix(double /*unused*/) static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
{ {
return static_cast<CharType>(0xCB); // float 64 return to_char_type(0xCB); // float 64
} }
//////////// ////////////
@ -9395,7 +9395,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('i')); // int8 oa->write_character(to_char_type('i')); // int8
} }
write_number(static_cast<uint8_t>(n)); write_number(static_cast<uint8_t>(n));
} }
@ -9403,7 +9403,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('U')); // uint8 oa->write_character(to_char_type('U')); // uint8
} }
write_number(static_cast<uint8_t>(n)); write_number(static_cast<uint8_t>(n));
} }
@ -9411,7 +9411,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('I')); // int16 oa->write_character(to_char_type('I')); // int16
} }
write_number(static_cast<int16_t>(n)); write_number(static_cast<int16_t>(n));
} }
@ -9419,7 +9419,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('l')); // int32 oa->write_character(to_char_type('l')); // int32
} }
write_number(static_cast<int32_t>(n)); write_number(static_cast<int32_t>(n));
} }
@ -9427,7 +9427,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('L')); // int64 oa->write_character(to_char_type('L')); // int64
} }
write_number(static_cast<int64_t>(n)); write_number(static_cast<int64_t>(n));
} }
@ -9448,7 +9448,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('i')); // int8 oa->write_character(to_char_type('i')); // int8
} }
write_number(static_cast<int8_t>(n)); write_number(static_cast<int8_t>(n));
} }
@ -9456,7 +9456,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('U')); // uint8 oa->write_character(to_char_type('U')); // uint8
} }
write_number(static_cast<uint8_t>(n)); write_number(static_cast<uint8_t>(n));
} }
@ -9464,7 +9464,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('I')); // int16 oa->write_character(to_char_type('I')); // int16
} }
write_number(static_cast<int16_t>(n)); write_number(static_cast<int16_t>(n));
} }
@ -9472,7 +9472,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('l')); // int32 oa->write_character(to_char_type('l')); // int32
} }
write_number(static_cast<int32_t>(n)); write_number(static_cast<int32_t>(n));
} }
@ -9480,7 +9480,7 @@ class binary_writer
{ {
if (add_prefix) if (add_prefix)
{ {
oa->write_character(static_cast<CharType>('L')); // int64 oa->write_character(to_char_type('L')); // int64
} }
write_number(static_cast<int64_t>(n)); write_number(static_cast<int64_t>(n));
} }
@ -9588,7 +9588,6 @@ class binary_writer
/* /*
@brief write a number to output input @brief write a number to output input
@param[in] n number of type @a NumberType @param[in] n number of type @a NumberType
@tparam NumberType the type of the number @tparam NumberType the type of the number
@tparam OutputIsLittleEndian Set to true if output data is @tparam OutputIsLittleEndian Set to true if output data is
@ -9615,6 +9614,46 @@ class binary_writer
oa->write_characters(vec.data(), sizeof(NumberType)); oa->write_characters(vec.data(), sizeof(NumberType));
} }
// The following to_char_type functions are implement the conversion
// between uint8_t and CharType. In case CharType is not unsigned,
// such a conversion is required to allow values greater than 128.
// See <https://github.com/nlohmann/json/issues/1286> for a discussion.
template < typename C = CharType,
enable_if_t < std::is_signed<C>::value and std::is_signed<char>::value > * = nullptr >
static constexpr CharType to_char_type(std::uint8_t x) noexcept
{
return *reinterpret_cast<char*>(&x);
}
template < typename C = CharType,
enable_if_t < std::is_signed<C>::value and std::is_unsigned<char>::value > * = nullptr >
static CharType to_char_type(std::uint8_t x) noexcept
{
static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
static_assert(std::is_pod<CharType>::value, "CharType must be POD");
CharType result;
std::memcpy(&result, &x, sizeof(x));
return result;
}
template<typename C = CharType,
enable_if_t<std::is_unsigned<C>::value>* = nullptr>
static constexpr CharType to_char_type(std::uint8_t x) noexcept
{
return x;
}
template < typename InputCharType, typename C = CharType,
enable_if_t <
std::is_signed<C>::value and
std::is_signed<char>::value and
std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
> * = nullptr >
static constexpr CharType to_char_type(InputCharType x) noexcept
{
return x;
}
private: private:
/// whether we can assume little endianess /// whether we can assume little endianess
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess(); const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();