Merge pull request #2 from pboettch/master

Merge upstream
This commit is contained in:
Ian Bell 2018-02-27 21:51:13 -07:00 committed by GitHub
commit ecddd4b146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 34 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
@ -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")

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")
@ -67,50 +73,87 @@ 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`.
```C++
#include <iostream>
#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 people */
/* 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;
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

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