From 95e07469ae5ed1e8a17ce56421a51d5f268555af Mon Sep 17 00:00:00 2001 From: Ian Bell Date: Fri, 23 Feb 2018 19:38:53 -0700 Subject: [PATCH 1/3] Add a runnable and useful exampe --- README.md | 69 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 12f24cc..4fbbdcf 100644 --- a/README.md +++ b/README.md @@ -72,45 +72,72 @@ add_subdirectory(path-to-this-project json-schema-validator) See also `app/json-schema-validate.cpp`. ```C++ +#include + #include "json-schema.hpp" using nlohmann::json; using nlohmann::json_uri; using nlohmann::json_schema_draft4::json_validator; -static void loader(const json_uri &uri, json &schema) +// The schema is defined based upon a string literal +static json person_schema = R"( { - // get the schema from uri and feed it into schema - // if not possible, otherwise, throw an excpetion + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "A person", + "properties": { + "name": { + "description": "Name", + "type": "string" + }, + "age": { + "description": "Age of the person", + "type": "number", + "minimum": 2, + "maximum": 200 + } + }, + "required": [ + "name", + "age" + ], + "type": "object" } -int main(void) -{ - json schema; +)"_json; +// The people are defined with brace initialization +static json bad_person = {{"age", 42}}; +static json good_person = {{"name", "Albert"}, {"age", 42}}; + +int main(){ + /* json-parse the schema */ - - json_validator validator(loader); // create validator with a loader-callback - + + json_validator validator; // create validator + try { - validator.set_root_schema(schema); // insert root-schema + validator.set_root_schema(person_schema); // insert root-schema } catch (const std::exception &e) { - std::cerr << "Validation failed, here is why: " << e.what() << "\n"; + std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n"; return EXIT_FAILURE; } - - json document; - - /* json-parse the document */ - - try { - validator.validate(document); // validate the document - } catch (const std::exception &e) { - std::cerr << "Validation 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; + try { + validator.validate(person); // validate the document + std::cout << "Validation succeeded\n"; + } catch (const std::exception &e) { + std::cerr << "Validation failed, here is why: " << e.what() << "\n"; + } } return EXIT_SUCCESS; } + ``` # Compliance From 6f69d01b58f63232369b7276625f09f4c7765fb3 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Tue, 27 Feb 2018 11:11:55 +0100 Subject: [PATCH 2/3] use #include to avoid filename clashes alternative for #20 --- CMakeLists.txt | 16 ++++++++-------- README.md | 24 +++++++++++++++--------- src/json-schema.hpp | 2 +- 3 files changed, 24 insertions(+), 18 deletions(-) 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 From 2a1f77d08426357e9255f94acadc509d0b4d27ec Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Tue, 27 Feb 2018 11:12:21 +0100 Subject: [PATCH 3/3] cmake: allow the building of shared or static libraries Alternative for #15 --- CMakeLists.txt | 10 ++++++---- README.md | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dce992f..2da3c4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if(NOT TARGET json-hpp) endif() # and one for the validator -add_library(json-schema-validator SHARED +add_library(json-schema-validator src/json-schema-draft4.json.cpp src/json-uri.cpp src/json-validator.cpp) @@ -49,9 +49,11 @@ endif() target_link_libraries(json-schema-validator PUBLIC json-hpp) -target_compile_definitions(json-schema-validator - PRIVATE - -DJSON_SCHEMA_VALIDATOR_EXPORTS) +if(BUILD_SHARED_LIBS) + target_compile_definitions(json-schema-validator + PRIVATE + -DJSON_SCHEMA_VALIDATOR_EXPORTS) +endif() # regex with boost if gcc < 4.9 - default is std::regex if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/README.md b/README.md index c604c0d..ac55838 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,16 @@ enable_testing() # if you want to inherit tests add_subdirectory(path-to-this-project json-schema-validator) ``` +### Building a shared library + +By default a static library is built. Shared libraries are generated by using +the `BUILD_SHARED_LIBS`-cmake variable: + +In your initial call to cmake simply add: +```bash +cmake -DBUILD_SHARED_LIBS=ON +``` + ## Code See also `app/json-schema-validate.cpp`.