Move functions for parsing/unpacking JSON Reference strings to separate header file

This commit is contained in:
Tristan Penman 2015-10-16 21:33:40 +11:00
parent b54e0b85ae
commit 9b3779a57b
3 changed files with 71 additions and 51 deletions

View File

@ -0,0 +1,65 @@
#ifndef __VALIJSON_INTERNAL_JSON_REFERENCE_HPP
#define __VALIJSON_INTERNAL_JSON_REFERENCE_HPP
#include <stdexcept>
#include <string>
#include <boost/optional.hpp>
namespace valijson {
namespace internal {
namespace json_reference {
/**
* @brief Extract URI from JSON Reference relative to the current schema
*
* @param jsonRef JSON Reference to extract from
* @param schema Schema that JSON Reference URI is relative to
*
* @return Optional string containing URI
*/
inline boost::optional<std::string> getJsonReferenceUri(
const std::string &jsonRef)
{
const size_t ptrPos = jsonRef.find("#");
if (ptrPos == 0) {
// The JSON Reference does not contain a URI, but might contain a
// JSON Pointer that refers to the current document
return boost::none;
} else if (ptrPos != std::string::npos) {
// The JSON Reference contains a URI and possibly a JSON Pointer
return jsonRef.substr(0, ptrPos);
}
// The entire JSON Reference should be treated as a URI
return jsonRef;
}
/**
* @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");
}
} // namespace json_reference
} // namespace internal
} // namespace valijson
#endif

View File

@ -12,6 +12,7 @@
#include <valijson/adapters/adapter.hpp>
#include <valijson/constraints/concrete_constraints.hpp>
#include <valijson/internal/json_pointer.hpp>
#include <valijson/internal/json_reference.hpp>
#include <valijson/schema.hpp>
namespace valijson {
@ -305,55 +306,6 @@ private:
}
}
/**
* @brief Extract URI from JSON Reference relative to the current schema
*
* @param jsonRef JSON Reference to extract from
* @param schema Schema that JSON Reference URI is relative to
*
* @return Optional string containing URI
*/
inline boost::optional<std::string> getJsonReferenceUri(
const std::string &jsonRef,
const Schema &schema)
{
const size_t ptrPos = jsonRef.find("#");
if (ptrPos == 0) {
// The JSON Reference does not contain a URI, but might contain a
// JSON Pointer that refers to the current document
return boost::none;
} else if (ptrPos != std::string::npos) {
// The JSON Reference contains a URI and possibly a JSON Pointer
return schema.resolveUri(jsonRef.substr(0, ptrPos));
}
// The entire JSON Reference should be treated as a URI
return schema.resolveUri(jsonRef);
}
/**
* @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
*
@ -386,10 +338,11 @@ private:
// Returns a document URI if the reference points somewhere
// other than the current document
const boost::optional<std::string> documentUri =
getJsonReferenceUri(jsonRef, schema);
internal::json_reference::getJsonReferenceUri(jsonRef);
// Extract JSON Pointer from JSON Reference
const std::string jsonPointer = getJsonReferencePointer(jsonRef);
const std::string jsonPointer =
internal::json_reference::getJsonReferencePointer(jsonRef);
if (documentUri) {
// Resolve reference against remote document

View File

@ -40,6 +40,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
6A34698C1BD109A900C97DA2 /* json_reference.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = json_reference.hpp; path = internal/json_reference.hpp; sourceTree = "<group>"; };
6A477F8417D6BCBB0013571C /* libboost_regex-mt.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libboost_regex-mt.dylib"; path = "/usr/local/lib/libboost_regex-mt.dylib"; sourceTree = "<absolute>"; };
6A506D121AF884E100C2C818 /* draft-03.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "draft-03.json"; sourceTree = "<group>"; };
6A506D131AF884E100C2C818 /* draft-04.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "draft-04.json"; sourceTree = "<group>"; };
@ -328,6 +329,7 @@
isa = PBXGroup;
children = (
6A506D1E1AF88D8700C2C818 /* json_pointer.hpp */,
6A34698C1BD109A900C97DA2 /* json_reference.hpp */,
);
name = internal;
sourceTree = "<group>";