From 83fd6a8a1fa5d1209895948996b4c818fcdb26ee Mon Sep 17 00:00:00 2001 From: Felix Benning Date: Sun, 7 Mar 2021 19:53:29 +0100 Subject: [PATCH 1/3] remove-semicolon --- src/json-patch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/json-patch.cpp b/src/json-patch.cpp index 2276c92..d0cd2e4 100644 --- a/src/json-patch.cpp +++ b/src/json-patch.cpp @@ -66,7 +66,7 @@ const nlohmann::json patch_schema = R"patch({ } } })patch"_json; -}; // namespace +} // namespace namespace nlohmann { From 3893a2c3afe4943948e9eb8099ee2fee8434a683 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Tue, 9 Feb 2021 10:57:09 +0100 Subject: [PATCH 2/3] fix #143: the whole unknown keywords-hierarchy needs to be an object Even if the key is a number in a string. See issue for more details. --- src/json-validator.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 94691a2..7d602ac 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -13,6 +13,7 @@ #include #include #include +#include using nlohmann::json; using nlohmann::json_patch; @@ -171,11 +172,31 @@ public: auto unresolved = file.unresolved.find(fragment); if (unresolved != file.unresolved.end()) schema::make(value, this, {}, {{new_uri}}); - else // no, nothing ref'd it, keep for later - // could use fragment here, but better not to: if key is an integer the pointer will be interpreted as - // an array. This cannot be the case here, so we force using key as a string when storing the - // unknown_keyword-schema. - file.unknown_keywords[uri.pointer()][key] = value; + else { // no, nothing ref'd it, keep for later + + // need to create an object for each reference-token in the + // JSON-Pointer When not existing, a stringified integer reference + // token (e.g. "123") in the middle of the pointer will be + // interpreted a an array-index and an array will be created. + + // json_pointer's reference_tokens is private - get them + std::deque ref_tokens; + auto uri_pointer = uri.pointer(); + while (!uri_pointer.empty()) { + ref_tokens.push_front(uri_pointer.back()); + uri_pointer.pop_back(); + } + + // for each token create an object, if not already existing + auto unk_kw = &file.unknown_keywords; + for (auto &rt : ref_tokens) { + auto existing_object = unk_kw->find(rt); + if (existing_object == unk_kw->end()) + (*unk_kw)[rt] = json::object(); + unk_kw = &(*unk_kw)[rt]; + } + (*unk_kw)[key] = value; + } // recursively add possible subschemas of unknown keywords if (value.type() == json::value_t::object) From ecaeea06bcf833611b4cc8296dfc3396b57fe136 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Tue, 9 Mar 2021 11:04:15 +0100 Subject: [PATCH 3/3] Add an example for how to use with cmake-submodules fix #147 fix #148 --- README.md | 2 ++ example/cmake-submodule/CMakeLists.txt | 24 ++++++++++++++++++++++++ example/cmake-submodule/validate.cpp | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 example/cmake-submodule/CMakeLists.txt create mode 100644 example/cmake-submodule/validate.cpp diff --git a/README.md b/README.md index 6ae1b88..84631d6 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ access nlohmann-json: a sub-directory (via `add_subdirectory()`), 2 and 3 can be assisted by setting the `nlohmann_json_DIR`-variable. +For 1 there is an example of you to do in example/cmake-submodule. + ### Building with Hunter package manager To enable access to nlohmann json library, Hunter can be used. Just run with HUNTER_ENABLED=ON option. No further dependencies needed diff --git a/example/cmake-submodule/CMakeLists.txt b/example/cmake-submodule/CMakeLists.txt new file mode 100644 index 0000000..d776fa2 --- /dev/null +++ b/example/cmake-submodule/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.2) + +project(simple-json-validator-as-submodule-test) + +# absolute-path +set(PATH_TO_NLOHMANN_JSON_REPO $ENV{HOME}/devel/upstream/json) + +# build shared library +set(BUILD_SHARED_LIBS ON) + +# JSON library +option(JSON_BuildTests OFF) +add_subdirectory(${PATH_TO_NLOHMANN_JSON_REPO} + json-binary-dir + EXCLUDE_FROM_ALL) + +# JSON SCHEMA VALIDATOR library +option(BUILD_TESTS OFF) +add_subdirectory(../.. + json-validator-binary-dir + EXCLUDE_FROM_ALL) + +add_executable(validate validate.cpp) +target_link_libraries(validate nlohmann_json_schema_validator) diff --git a/example/cmake-submodule/validate.cpp b/example/cmake-submodule/validate.cpp new file mode 100644 index 0000000..d8f3c12 --- /dev/null +++ b/example/cmake-submodule/validate.cpp @@ -0,0 +1,20 @@ +#include + +#include + +int main(void) +{ + nlohmann::json schema_json{{"type", "number"}}; + nlohmann::json_schema::json_validator validator; + + validator.set_root_schema(schema_json); + + validator.validate(1); + try { + validator.validate("\"1\""); + } catch (const std::exception &e) { + std::cerr << "expected exception: " << e.what() << "\n"; + return EXIT_SUCCESS; + } + return EXIT_FAILURE; +}