mirror of
https://github.com/nlohmann/json
synced 2024-11-22 12:00:05 +00:00
Enhace to_cbor() to support +/-Infinity, NaN, and single-precision float
This commit is contained in:
parent
a414e35971
commit
44fe284f9d
@ -6,6 +6,7 @@
|
|||||||
#include <cstring> // memcpy
|
#include <cstring> // memcpy
|
||||||
#include <limits> // numeric_limits
|
#include <limits> // numeric_limits
|
||||||
#include <string> // string
|
#include <string> // string
|
||||||
|
#include <cmath> // isnan, isinf
|
||||||
|
|
||||||
#include <nlohmann/detail/input/binary_reader.hpp>
|
#include <nlohmann/detail/input/binary_reader.hpp>
|
||||||
#include <nlohmann/detail/macro_scope.hpp>
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
@ -176,9 +177,34 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
|
|
||||||
case value_t::number_float:
|
case value_t::number_float:
|
||||||
|
{
|
||||||
|
if (std::isnan(j.m_value.number_float))
|
||||||
|
{
|
||||||
|
// NaN is 0xf97e00 in CBOR
|
||||||
|
oa->write_character(to_char_type(0xF9));
|
||||||
|
oa->write_character(to_char_type(0x7E));
|
||||||
|
oa->write_character(to_char_type(0x00));
|
||||||
|
}
|
||||||
|
else if (std::isinf(j.m_value.number_float))
|
||||||
|
{
|
||||||
|
// Infinity is 0xf97c00, -Infinity is 0xf9fc00
|
||||||
|
oa->write_character(to_char_type(0xf9));
|
||||||
|
oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
|
||||||
|
oa->write_character(to_char_type(0x00));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
|
||||||
|
{
|
||||||
|
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));
|
||||||
|
write_number(static_cast<float>(j.m_value.number_float));
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
|
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
|
||||||
write_number(j.m_value.number_float);
|
write_number(j.m_value.number_float);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7059,7 +7059,7 @@ class basic_json
|
|||||||
- break (0xFF)
|
- break (0xFF)
|
||||||
|
|
||||||
@param[in] j JSON value to serialize
|
@param[in] j JSON value to serialize
|
||||||
@return MessagePack serialization as byte vector
|
@return CBOR serialization as byte vector
|
||||||
|
|
||||||
@complexity Linear in the size of the JSON value @a j.
|
@complexity Linear in the size of the JSON value @a j.
|
||||||
|
|
||||||
|
@ -11828,6 +11828,7 @@ class json_ref
|
|||||||
#include <cstring> // memcpy
|
#include <cstring> // memcpy
|
||||||
#include <limits> // numeric_limits
|
#include <limits> // numeric_limits
|
||||||
#include <string> // string
|
#include <string> // string
|
||||||
|
#include <cmath> // isnan, isinf
|
||||||
|
|
||||||
// #include <nlohmann/detail/input/binary_reader.hpp>
|
// #include <nlohmann/detail/input/binary_reader.hpp>
|
||||||
|
|
||||||
@ -12125,9 +12126,34 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
|
|
||||||
case value_t::number_float:
|
case value_t::number_float:
|
||||||
|
{
|
||||||
|
if (std::isnan(j.m_value.number_float))
|
||||||
|
{
|
||||||
|
// NaN is 0xf97e00 in CBOR
|
||||||
|
oa->write_character(to_char_type(0xF9));
|
||||||
|
oa->write_character(to_char_type(0x7E));
|
||||||
|
oa->write_character(to_char_type(0x00));
|
||||||
|
}
|
||||||
|
else if (std::isinf(j.m_value.number_float))
|
||||||
|
{
|
||||||
|
// Infinity is 0xf97c00, -Infinity is 0xf9fc00
|
||||||
|
oa->write_character(to_char_type(0xf9));
|
||||||
|
oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
|
||||||
|
oa->write_character(to_char_type(0x00));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
|
||||||
|
{
|
||||||
|
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));
|
||||||
|
write_number(static_cast<float>(j.m_value.number_float));
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
|
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
|
||||||
write_number(j.m_value.number_float);
|
write_number(j.m_value.number_float);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22507,7 +22533,7 @@ class basic_json
|
|||||||
- break (0xFF)
|
- break (0xFF)
|
||||||
|
|
||||||
@param[in] j JSON value to serialize
|
@param[in] j JSON value to serialize
|
||||||
@return MessagePack serialization as byte vector
|
@return CBOR serialization as byte vector
|
||||||
|
|
||||||
@complexity Linear in the size of the JSON value @a j.
|
@complexity Linear in the size of the JSON value @a j.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user