Merge branch 'develop' of https://github.com/nlohmann/json into comments

This commit is contained in:
Niels Lohmann 2020-06-21 12:40:21 +02:00
commit 139a0258cc
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
9 changed files with 110 additions and 57 deletions

View file

@ -1557,4 +1557,6 @@ $ cmake --build .
$ ctest --output-on-failure $ ctest --output-on-failure
``` ```
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml). Note that during the `ctest` stage, several JSON test files are downloaded from an [external repository](https://github.com/nlohmann/json_test_data). If policies forbid downloading artifacts during testing, you can download the files yourself and pass the directory with the test files via `-DJSON_TestDataDirectory=path` to CMake. Then, no Internet connectivity is required. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information.
In case you have downloaded the library rather than checked out the code via Git, test `cmake_fetch_content_configure`. Please execute `ctest -LE git_required` to skip these tests. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information.

View file

@ -1,17 +1,22 @@
find_package(Git)
set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data) set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data)
set(JSON_TEST_DATA_VERSION 2.0.0) set(JSON_TEST_DATA_VERSION 3.0.0)
# target to download test data # if variable is set, use test data from given directory rather than downloading them
add_custom_target(download_test_data if(JSON_TestDataDirectory)
COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1 message(STATUS "Using test data in ${JSON_TestDataDirectory}.")
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})" add_custom_target(download_test_data)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TestDataDirectory}\"\n")
) else()
find_package(Git)
# create a header with the path to the downloaded test data # target to download test data
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n") add_custom_target(download_test_data
COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
# create a header with the path to the downloaded test data
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n")
endif()
# determine the operating system (for debug and support purposes) # determine the operating system (for debug and support purposes)
find_program(UNAME_COMMAND uname) find_program(UNAME_COMMAND uname)

View file

@ -31,7 +31,8 @@ number_unsigned | 128..255 | uint 8 | 0xCC
number_unsigned | 256..65535 | uint 16 | 0xCD number_unsigned | 256..65535 | uint 16 | 0xCD
number_unsigned | 65536..4294967295 | uint 32 | 0xCE number_unsigned | 65536..4294967295 | uint 32 | 0xCE
number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
number_float | *any value* | float 64 | 0xCB number_float | *any value representable by a float* | float 32 | 0xCA
number_float | *any value NOT representable by a float* | float 64 | 0xCB
string | *length*: 0..31 | fixstr | 0xA0..0xBF string | *length*: 0..31 | fixstr | 0xA0..0xBF
string | *length*: 32..255 | str 8 | 0xD9 string | *length*: 32..255 | str 8 | 0xD9
string | *length*: 256..65535 | str 16 | 0xDA string | *length*: 256..65535 | str 16 | 0xDA
@ -61,10 +62,6 @@ binary | *size*: 65536..4294967295 | bin 32 | 0xC6
- arrays with more than 4294967295 elements - arrays with more than 4294967295 elements
- objects with more than 4294967295 elements - objects with more than 4294967295 elements
!!! info "Unused MessagePack types"
The following MessagePack types are not used in the conversion: float 32 (0xCA)
!!! info "NaN/infinity handling" !!! info "NaN/infinity handling"
If NaN or Infinity are stored inside a JSON number, they are serialized properly. function which serializes NaN or Infinity to `null`. If NaN or Infinity are stored inside a JSON number, they are serialized properly. function which serializes NaN or Infinity to `null`.

View file

@ -28,6 +28,7 @@ class binary_writer
{ {
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
using binary_t = typename BasicJsonType::binary_t; using binary_t = typename BasicJsonType::binary_t;
using number_float_t = typename BasicJsonType::number_float_t;
public: public:
/*! /*!
@ -194,18 +195,7 @@ class binary_writer
} }
else else
{ {
if (static_cast<double>(j.m_value.number_float) >= static_cast<double>(std::numeric_limits<float>::lowest()) and write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
static_cast<double>(j.m_value.number_float) <= static_cast<double>((std::numeric_limits<float>::max)()) and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == static_cast<double>(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));
write_number(j.m_value.number_float);
}
} }
break; break;
} }
@ -504,8 +494,7 @@ class binary_writer
case value_t::number_float: case value_t::number_float:
{ {
oa->write_character(get_msgpack_float_prefix(j.m_value.number_float)); write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
write_number(j.m_value.number_float);
break; break;
} }
@ -1518,6 +1507,26 @@ class binary_writer
oa->write_characters(vec.data(), sizeof(NumberType)); oa->write_characters(vec.data(), sizeof(NumberType));
} }
void write_compact_float(const number_float_t n, detail::input_format_t format)
{
if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) and
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) and
static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
{
oa->write_character(format == detail::input_format_t::cbor
? get_cbor_float_prefix(static_cast<float>(n))
: get_msgpack_float_prefix(static_cast<float>(n)));
write_number(static_cast<float>(n));
}
else
{
oa->write_character(format == detail::input_format_t::cbor
? get_cbor_float_prefix(n)
: get_msgpack_float_prefix(n));
write_number(n);
}
}
public: public:
// The following to_char_type functions are implement the conversion // The following to_char_type functions are implement the conversion
// between uint8_t and CharType. In case CharType is not unsigned, // between uint8_t and CharType. In case CharType is not unsigned,

View file

@ -7061,7 +7061,8 @@ class basic_json
number_unsigned | 256..65535 | uint 16 | 0xCD number_unsigned | 256..65535 | uint 16 | 0xCD
number_unsigned | 65536..4294967295 | uint 32 | 0xCE number_unsigned | 65536..4294967295 | uint 32 | 0xCE
number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
number_float | *any value* | float 64 | 0xCB number_float | *any value representable by a float* | float 32 | 0xCA
number_float | *any value NOT representable by a float* | float 64 | 0xCB
string | *length*: 0..31 | fixstr | 0xA0..0xBF string | *length*: 0..31 | fixstr | 0xA0..0xBF
string | *length*: 32..255 | str 8 | 0xD9 string | *length*: 32..255 | str 8 | 0xD9
string | *length*: 256..65535 | str 16 | 0xDA string | *length*: 256..65535 | str 16 | 0xDA
@ -7085,9 +7086,6 @@ class basic_json
- arrays with more than 4294967295 elements - arrays with more than 4294967295 elements
- objects with more than 4294967295 elements - objects with more than 4294967295 elements
@note The following MessagePack types are not used in the conversion:
- float 32 (0xCA)
@note Any MessagePack output created @ref to_msgpack can be successfully @note Any MessagePack output created @ref to_msgpack can be successfully
parsed by @ref from_msgpack. parsed by @ref from_msgpack.

View file

@ -12329,6 +12329,7 @@ class binary_writer
{ {
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
using binary_t = typename BasicJsonType::binary_t; using binary_t = typename BasicJsonType::binary_t;
using number_float_t = typename BasicJsonType::number_float_t;
public: public:
/*! /*!
@ -12495,18 +12496,7 @@ class binary_writer
} }
else else
{ {
if (static_cast<double>(j.m_value.number_float) >= static_cast<double>(std::numeric_limits<float>::lowest()) and write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
static_cast<double>(j.m_value.number_float) <= static_cast<double>((std::numeric_limits<float>::max)()) and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == static_cast<double>(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));
write_number(j.m_value.number_float);
}
} }
break; break;
} }
@ -12805,8 +12795,7 @@ class binary_writer
case value_t::number_float: case value_t::number_float:
{ {
oa->write_character(get_msgpack_float_prefix(j.m_value.number_float)); write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
write_number(j.m_value.number_float);
break; break;
} }
@ -13819,6 +13808,26 @@ class binary_writer
oa->write_characters(vec.data(), sizeof(NumberType)); oa->write_characters(vec.data(), sizeof(NumberType));
} }
void write_compact_float(const number_float_t n, detail::input_format_t format)
{
if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) and
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) and
static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
{
oa->write_character(format == detail::input_format_t::cbor
? get_cbor_float_prefix(static_cast<float>(n))
: get_msgpack_float_prefix(static_cast<float>(n)));
write_number(static_cast<float>(n));
}
else
{
oa->write_character(format == detail::input_format_t::cbor
? get_cbor_float_prefix(n)
: get_msgpack_float_prefix(n));
write_number(n);
}
}
public: public:
// The following to_char_type functions are implement the conversion // The following to_char_type functions are implement the conversion
// between uint8_t and CharType. In case CharType is not unsigned, // between uint8_t and CharType. In case CharType is not unsigned,
@ -22933,7 +22942,8 @@ class basic_json
number_unsigned | 256..65535 | uint 16 | 0xCD number_unsigned | 256..65535 | uint 16 | 0xCD
number_unsigned | 65536..4294967295 | uint 32 | 0xCE number_unsigned | 65536..4294967295 | uint 32 | 0xCE
number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
number_float | *any value* | float 64 | 0xCB number_float | *any value representable by a float* | float 32 | 0xCA
number_float | *any value NOT representable by a float* | float 64 | 0xCB
string | *length*: 0..31 | fixstr | 0xA0..0xBF string | *length*: 0..31 | fixstr | 0xA0..0xBF
string | *length*: 32..255 | str 8 | 0xD9 string | *length*: 32..255 | str 8 | 0xD9
string | *length*: 256..65535 | str 16 | 0xDA string | *length*: 256..65535 | str 16 | 0xDA
@ -22957,9 +22967,6 @@ class basic_json
- arrays with more than 4294967295 elements - arrays with more than 4294967295 elements
- objects with more than 4294967295 elements - objects with more than 4294967295 elements
@note The following MessagePack types are not used in the conversion:
- float 32 (0xCA)
@note Any MessagePack output created @ref to_msgpack can be successfully @note Any MessagePack output created @ref to_msgpack can be successfully
parsed by @ref from_msgpack. parsed by @ref from_msgpack.

View file

@ -11,8 +11,10 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.11.0")
) )
set_tests_properties(cmake_fetch_content_configure PROPERTIES set_tests_properties(cmake_fetch_content_configure PROPERTIES
FIXTURES_SETUP cmake_fetch_content FIXTURES_SETUP cmake_fetch_content
LABELS git_required
) )
set_tests_properties(cmake_fetch_content_build PROPERTIES set_tests_properties(cmake_fetch_content_build PROPERTIES
FIXTURES_REQUIRED cmake_fetch_content FIXTURES_REQUIRED cmake_fetch_content
LABELS git_required
) )
endif() endif()

View file

@ -4,9 +4,8 @@ project(DummyImport CXX)
include(FetchContent) include(FetchContent)
FetchContent_Declare(json get_filename_component(GIT_REPOSITORY_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../.. ABSOLUTE)
GIT_REPOSITORY ${CMAKE_CURRENT_SOURCE_DIR}/../../.. FetchContent_Declare(json GIT_REPOSITORY ${GIT_REPOSITORY_DIRECTORY} GIT_TAG HEAD)
GIT_TAG HEAD)
FetchContent_GetProperties(json) FetchContent_GetProperties(json)
if(NOT json_POPULATED) if(NOT json_POPULATED)

View file

@ -783,6 +783,40 @@ TEST_CASE("MessagePack")
CHECK(json::from_msgpack(result) == v); CHECK(json::from_msgpack(result) == v);
CHECK(json::from_msgpack(result, true, false) == j); CHECK(json::from_msgpack(result, true, false) == j);
} }
SECTION("1.0")
{
double v = 1.0;
json j = v;
std::vector<uint8_t> expected =
{
0xca, 0x3f, 0x80, 0x00, 0x00
};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
CHECK(json::from_msgpack(result) == v);
CHECK(json::from_msgpack(result, true, false) == j);
}
SECTION("128.128")
{
double v = 128.1280059814453125;
json j = v;
std::vector<uint8_t> expected =
{
0xca, 0x43, 0x00, 0x20, 0xc5
};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_msgpack(result) == j);
CHECK(json::from_msgpack(result) == v);
CHECK(json::from_msgpack(result, true, false) == j);
}
} }
} }