Unverified Commit 3d67ec40 by Niels Lohmann

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

parents 85c76808 67fb517c
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0.0)
# define the project ##
project(nlohmann_json VERSION 2.1.1 LANGUAGES CXX) ## PROJECT
## name and version
##
project(nlohmann_json VERSION 2.1.1)
##
## OPTIONS
##
option(JSON_BuildTests "Build the unit tests" ON) option(JSON_BuildTests "Build the unit tests" ON)
# define project variables ##
set(JSON_TARGET_NAME ${PROJECT_NAME}) ## CONFIGURATION
set(JSON_PACKAGE_NAME ${JSON_TARGET_NAME}) ##
set(JSON_TARGETS_FILENAME "${JSON_PACKAGE_NAME}Targets.cmake") set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME})
set(JSON_CONFIG_FILENAME "${JSON_PACKAGE_NAME}Config.cmake") set(NLOHMANN_JSON_SOURCE_DIR "src/")
set(JSON_CONFIGVERSION_FILENAME "${JSON_PACKAGE_NAME}ConfigVersion.cmake") set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}")
set(JSON_CONFIG_DESTINATION "cmake") set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "include")
set(JSON_INCLUDE_DESTINATION "include/nlohmann") set(NLOHMANN_JSON_HEADER_INSTALL_DIR "${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}/nlohmann")
set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in")
set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/cmake_config")
set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") ##
## TARGET
## create target and add include path
##
add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE)
# create and configure the library target target_include_directories(
add_library(${JSON_TARGET_NAME} INTERFACE) ${NLOHMANN_JSON_TARGET_NAME}
target_include_directories(${JSON_TARGET_NAME} INTERFACE INTERFACE $<INSTALL_INTERFACE:include/>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> )
$<INSTALL_INTERFACE:${JSON_INCLUDE_DESTINATION}>)
##
# create and configure the unit test target ## TESTS
## create and configure the unit test target
##
if(JSON_BuildTests) if(JSON_BuildTests)
enable_testing() enable_testing()
include_directories(${NLOHMANN_JSON_SOURCE_DIR})
add_subdirectory(test) add_subdirectory(test)
endif() endif()
# generate a config and config version file for the package ##
## INSTALL
## install header files, generate and install cmake config files for find_package()
##
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
configure_package_config_file("cmake/config.cmake.in" write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}" ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} COMPATIBILITY SameMajorVersion
INSTALL_DESTINATION ${JSON_CONFIG_DESTINATION} )
PATH_VARS JSON_INCLUDE_DESTINATION) configure_package_config_file(
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" ${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE}
VERSION ${PROJECT_VERSION} ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE}
COMPATIBILITY SameMajorVersion) INSTALL_DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)
# export the library target and store build directory in package registry install(
export(TARGETS ${JSON_TARGET_NAME} DIRECTORY ${NLOHMANN_JSON_SOURCE_DIR}
FILE "${CMAKE_CURRENT_BINARY_DIR}/${JSON_TARGETS_FILENAME}") DESTINATION ${NLOHMANN_JSON_HEADER_INSTALL_DIR}
export(PACKAGE ${JSON_PACKAGE_NAME}) )
install(
# install library target and config files FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
install(TARGETS ${JSON_TARGET_NAME} DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
EXPORT ${JSON_PACKAGE_NAME}) )
install(FILES "src/json.hpp" install(
DESTINATION ${JSON_INCLUDE_DESTINATION}) TARGETS ${NLOHMANN_JSON_TARGET_NAME}
install(EXPORT ${JSON_PACKAGE_NAME} EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
FILE ${JSON_TARGETS_FILENAME} INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
DESTINATION ${JSON_CONFIG_DESTINATION}) )
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}" install(
"${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
DESTINATION ${JSON_CONFIG_DESTINATION}) DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)
@PACKAGE_INIT@ @PACKAGE_INIT@
set_and_check(JSON_INCLUDE_DIR "@PACKAGE_JSON_INCLUDE_DESTINATION@") include("${CMAKE_CURRENT_LIST_DIR}/@NLOHMANN_JSON_TARGETS_EXPORT_NAME@.cmake")
check_required_components("@PROJECT_NAME@")
cmake_policy(PUSH)
cmake_policy(SET CMP0024 OLD)
include(${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_FILENAME@)
cmake_policy(POP)
...@@ -842,8 +842,7 @@ TEST_CASE("constructors") ...@@ -842,8 +842,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l; json j(json::initializer_list_t {});
json j(l);
CHECK(j.type() == json::value_t::object); CHECK(j.type() == json::value_t::object);
} }
...@@ -860,8 +859,7 @@ TEST_CASE("constructors") ...@@ -860,8 +859,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json(json::array_t())}; json j(json::initializer_list_t {json(json::array_t())});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -876,8 +874,7 @@ TEST_CASE("constructors") ...@@ -876,8 +874,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json(json::object_t())}; json j(json::initializer_list_t {json(json::object_t())});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -892,8 +889,7 @@ TEST_CASE("constructors") ...@@ -892,8 +889,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json("Hello world")}; json j(json::initializer_list_t {json("Hello world")});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -908,8 +904,7 @@ TEST_CASE("constructors") ...@@ -908,8 +904,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json(true)}; json j(json::initializer_list_t {json(true)});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -924,8 +919,7 @@ TEST_CASE("constructors") ...@@ -924,8 +919,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json(1)}; json j(json::initializer_list_t {json(1)});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -940,8 +934,7 @@ TEST_CASE("constructors") ...@@ -940,8 +934,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json(1u)}; json j(json::initializer_list_t {json(1u)});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -956,8 +949,7 @@ TEST_CASE("constructors") ...@@ -956,8 +949,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {json(42.23)}; json j(json::initializer_list_t {json(42.23)});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -973,8 +965,7 @@ TEST_CASE("constructors") ...@@ -973,8 +965,7 @@ TEST_CASE("constructors")
{ {
SECTION("explicit") SECTION("explicit")
{ {
std::initializer_list<json> l = {1, 1u, 42.23, true, nullptr, json::object_t(), json::array_t()}; json j(json::initializer_list_t {1, 1u, 42.23, true, nullptr, json::object_t(), json::array_t()});
json j(l);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
...@@ -1034,6 +1025,125 @@ TEST_CASE("constructors") ...@@ -1034,6 +1025,125 @@ TEST_CASE("constructors")
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
} }
SECTION("move from initializer_list")
{
SECTION("string")
{
// This should break through any short string optimization in std::string
std::string source(1024, '!');
const char* source_addr = source.data();
SECTION("constructor with implicit types (array)")
{
json j = {std::move(source)};
CHECK(j[0].get_ref<std::string const&>().data() == source_addr);
}
SECTION("constructor with implicit types (object)")
{
json j = {{"key", std::move(source)}};
CHECK(j["key"].get_ref<std::string const&>().data() == source_addr);
}
SECTION("constructor with implicit types (object key)")
{
json j = {{std::move(source), 42}};
CHECK(j.get_ref<json::object_t&>().begin()->first.data() == source_addr);
}
}
SECTION("array")
{
json::array_t source = {1, 2, 3};
const json* source_addr = source.data();
SECTION("constructor with implicit types (array)")
{
json j {std::move(source)};
CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr);
}
SECTION("constructor with implicit types (object)")
{
json j {{"key", std::move(source)}};
CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr);
}
SECTION("assignment with implicit types (array)")
{
json j = {std::move(source)};
CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr);
}
SECTION("assignment with implicit types (object)")
{
json j = {{"key", std::move(source)}};
CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr);
}
}
SECTION("object")
{
json::object_t source = {{"hello", "world"}};
const json* source_addr = &source.at("hello");
SECTION("constructor with implicit types (array)")
{
json j {std::move(source)};
CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr);
}
SECTION("constructor with implicit types (object)")
{
json j {{"key", std::move(source)}};
CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr);
}
SECTION("assignment with implicit types (array)")
{
json j = {std::move(source)};
CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr);
}
SECTION("assignment with implicit types (object)")
{
json j = {{"key", std::move(source)}};
CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr);
}
}
SECTION("json")
{
json source {1, 2, 3};
const json* source_addr = &source[0];
SECTION("constructor with implicit types (array)")
{
json j {std::move(source), {}};
CHECK(&j[0][0] == source_addr);
}
SECTION("constructor with implicit types (object)")
{
json j {{"key", std::move(source)}};
CHECK(&j["key"][0] == source_addr);
}
SECTION("assignment with implicit types (array)")
{
json j = {std::move(source), {}};
CHECK(&j[0][0] == source_addr);
}
SECTION("assignment with implicit types (object)")
{
json j = {{"key", std::move(source)}};
CHECK(&j["key"][0] == source_addr);
}
}
}
} }
SECTION("create an array of n copies of a given value") SECTION("create an array of n copies of a given value")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment