use #include <nlohmann/json.hpp> to avoid filename clashes

alternative for #20
This commit is contained in:
Patrick Boettcher 2018-02-27 11:11:55 +01:00
parent 95e07469ae
commit 6f69d01b58
3 changed files with 24 additions and 18 deletions

View File

@ -7,19 +7,19 @@ cmake_minimum_required(VERSION 3.2)
if(NOT TARGET json-hpp)
set(NLOHMANN_JSON_DIR "" CACHE STRING "path to json.hpp")
# find nlohmann's json.hpp
find_path(JSON_HPP json.hpp
# find nlohmann's json.hpp
find_path(JSON_HPP nlohmann/json.hpp
PATHS
${CMAKE_BINARY_DIR}/${NLOHMANN_JSON_DIR} # in case it is a relative path
${NLOHMANN_JSON_DIR})
if(NOT JSON_HPP)
message(FATAL_ERROR "please set NLOHMANN_JSON_DIR to a path in which NLohmann's json.hpp can be found.")
endif()
${NLOHMANN_JSON_DIR}
${CMAKE_BINARY_DIR}/${NLOHMANN_JSON_DIR}) # in case it is a relative path
# get the full, real path
get_filename_component(NLOHMANN_JSON_REALPATH ${JSON_HPP} REALPATH)
if(NOT EXISTS ${NLOHMANN_JSON_REALPATH}/nlohmann/json.hpp)
message(FATAL_ERROR "please set NLOHMANN_JSON_DIR to a path in which NLohmann's json.hpp can be found. Looking for nlohmann/json.hpp in '${NLOHMANN_JSON_REALPATH}")
endif()
# create an interface-library for simple cmake-linking
add_library(json-hpp INTERFACE)
target_include_directories(json-hpp

View File

@ -36,9 +36,15 @@ cross-schema reference, it will not stop. (Though I haven't tested it)
# How to use
The current state of the build-system needs at least version **3.1.1** of NLohmann's
JSON library. It is looking for the `json.hpp` within a `nlohmann/`-path.
When build the library you need to provide the path to the directory where the include-file
is located as `nlohmann/json.hpp`.
## Build
Directly
### Within a build-dir
```Bash
git clone https://github.com/pboettch/json-schema-validator.git
@ -46,19 +52,19 @@ cd json-schema-validator
mkdir build
cd build
cmake .. \
-DNLOHMANN_JSON_DIR=<path/to/json.hpp> \
-DNLOHMANN_JSON_DIR=<path/to/>nlohmann/json.hpp \
-DJSON_SCHEMA_TEST_SUITE_PATH=<path/to/JSON-Schema-test-suite> # optional
make # install
ctest # if test-suite has been given
```
or from another CMakeLists.txt as a subdirectory:
### As a subdirectory from within
```CMake
# create an interface-target called json-hpp
add_library(json-hpp INTERFACE)
target_include_directories(json-hpp
INTERFACE
path/to/json.hpp)
path/to/nlohmann/json.hpp)
# set this path to schema-test-suite to get tests compiled - optional
set(JSON_SCHEMA_TEST_SUITE_PATH "path/to/json-schema-test-suite")
@ -111,20 +117,20 @@ static json bad_person = {{"age", 42}};
static json good_person = {{"name", "Albert"}, {"age", 42}};
int main(){
/* json-parse the schema */
json_validator validator; // create validator
try {
validator.set_root_schema(person_schema); // insert root-schema
} catch (const std::exception &e) {
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
return EXIT_FAILURE;
}
/* json-parse the people */
for (auto &person : {bad_person, good_person})
{
std::cout << "About to validate this person:\n" << std::setw(2) << person << std::endl;

View File

@ -36,7 +36,7 @@
# define JSON_SCHEMA_VALIDATOR_API
#endif
#include <json.hpp>
#include <nlohmann/json.hpp>
// make yourself a home - welcome to nlohmann's namespace
namespace nlohmann