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)
# define project variables
set(JSON_TARGET_NAME ${PROJECT_NAME})
set(JSON_PACKAGE_NAME ${JSON_TARGET_NAME})
set(JSON_TARGETS_FILENAME "${JSON_PACKAGE_NAME}Targets.cmake")
set(JSON_CONFIG_FILENAME "${JSON_PACKAGE_NAME}Config.cmake")
set(JSON_CONFIGVERSION_FILENAME "${JSON_PACKAGE_NAME}ConfigVersion.cmake")
set(JSON_CONFIG_DESTINATION "cmake")
set(JSON_INCLUDE_DESTINATION "include/nlohmann")
##
## CONFIGURATION
##
set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME})
set(NLOHMANN_JSON_SOURCE_DIR "src/")
set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}")
set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "include")
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
add_library(${JSON_TARGET_NAME} INTERFACE)
target_include_directories(${JSON_TARGET_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:${JSON_INCLUDE_DESTINATION}>)
# create and configure the unit test target
target_include_directories(
${NLOHMANN_JSON_TARGET_NAME}
INTERFACE $<INSTALL_INTERFACE:include/>
)
##
## TESTS
## create and configure the unit test target
##
if(JSON_BuildTests)
enable_testing()
include_directories(${NLOHMANN_JSON_SOURCE_DIR})
add_subdirectory(test)
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)
configure_package_config_file("cmake/config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}"
INSTALL_DESTINATION ${JSON_CONFIG_DESTINATION}
PATH_VARS JSON_INCLUDE_DESTINATION)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
# export the library target and store build directory in package registry
export(TARGETS ${JSON_TARGET_NAME}
FILE "${CMAKE_CURRENT_BINARY_DIR}/${JSON_TARGETS_FILENAME}")
export(PACKAGE ${JSON_PACKAGE_NAME})
# install library target and config files
install(TARGETS ${JSON_TARGET_NAME}
EXPORT ${JSON_PACKAGE_NAME})
install(FILES "src/json.hpp"
DESTINATION ${JSON_INCLUDE_DESTINATION})
install(EXPORT ${JSON_PACKAGE_NAME}
FILE ${JSON_TARGETS_FILENAME}
DESTINATION ${JSON_CONFIG_DESTINATION})
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}"
"${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}"
DESTINATION ${JSON_CONFIG_DESTINATION})
write_basic_package_version_file(
${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE}
${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE}
INSTALL_DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)
install(
DIRECTORY ${NLOHMANN_JSON_SOURCE_DIR}
DESTINATION ${NLOHMANN_JSON_HEADER_INSTALL_DIR}
)
install(
FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)
install(
TARGETS ${NLOHMANN_JSON_TARGET_NAME}
EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
)
install(
EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)
@PACKAGE_INIT@
set_and_check(JSON_INCLUDE_DIR "@PACKAGE_JSON_INCLUDE_DESTINATION@")
cmake_policy(PUSH)
cmake_policy(SET CMP0024 OLD)
include(${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_FILENAME@)
cmake_policy(POP)
include("${CMAKE_CURRENT_LIST_DIR}/@NLOHMANN_JSON_TARGETS_EXPORT_NAME@.cmake")
check_required_components("@PROJECT_NAME@")
......@@ -842,8 +842,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l;
json j(l);
json j(json::initializer_list_t {});
CHECK(j.type() == json::value_t::object);
}
......@@ -860,8 +859,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json(json::array_t())};
json j(l);
json j(json::initializer_list_t {json(json::array_t())});
CHECK(j.type() == json::value_t::array);
}
......@@ -876,8 +874,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json(json::object_t())};
json j(l);
json j(json::initializer_list_t {json(json::object_t())});
CHECK(j.type() == json::value_t::array);
}
......@@ -892,8 +889,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json("Hello world")};
json j(l);
json j(json::initializer_list_t {json("Hello world")});
CHECK(j.type() == json::value_t::array);
}
......@@ -908,8 +904,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json(true)};
json j(l);
json j(json::initializer_list_t {json(true)});
CHECK(j.type() == json::value_t::array);
}
......@@ -924,8 +919,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json(1)};
json j(l);
json j(json::initializer_list_t {json(1)});
CHECK(j.type() == json::value_t::array);
}
......@@ -940,8 +934,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json(1u)};
json j(l);
json j(json::initializer_list_t {json(1u)});
CHECK(j.type() == json::value_t::array);
}
......@@ -956,8 +949,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {json(42.23)};
json j(l);
json j(json::initializer_list_t {json(42.23)});
CHECK(j.type() == json::value_t::array);
}
......@@ -973,8 +965,7 @@ TEST_CASE("constructors")
{
SECTION("explicit")
{
std::initializer_list<json> l = {1, 1u, 42.23, true, nullptr, json::object_t(), json::array_t()};
json j(l);
json j(json::initializer_list_t {1, 1u, 42.23, true, nullptr, json::object_t(), json::array_t()});
CHECK(j.type() == json::value_t::array);
}
......@@ -1034,6 +1025,125 @@ TEST_CASE("constructors")
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")
......
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