diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cd7e8b..dce992f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/README.md b/README.md index 4fbbdcf..c604c0d 100644 --- a/README.md +++ b/README.md @@ -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= \ + -DNLOHMANN_JSON_DIR=nlohmann/json.hpp \ -DJSON_SCHEMA_TEST_SUITE_PATH= # 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; diff --git a/src/json-schema.hpp b/src/json-schema.hpp index 71ea25c..2817fe3 100644 --- a/src/json-schema.hpp +++ b/src/json-schema.hpp @@ -36,7 +36,7 @@ # define JSON_SCHEMA_VALIDATOR_API #endif -#include +#include // make yourself a home - welcome to nlohmann's namespace namespace nlohmann