use #include <nlohmann/json.hpp> to avoid filename clashes
alternative for #20
This commit is contained in:
parent
95e07469ae
commit
6f69d01b58
@ -7,19 +7,19 @@ cmake_minimum_required(VERSION 3.2)
|
|||||||
if(NOT TARGET json-hpp)
|
if(NOT TARGET json-hpp)
|
||||||
set(NLOHMANN_JSON_DIR "" CACHE STRING "path to json.hpp")
|
set(NLOHMANN_JSON_DIR "" CACHE STRING "path to json.hpp")
|
||||||
|
|
||||||
# find nlohmann's json.hpp
|
# find nlohmann's json.hpp
|
||||||
find_path(JSON_HPP json.hpp
|
find_path(JSON_HPP nlohmann/json.hpp
|
||||||
PATHS
|
PATHS
|
||||||
${CMAKE_BINARY_DIR}/${NLOHMANN_JSON_DIR} # in case it is a relative path
|
${NLOHMANN_JSON_DIR}
|
||||||
${NLOHMANN_JSON_DIR})
|
${CMAKE_BINARY_DIR}/${NLOHMANN_JSON_DIR}) # in case it is a relative path
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
# get the full, real path
|
# get the full, real path
|
||||||
get_filename_component(NLOHMANN_JSON_REALPATH ${JSON_HPP} REALPATH)
|
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
|
# create an interface-library for simple cmake-linking
|
||||||
add_library(json-hpp INTERFACE)
|
add_library(json-hpp INTERFACE)
|
||||||
target_include_directories(json-hpp
|
target_include_directories(json-hpp
|
||||||
|
|||||||
24
README.md
24
README.md
@ -36,9 +36,15 @@ cross-schema reference, it will not stop. (Though I haven't tested it)
|
|||||||
|
|
||||||
# How to use
|
# 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
|
## Build
|
||||||
|
|
||||||
Directly
|
### Within a build-dir
|
||||||
|
|
||||||
```Bash
|
```Bash
|
||||||
git clone https://github.com/pboettch/json-schema-validator.git
|
git clone https://github.com/pboettch/json-schema-validator.git
|
||||||
@ -46,19 +52,19 @@ cd json-schema-validator
|
|||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. \
|
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
|
-DJSON_SCHEMA_TEST_SUITE_PATH=<path/to/JSON-Schema-test-suite> # optional
|
||||||
make # install
|
make # install
|
||||||
ctest # if test-suite has been given
|
ctest # if test-suite has been given
|
||||||
```
|
```
|
||||||
or from another CMakeLists.txt as a subdirectory:
|
### As a subdirectory from within
|
||||||
|
|
||||||
```CMake
|
```CMake
|
||||||
# create an interface-target called json-hpp
|
# create an interface-target called json-hpp
|
||||||
add_library(json-hpp INTERFACE)
|
add_library(json-hpp INTERFACE)
|
||||||
target_include_directories(json-hpp
|
target_include_directories(json-hpp
|
||||||
INTERFACE
|
INTERFACE
|
||||||
path/to/json.hpp)
|
path/to/nlohmann/json.hpp)
|
||||||
|
|
||||||
# set this path to schema-test-suite to get tests compiled - optional
|
# set this path to schema-test-suite to get tests compiled - optional
|
||||||
set(JSON_SCHEMA_TEST_SUITE_PATH "path/to/json-schema-test-suite")
|
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}};
|
static json good_person = {{"name", "Albert"}, {"age", 42}};
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
/* json-parse the schema */
|
/* json-parse the schema */
|
||||||
|
|
||||||
json_validator validator; // create validator
|
json_validator validator; // create validator
|
||||||
|
|
||||||
try {
|
try {
|
||||||
validator.set_root_schema(person_schema); // insert root-schema
|
validator.set_root_schema(person_schema); // insert root-schema
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
|
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* json-parse the people */
|
/* json-parse the people */
|
||||||
|
|
||||||
for (auto &person : {bad_person, good_person})
|
for (auto &person : {bad_person, good_person})
|
||||||
{
|
{
|
||||||
std::cout << "About to validate this person:\n" << std::setw(2) << person << std::endl;
|
std::cout << "About to validate this person:\n" << std::setw(2) << person << std::endl;
|
||||||
|
|||||||
@ -36,7 +36,7 @@
|
|||||||
# define JSON_SCHEMA_VALIDATOR_API
|
# define JSON_SCHEMA_VALIDATOR_API
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
// make yourself a home - welcome to nlohmann's namespace
|
// make yourself a home - welcome to nlohmann's namespace
|
||||||
namespace nlohmann
|
namespace nlohmann
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user