mirror of
https://github.com/nlohmann/json
synced 2025-01-11 17:50:06 +00:00
🔨 improved code #1021
This commit is contained in:
parent
a9baab76c2
commit
896a9db461
@ -301,7 +301,6 @@ Exceptions have ids 5xx.
|
||||
name / id | example message | description
|
||||
------------------------------ | --------------- | -------------------------
|
||||
json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed.
|
||||
json.exception.other_error.502 | type for number_float_t is not supported | The template type for a number is not supported for the binary serialization in CBOR, MessagePack or UBJSON.
|
||||
|
||||
@sa @ref exception for the base class of the library exceptions
|
||||
@sa @ref parse_error for exceptions indicating a parse error
|
||||
|
@ -151,17 +151,7 @@ class binary_writer
|
||||
|
||||
case value_t::number_float:
|
||||
{
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // Single-Precision Float
|
||||
oa->write_character(static_cast<CharType>(0xFA));
|
||||
break;
|
||||
case 8: // Double-Precision Float
|
||||
oa->write_character(static_cast<CharType>(0xFB));
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
|
||||
write_number(j.m_value.number_float);
|
||||
break;
|
||||
}
|
||||
@ -421,17 +411,7 @@ class binary_writer
|
||||
|
||||
case value_t::number_float:
|
||||
{
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // float 32
|
||||
oa->write_character(static_cast<CharType>(0xCA));
|
||||
break;
|
||||
case 8: // float 64
|
||||
oa->write_character(static_cast<CharType>(0xCB));
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
|
||||
write_number(j.m_value.number_float);
|
||||
break;
|
||||
}
|
||||
@ -608,7 +588,7 @@ class binary_writer
|
||||
if (use_type and not j.m_value.array->empty())
|
||||
{
|
||||
assert(use_count);
|
||||
const char first_prefix = ubjson_prefix(j.front());
|
||||
const CharType first_prefix = ubjson_prefix(j.front());
|
||||
const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
|
||||
[this, first_prefix](const BasicJsonType & v)
|
||||
{
|
||||
@ -619,7 +599,7 @@ class binary_writer
|
||||
{
|
||||
prefix_required = false;
|
||||
oa->write_character(static_cast<CharType>('$'));
|
||||
oa->write_character(static_cast<CharType>(first_prefix));
|
||||
oa->write_character(first_prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@ -653,7 +633,7 @@ class binary_writer
|
||||
if (use_type and not j.m_value.object->empty())
|
||||
{
|
||||
assert(use_count);
|
||||
const char first_prefix = ubjson_prefix(j.front());
|
||||
const CharType first_prefix = ubjson_prefix(j.front());
|
||||
const bool same_prefix = std::all_of(j.begin(), j.end(),
|
||||
[this, first_prefix](const BasicJsonType & v)
|
||||
{
|
||||
@ -664,7 +644,7 @@ class binary_writer
|
||||
{
|
||||
prefix_required = false;
|
||||
oa->write_character(static_cast<CharType>('$'));
|
||||
oa->write_character(static_cast<CharType>(first_prefix));
|
||||
oa->write_character(first_prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@ -732,17 +712,7 @@ class binary_writer
|
||||
{
|
||||
if (add_prefix)
|
||||
{
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // float 32
|
||||
oa->write_character(static_cast<CharType>('d'));
|
||||
break;
|
||||
case 8: // float 64
|
||||
oa->write_character(static_cast<CharType>('D'));
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
oa->write_character(get_ubjson_float_prefix(n));
|
||||
}
|
||||
write_number(n);
|
||||
}
|
||||
@ -863,7 +833,7 @@ class binary_writer
|
||||
write_number_with_ubjson_prefix. Therefore, we return 'L' for any
|
||||
value that does not fit the previous limits.
|
||||
*/
|
||||
char ubjson_prefix(const BasicJsonType& j) const noexcept
|
||||
CharType ubjson_prefix(const BasicJsonType& j) const noexcept
|
||||
{
|
||||
switch (j.type())
|
||||
{
|
||||
@ -922,15 +892,7 @@ class binary_writer
|
||||
}
|
||||
|
||||
case value_t::number_float:
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // float 32
|
||||
return 'd';
|
||||
case 8: // float 64
|
||||
return 'D';
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
return get_ubjson_float_prefix(j.m_value.number_float);
|
||||
|
||||
case value_t::string:
|
||||
return 'S';
|
||||
@ -946,6 +908,36 @@ class binary_writer
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr CharType get_cbor_float_prefix(float)
|
||||
{
|
||||
return static_cast<CharType>(0xFA); // Single-Precision Float
|
||||
}
|
||||
|
||||
static constexpr CharType get_cbor_float_prefix(double)
|
||||
{
|
||||
return static_cast<CharType>(0xFB); // Double-Precision Float
|
||||
}
|
||||
|
||||
static constexpr CharType get_msgpack_float_prefix(float)
|
||||
{
|
||||
return static_cast<CharType>(0xCA); // float 32
|
||||
}
|
||||
|
||||
static constexpr CharType get_msgpack_float_prefix(double)
|
||||
{
|
||||
return static_cast<CharType>(0xCB); // float 64
|
||||
}
|
||||
|
||||
static constexpr CharType get_ubjson_float_prefix(float)
|
||||
{
|
||||
return 'd'; // float 32
|
||||
}
|
||||
|
||||
static constexpr CharType get_ubjson_float_prefix(double)
|
||||
{
|
||||
return 'D'; // float 64
|
||||
}
|
||||
|
||||
private:
|
||||
/// whether we can assume little endianess
|
||||
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
|
||||
|
@ -794,7 +794,6 @@ Exceptions have ids 5xx.
|
||||
name / id | example message | description
|
||||
------------------------------ | --------------- | -------------------------
|
||||
json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed.
|
||||
json.exception.other_error.502 | type for number_float_t is not supported | The template type for a number is not supported for the binary serialization in CBOR, MessagePack or UBJSON.
|
||||
|
||||
@sa @ref exception for the base class of the library exceptions
|
||||
@sa @ref parse_error for exceptions indicating a parse error
|
||||
@ -6360,17 +6359,7 @@ class binary_writer
|
||||
|
||||
case value_t::number_float:
|
||||
{
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // Single-Precision Float
|
||||
oa->write_character(static_cast<CharType>(0xFA));
|
||||
break;
|
||||
case 8: // Double-Precision Float
|
||||
oa->write_character(static_cast<CharType>(0xFB));
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
|
||||
write_number(j.m_value.number_float);
|
||||
break;
|
||||
}
|
||||
@ -6630,17 +6619,7 @@ class binary_writer
|
||||
|
||||
case value_t::number_float:
|
||||
{
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // float 32
|
||||
oa->write_character(static_cast<CharType>(0xCA));
|
||||
break;
|
||||
case 8: // float 64
|
||||
oa->write_character(static_cast<CharType>(0xCB));
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
|
||||
write_number(j.m_value.number_float);
|
||||
break;
|
||||
}
|
||||
@ -6817,7 +6796,7 @@ class binary_writer
|
||||
if (use_type and not j.m_value.array->empty())
|
||||
{
|
||||
assert(use_count);
|
||||
const char first_prefix = ubjson_prefix(j.front());
|
||||
const CharType first_prefix = ubjson_prefix(j.front());
|
||||
const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
|
||||
[this, first_prefix](const BasicJsonType & v)
|
||||
{
|
||||
@ -6828,7 +6807,7 @@ class binary_writer
|
||||
{
|
||||
prefix_required = false;
|
||||
oa->write_character(static_cast<CharType>('$'));
|
||||
oa->write_character(static_cast<CharType>(first_prefix));
|
||||
oa->write_character(first_prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6862,7 +6841,7 @@ class binary_writer
|
||||
if (use_type and not j.m_value.object->empty())
|
||||
{
|
||||
assert(use_count);
|
||||
const char first_prefix = ubjson_prefix(j.front());
|
||||
const CharType first_prefix = ubjson_prefix(j.front());
|
||||
const bool same_prefix = std::all_of(j.begin(), j.end(),
|
||||
[this, first_prefix](const BasicJsonType & v)
|
||||
{
|
||||
@ -6873,7 +6852,7 @@ class binary_writer
|
||||
{
|
||||
prefix_required = false;
|
||||
oa->write_character(static_cast<CharType>('$'));
|
||||
oa->write_character(static_cast<CharType>(first_prefix));
|
||||
oa->write_character(first_prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6941,17 +6920,7 @@ class binary_writer
|
||||
{
|
||||
if (add_prefix)
|
||||
{
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // float 32
|
||||
oa->write_character(static_cast<CharType>('d'));
|
||||
break;
|
||||
case 8: // float 64
|
||||
oa->write_character(static_cast<CharType>('D'));
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
oa->write_character(get_ubjson_float_prefix(n));
|
||||
}
|
||||
write_number(n);
|
||||
}
|
||||
@ -7072,7 +7041,7 @@ class binary_writer
|
||||
write_number_with_ubjson_prefix. Therefore, we return 'L' for any
|
||||
value that does not fit the previous limits.
|
||||
*/
|
||||
char ubjson_prefix(const BasicJsonType& j) const noexcept
|
||||
CharType ubjson_prefix(const BasicJsonType& j) const noexcept
|
||||
{
|
||||
switch (j.type())
|
||||
{
|
||||
@ -7131,15 +7100,7 @@ class binary_writer
|
||||
}
|
||||
|
||||
case value_t::number_float:
|
||||
switch (sizeof(typename BasicJsonType::number_float_t))
|
||||
{
|
||||
case 4: // float 32
|
||||
return 'd';
|
||||
case 8: // float 64
|
||||
return 'D';
|
||||
default:
|
||||
JSON_THROW(other_error::create(502, "type for number_float_t is not supported"));
|
||||
}
|
||||
return get_ubjson_float_prefix(j.m_value.number_float);
|
||||
|
||||
case value_t::string:
|
||||
return 'S';
|
||||
@ -7155,6 +7116,36 @@ class binary_writer
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr CharType get_cbor_float_prefix(float)
|
||||
{
|
||||
return static_cast<CharType>(0xFA); // Single-Precision Float
|
||||
}
|
||||
|
||||
static constexpr CharType get_cbor_float_prefix(double)
|
||||
{
|
||||
return static_cast<CharType>(0xFB); // Double-Precision Float
|
||||
}
|
||||
|
||||
static constexpr CharType get_msgpack_float_prefix(float)
|
||||
{
|
||||
return static_cast<CharType>(0xCA); // float 32
|
||||
}
|
||||
|
||||
static constexpr CharType get_msgpack_float_prefix(double)
|
||||
{
|
||||
return static_cast<CharType>(0xCB); // float 64
|
||||
}
|
||||
|
||||
static constexpr CharType get_ubjson_float_prefix(float)
|
||||
{
|
||||
return 'd'; // float 32
|
||||
}
|
||||
|
||||
static constexpr CharType get_ubjson_float_prefix(double)
|
||||
{
|
||||
return 'D'; // float 64
|
||||
}
|
||||
|
||||
private:
|
||||
/// whether we can assume little endianess
|
||||
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
|
||||
|
@ -118,7 +118,7 @@ struct nocopy
|
||||
// for #1021
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
using float_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int32_t, std::uint32_t, float>;
|
||||
using float_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, float>;
|
||||
|
||||
|
||||
TEST_CASE("regression tests")
|
||||
|
Loading…
Reference in New Issue
Block a user