From c43c8568f5692da36ffa4a69b671d5b2d7424968 Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Wed, 6 May 2015 18:46:39 +1000 Subject: [PATCH] Rename json_reference namespace to json_pointer and move unrelated functionality back into SchemaParser class --- CMakeLists.txt | 2 +- .../{json_reference.hpp => json_pointer.hpp} | 35 +++---------------- include/valijson/schema_parser.hpp | 32 ++++++++++++++--- ...on_reference.cpp => test_json_pointer.cpp} | 8 ++--- xcode/valijson.xcodeproj/project.pbxproj | 12 +++---- 5 files changed, 42 insertions(+), 47 deletions(-) rename include/valijson/internal/{json_reference.hpp => json_pointer.hpp} (85%) rename tests/{test_json_reference.cpp => test_json_pointer.cpp} (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index cfb2588..a69fffc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ add_executable(external_schema add_executable(test_suite tests/test_adapter_comparison.cpp tests/test_fetch_document_callback.cpp - tests/test_json_reference.cpp + tests/test_json_pointer.cpp tests/test_jsoncpp_adapter.cpp tests/test_property_tree_adapter.cpp tests/test_rapidjson_adapter.cpp diff --git a/include/valijson/internal/json_reference.hpp b/include/valijson/internal/json_pointer.hpp similarity index 85% rename from include/valijson/internal/json_reference.hpp rename to include/valijson/internal/json_pointer.hpp index 7c1574a..093cf47 100644 --- a/include/valijson/internal/json_reference.hpp +++ b/include/valijson/internal/json_pointer.hpp @@ -9,7 +9,9 @@ #include -namespace { +namespace valijson { +namespace internal { +namespace json_pointer { /** * @brief Recursively locate the value referenced by a JSON Pointer @@ -112,35 +114,6 @@ inline AdapterType resolveJsonPointer( std::string(jsonPointerNext, jsonPointerEnd)); } -} // end anonymous namespace - -namespace valijson { -namespace internal { -namespace json_reference { - -/** - * @brief Extract JSON Pointer portion of a JSON Reference - * - * @param jsonRef JSON Reference to extract from - * - * @return string containing JSON Pointer - * - * @throw std::runtime_error if the string does not contain a JSON Pointer - */ -inline std::string getJsonReferencePointer(const std::string &jsonRef) -{ - // Attempt to extract JSON Pointer if '#' character is present. Note - // that a valid pointer would contain at least a leading forward - // slash character. - const size_t ptrPos = jsonRef.find("#"); - if (ptrPos != std::string::npos) { - return jsonRef.substr(ptrPos + 1); - } - - throw std::runtime_error( - "JSON Reference value does not contain a valid JSON Pointer"); -} - /** * @brief Return the JSON Value referenced by a JSON Pointer * @@ -154,7 +127,7 @@ inline AdapterType resolveJsonPointer( const AdapterType &rootNode, const std::string &jsonPointer) { - return ::resolveJsonPointer(rootNode, jsonPointer, jsonPointer.begin()); + return resolveJsonPointer(rootNode, jsonPointer, jsonPointer.begin()); } } // namespace json_reference diff --git a/include/valijson/schema_parser.hpp b/include/valijson/schema_parser.hpp index d9ccea7..ad7c354 100644 --- a/include/valijson/schema_parser.hpp +++ b/include/valijson/schema_parser.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include namespace valijson { @@ -288,6 +288,29 @@ private: } } + /** + * @brief Extract JSON Pointer portion of a JSON Reference + * + * @param jsonRef JSON Reference to extract from + * + * @return string containing JSON Pointer + * + * @throw std::runtime_error if the string does not contain a JSON Pointer + */ + inline std::string getJsonReferencePointer(const std::string &jsonRef) + { + // Attempt to extract JSON Pointer if '#' character is present. Note + // that a valid pointer would contain at least a leading forward + // slash character. + const size_t ptrPos = jsonRef.find("#"); + if (ptrPos != std::string::npos) { + return jsonRef.substr(ptrPos + 1); + } + + throw std::runtime_error( + "JSON Reference value does not contain a valid JSON Pointer"); + } + /** * @brief Populate a schema using a JSON Reference * @@ -319,8 +342,7 @@ private: getJsonReferenceUri(jsonRef, schema); // Extract JSON Pointer from JSON Reference - const std::string jsonPointer = - internal::json_reference::getJsonReferencePointer(jsonRef); + const std::string jsonPointer = getJsonReferencePointer(jsonRef); if (documentUri) { // Resolve reference against remote document @@ -342,7 +364,7 @@ private: } const AdapterType &ref = - internal::json_reference::resolveJsonPointer(*docPtr, + internal::json_pointer::resolveJsonPointer(*docPtr, jsonPointer); // Resolve reference against retrieved document @@ -351,7 +373,7 @@ private: } else { const AdapterType &ref = - internal::json_reference::resolveJsonPointer(node, + internal::json_pointer::resolveJsonPointer(node, jsonPointer); // Resolve reference against current document diff --git a/tests/test_json_reference.cpp b/tests/test_json_pointer.cpp similarity index 95% rename from tests/test_json_reference.cpp rename to tests/test_json_pointer.cpp index c930ae0..3d4343b 100644 --- a/tests/test_json_reference.cpp +++ b/tests/test_json_pointer.cpp @@ -3,17 +3,17 @@ #include -#include +#include #include using valijson::adapters::RapidJsonAdapter; -using valijson::internal::json_reference::resolveJsonPointer; +using valijson::internal::json_pointer::resolveJsonPointer; typedef rapidjson::MemoryPoolAllocator RapidJsonCrtAllocator; -class TestJsonReference : public testing::Test +class TestJsonPointer : public testing::Test { }; @@ -107,7 +107,7 @@ std::vector > return testCases; } -TEST_F(TestJsonReference, JsonPointerTestCases) +TEST_F(TestJsonPointer, JsonPointerTestCases) { typedef std::vector > TestCases; diff --git a/xcode/valijson.xcodeproj/project.pbxproj b/xcode/valijson.xcodeproj/project.pbxproj index 66f7bbc..db6efc9 100644 --- a/xcode/valijson.xcodeproj/project.pbxproj +++ b/xcode/valijson.xcodeproj/project.pbxproj @@ -9,7 +9,7 @@ /* Begin PBXBuildFile section */ 6A477F8517D6BCBB0013571C /* libboost_regex-mt.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A477F8417D6BCBB0013571C /* libboost_regex-mt.dylib */; }; 6A477F8617D6EA000013571C /* libboost_regex-mt.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A477F8417D6BCBB0013571C /* libboost_regex-mt.dylib */; }; - 6A506D201AF88E5D00C2C818 /* test_json_reference.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A506D1F1AF88E5D00C2C818 /* test_json_reference.cpp */; }; + 6A506D201AF88E5D00C2C818 /* test_json_pointer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A506D1F1AF88E5D00C2C818 /* test_json_pointer.cpp */; }; 6A725F4517F61D7000D6B2FF /* test_validation_errors.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A725F4317F61B5100D6B2FF /* test_validation_errors.cpp */; }; 6A725F4617F6404100D6B2FF /* test_adapter_comparison.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6AB8FEB117E6DF9A0028E147 /* test_adapter_comparison.cpp */; }; 6A725F4717F6404100D6B2FF /* test_jsoncpp_adapter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6AB8FE9217E6BE770028E147 /* test_jsoncpp_adapter.cpp */; }; @@ -51,8 +51,8 @@ 6A506D1A1AF884E100C2C818 /* rfc3986-uri.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "rfc3986-uri.txt"; sourceTree = ""; }; 6A506D1B1AF884E100C2C818 /* rfc4627-json.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "rfc4627-json.txt"; sourceTree = ""; }; 6A506D1C1AF884E100C2C818 /* rfc6901-json-pointer.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "rfc6901-json-pointer.txt"; sourceTree = ""; }; - 6A506D1E1AF88D8700C2C818 /* json_reference.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = json_reference.hpp; path = internal/json_reference.hpp; sourceTree = ""; }; - 6A506D1F1AF88E5D00C2C818 /* test_json_reference.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_json_reference.cpp; sourceTree = ""; }; + 6A506D1E1AF88D8700C2C818 /* json_pointer.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = json_pointer.hpp; path = internal/json_pointer.hpp; sourceTree = ""; }; + 6A506D1F1AF88E5D00C2C818 /* test_json_pointer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_json_pointer.cpp; sourceTree = ""; }; 6A725F3617F61A4400D6B2FF /* array_doubles_10_20_30_40.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = array_doubles_10_20_30_40.json; sourceTree = ""; }; 6A725F3717F61A4400D6B2FF /* array_doubles_1_2_3.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = array_doubles_1_2_3.json; sourceTree = ""; }; 6A725F3817F61A4400D6B2FF /* array_doubles_1_2_3_4.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = array_doubles_1_2_3_4.json; sourceTree = ""; }; @@ -324,7 +324,7 @@ 6A506D1D1AF88D5E00C2C818 /* internal */ = { isa = PBXGroup; children = ( - 6A506D1E1AF88D8700C2C818 /* json_reference.hpp */, + 6A506D1E1AF88D8700C2C818 /* json_pointer.hpp */, ); name = internal; sourceTree = ""; @@ -668,7 +668,7 @@ 6AB8FEB417E6E53D0028E147 /* data */, 6AB8FEB117E6DF9A0028E147 /* test_adapter_comparison.cpp */, 6AA8A5DA17F8BDCA002728A0 /* test_fetch_document_callback.cpp */, - 6A506D1F1AF88E5D00C2C818 /* test_json_reference.cpp */, + 6A506D1F1AF88E5D00C2C818 /* test_json_pointer.cpp */, 6AB8FE9217E6BE770028E147 /* test_jsoncpp_adapter.cpp */, 6AB8FEC417E92B100028E147 /* test_property_tree_adapter.cpp */, 6AC18D3917CC874100FE0EC9 /* test_rapidjson_adapter.cpp */, @@ -969,7 +969,7 @@ 6A725F4717F6404100D6B2FF /* test_jsoncpp_adapter.cpp in Sources */, 6A725F4817F6404100D6B2FF /* test_property_tree_adapter.cpp in Sources */, 6AD3490118FF56FB004BDEE7 /* gtest_main.cc in Sources */, - 6A506D201AF88E5D00C2C818 /* test_json_reference.cpp in Sources */, + 6A506D201AF88E5D00C2C818 /* test_json_pointer.cpp in Sources */, 6A725F4917F6404100D6B2FF /* test_rapidjson_adapter.cpp in Sources */, 6A725F4A17F6404100D6B2FF /* test_validator.cpp in Sources */, 6A725F4D17F8964B00D6B2FF /* test_uri_resolution.cpp in Sources */,