1
0
mirror of https://github.com/nlohmann/json synced 2024-12-24 01:30:08 +00:00

Merge branch 'develop' into feature/wstring

This commit is contained in:
Niels Lohmann 2018-04-08 11:36:58 +02:00
commit cf91b4f2bb
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
8 changed files with 115 additions and 33 deletions

View File

@ -277,6 +277,7 @@ script:
if [[ (-x $(which brew)) ]]; then
brew update
brew install cmake ninja
brew upgrade cmake
cmake --version
fi

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0.0)
cmake_minimum_required(VERSION 3.8)
##
## PROJECT
@ -44,6 +44,8 @@ endif()
##
add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE)
target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_std_11)
target_include_directories(
${NLOHMANN_JSON_TARGET_NAME}
INTERFACE
@ -51,8 +53,8 @@ target_include_directories(
$<INSTALL_INTERFACE:include>
)
## add debug view defintion file for msvc (natvis) [cmake <= 3.2.2 does not support export of source files]
if (MSVC AND CMAKE_VERSION VERSION_GREATER "3.2.2")
## add debug view defintion file for msvc (natvis)
if (MSVC)
set(NLOHMANN_ADD_NATVIS TRUE)
set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis")
target_sources(
@ -62,7 +64,7 @@ if (MSVC AND CMAKE_VERSION VERSION_GREATER "3.2.2")
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${NLOHMANN_NATVIS_FILE}>
)
endif()
##
## TESTS
## create and configure the unit test target

View File

@ -76,6 +76,8 @@ If you are using the [Meson Build System](http://mesonbuild.com), then you can w
If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add `jsonformoderncpp/x.y.z@vthiery/stable` to your `conanfile.py`'s requires, where `x.y.z` is the release version you want to use. Please file issues [here](https://github.com/vthiery/conan-jsonformoderncpp/issues) if you experience problems with the packages.
If you are using [Spack](https://www.spack.io/) to manage your dependencies, you can use the `nlohmann_json` package. Please see the [spack project](https://github.com/spack/spack) for any issues regarding the packaging.
If you are using [hunter](https://github.com/ruslo/hunter/) on your project for external dependencies, then you can use the [nlohmann_json package](https://docs.hunter.sh/en/latest/packages/pkg/nlohmann_json.html). Please see the hunter project for any issues regarding the packaging.
If you are using [Buckaroo](https://buckaroo.pm), you can install this library's module with `buckaroo install nlohmann/json`. Please file issues [here](https://github.com/LoopPerfect/buckaroo-recipes/issues/new?title=nlohmann/nlohmann/json).

View File

@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.8)
project(JSON_Benchmarks LANGUAGES CXX)
# set compiler flags
if((CMAKE_CXX_COMPILER_ID MATCHES GNU) OR (CMAKE_CXX_COMPILER_ID MATCHES Clang))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -flto -DNDEBUG -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -DNDEBUG -O3")
endif()
# configure Google Benchmarks
@ -23,4 +23,5 @@ file(COPY ${CMAKE_SOURCE_DIR}/../test/data/regression/floats.json
# benchmark binary
add_executable(json_benchmarks src/benchmarks.cpp)
target_compile_features(json_benchmarks PRIVATE cxx_std_11)
target_link_libraries(json_benchmarks benchmark ${CMAKE_THREAD_LIBS_INIT})

View File

@ -149,9 +149,9 @@ class binary_writer
break;
}
case value_t::number_float: // Double-Precision Float
case value_t::number_float:
{
oa->write_character(static_cast<CharType>(0xFB));
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
write_number(j.m_value.number_float);
break;
}
@ -409,9 +409,9 @@ class binary_writer
break;
}
case value_t::number_float: // float 64
case value_t::number_float:
{
oa->write_character(static_cast<CharType>(0xCB));
oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
write_number(j.m_value.number_float);
break;
}
@ -588,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)
{
@ -599,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);
}
}
@ -633,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)
{
@ -644,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);
}
}
@ -712,7 +712,7 @@ class binary_writer
{
if (add_prefix)
{
oa->write_character(static_cast<CharType>('D')); // float64
oa->write_character(get_ubjson_float_prefix(n));
}
write_number(n);
}
@ -833,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())
{
@ -892,7 +892,7 @@ class binary_writer
}
case value_t::number_float:
return 'D';
return get_ubjson_float_prefix(j.m_value.number_float);
case value_t::string:
return 'S';
@ -908,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();

View File

@ -6536,9 +6536,9 @@ class binary_writer
break;
}
case value_t::number_float: // Double-Precision Float
case value_t::number_float:
{
oa->write_character(static_cast<CharType>(0xFB));
oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
write_number(j.m_value.number_float);
break;
}
@ -6796,9 +6796,9 @@ class binary_writer
break;
}
case value_t::number_float: // float 64
case value_t::number_float:
{
oa->write_character(static_cast<CharType>(0xCB));
oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
write_number(j.m_value.number_float);
break;
}
@ -6975,7 +6975,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)
{
@ -6986,7 +6986,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);
}
}
@ -7020,7 +7020,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)
{
@ -7031,7 +7031,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);
}
}
@ -7099,7 +7099,7 @@ class binary_writer
{
if (add_prefix)
{
oa->write_character(static_cast<CharType>('D')); // float64
oa->write_character(get_ubjson_float_prefix(n));
}
write_number(n);
}
@ -7220,7 +7220,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())
{
@ -7279,7 +7279,7 @@ class binary_writer
}
case value_t::number_float:
return 'D';
return get_ubjson_float_prefix(j.m_value.number_float);
case value_t::string:
return 'S';
@ -7295,6 +7295,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();

View File

@ -6,7 +6,7 @@ option(JSON_Coverage "Build test suite with coverage information" OFF)
if(JSON_Sanitizer)
message(STATUS "Building test suite with Clang sanitizer")
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "-std=c++11 -g -O2 -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "-g -O2 -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer")
endif()
endif()
@ -61,11 +61,10 @@ add_library(catch_main OBJECT
"src/unit.cpp"
)
set_target_properties(catch_main PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON
COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
)
target_compile_features(catch_main PUBLIC cxx_std_11)
target_include_directories(catch_main PRIVATE "thirdparty/catch")
# https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake
@ -95,13 +94,12 @@ foreach(file ${files})
add_executable(${testcase} $<TARGET_OBJECTS:catch_main> ${file})
set_target_properties(${testcase} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON
COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
)
target_compile_definitions(${testcase} PRIVATE CATCH_CONFIG_FAST_COMPILE)
target_compile_features(${testcase} PRIVATE cxx_std_11)
target_include_directories(${testcase} PRIVATE "thirdparty/catch")
target_include_directories(${testcase} PRIVATE "thirdparty/fifo_map")
target_include_directories(${testcase} PRIVATE ${NLOHMANN_JSON_INCLUDE_BUILD_DIR})

View File

@ -114,6 +114,13 @@ struct nocopy
};
}
/////////////////////////////////////////////////////////////////////
// for #1021
/////////////////////////////////////////////////////////////////////
using float_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, float>;
TEST_CASE("regression tests")
{
SECTION("issue #60 - Double quotation mark is not parsed correctly")
@ -1597,4 +1604,15 @@ TEST_CASE("regression tests")
auto j = json::parse(geojsonExample, cb, true);
CHECK(j == json());
}
SECTION("issue #1021 - to/from_msgpack only works with standard typization")
{
float_json j = 1000.0;
CHECK(float_json::from_cbor(float_json::to_cbor(j)) == j);
CHECK(float_json::from_msgpack(float_json::to_msgpack(j)) == j);
CHECK(float_json::from_ubjson(float_json::to_ubjson(j)) == j);
float_json j2 = {1000.0, 2000.0, 3000.0};
CHECK(float_json::from_ubjson(float_json::to_ubjson(j2, true, true)) == j2);
}
}