Reformat header file to be more readable

Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
This commit is contained in:
Cristian Le 2023-05-10 11:55:01 +02:00
parent e5508ac4a3
commit 4bff849b25
Failed to extract signature
2 changed files with 182 additions and 132 deletions

View File

@ -12,3 +12,7 @@ SpaceAfterCStyleCast: true
TabWidth: 4 TabWidth: 4
AccessModifierOffset: -4 AccessModifierOffset: -4
UseTab: ForIndentation UseTab: ForIndentation
NamespaceIndentation: All
BraceWrapping:
AfterNamespace: false
CompactNamespaces: false

View File

@ -34,165 +34,211 @@
// make yourself a home - welcome to nlohmann's namespace // make yourself a home - welcome to nlohmann's namespace
namespace nlohmann namespace nlohmann
{ {
/**
// A class representing a JSON-URI for schemas derived from * A class representing a JSON-URI for schemas derived from
// section 8 of JSON Schema: A Media Type for Describing JSON Documents * section 8 of JSON Schema: A Media Type for Describing JSON Documents
// draft-wright-json-schema-00 * draft-wright-json-schema-00
// *
// New URIs can be derived from it using the derive()-method. * New URIs can be derived from it using the derive()-method.
// This is useful for resolving refs or subschema-IDs in json-schemas. * This is useful for resolving refs or subschema-IDs in json-schemas.
// *
// This is done implement the requirements described in section 8.2. * This is done implement the requirements described in section 8.2.
// */
class JSON_SCHEMA_VALIDATOR_API json_uri class JSON_SCHEMA_VALIDATOR_API json_uri
{
std::string urn_;
std::string scheme_;
std::string authority_;
std::string path_;
json::json_pointer pointer_; // fragment part if JSON-Pointer
std::string identifier_; // fragment part if Locatation Independent ID
protected:
// decodes a JSON uri and replaces all or part of the currently stored values
void update(const std::string &uri);
std::tuple<std::string, std::string, std::string, std::string, std::string> as_tuple() const
{ {
return std::make_tuple(urn_, scheme_, authority_, path_, identifier_ != "" ? identifier_ : pointer_.to_string()); std::string urn_;
}
public: std::string scheme_;
json_uri(const std::string &uri) std::string authority_;
std::string path_;
/**
* fragment part if JSON-Pointer
*/
json::json_pointer pointer_;
/**
* fragment part if Locatation Independent ID
*/
std::string identifier_;
protected:
/**
* decodes a JSON uri and replaces all or part of the currently stored values
*
* @param uri
*/
void update(const std::string &uri);
std::tuple<std::string, std::string, std::string, std::string, std::string> as_tuple() const
{
return std::make_tuple(urn_, scheme_, authority_, path_,
identifier_ != "" ? identifier_ : pointer_.to_string());
}
public:
json_uri(const std::string &uri)
{
update(uri);
}
const std::string &scheme() const { return scheme_; }
const std::string &authority() const { return authority_; }
const std::string &path() const { return path_; }
const json::json_pointer &pointer() const { return pointer_; }
const std::string &identifier() const { return identifier_; }
std::string fragment() const
{
if (identifier_ == "")
return pointer_.to_string();
else
return identifier_;
}
std::string url() const { return location(); }
std::string location() const;
static std::string escape(const std::string &);
/**
* create a new json_uri based in this one and the given uri
* resolves relative changes (pathes or pointers) and resets part if proto or hostname changes
*
* @param uri
* @return
*/
json_uri derive(const std::string &uri) const
{
json_uri u = *this;
u.update(uri);
return u;
}
/**
* append a pointer-field to the pointer-part of this uri
*/
json_uri append(const std::string &field) const
{
if (identifier_ != "")
return *this;
json_uri u = *this;
u.pointer_ /= field;
return u;
}
std::string to_string() const;
friend bool operator<(const json_uri &l, const json_uri &r)
{
return l.as_tuple() < r.as_tuple();
}
friend bool operator==(const json_uri &l, const json_uri &r)
{
return l.as_tuple() == r.as_tuple();
}
friend std::ostream &operator<<(std::ostream &os, const json_uri &u);
};
namespace json_schema
{ {
update(uri);
}
const std::string &scheme() const { return scheme_; } extern json draft7_schema_builtin;
const std::string &authority() const { return authority_; }
const std::string &path() const { return path_; }
const json::json_pointer &pointer() const { return pointer_; } typedef std::function<void(const json_uri & /*id*/, json & /*value*/)> schema_loader;
const std::string &identifier() const { return identifier_; } typedef std::function<void(const std::string & /*format*/, const std::string & /*value*/)> format_checker;
typedef std::function<void(const std::string & /*contentEncoding*/, const std::string & /*contentMediaType*/,
const json & /*instance*/)>
content_checker;
std::string fragment() const /**
{ * Interface for validation error handlers
if (identifier_ == "") */
return pointer_.to_string(); class JSON_SCHEMA_VALIDATOR_API error_handler
else {
return identifier_; public:
} virtual ~error_handler() {}
std::string url() const { return location(); } virtual void
std::string location() const; error(const json::json_pointer & /*ptr*/, const json & /*instance*/, const std::string & /*message*/) = 0;
};
static std::string escape(const std::string &); class JSON_SCHEMA_VALIDATOR_API basic_error_handler : public error_handler
{
bool error_{false};
// create a new json_uri based in this one and the given uri public:
// resolves relative changes (pathes or pointers) and resets part if proto or hostname changes void error(const json::json_pointer & /*ptr*/, const json & /*instance*/,
json_uri derive(const std::string &uri) const const std::string & /*message*/) override
{ {
json_uri u = *this; error_ = true;
u.update(uri); }
return u;
}
// append a pointer-field to the pointer-part of this uri virtual void reset() { error_ = false; }
json_uri append(const std::string &field) const
{
if (identifier_ != "")
return *this;
json_uri u = *this; operator bool() const { return error_; }
u.pointer_ /= field; };
return u;
}
std::string to_string() const; /**
* Checks validity of JSON schema built-in string format specifiers like 'date-time', 'ipv4', ...
*/
void JSON_SCHEMA_VALIDATOR_API default_string_format_check(const std::string &format, const std::string &value);
friend bool operator<(const json_uri &l, const json_uri &r) class root_schema;
{
return l.as_tuple() < r.as_tuple();
}
friend bool operator==(const json_uri &l, const json_uri &r) class JSON_SCHEMA_VALIDATOR_API json_validator
{ {
return l.as_tuple() == r.as_tuple(); std::unique_ptr<root_schema> root_;
}
friend std::ostream &operator<<(std::ostream &os, const json_uri &u); public:
}; json_validator(schema_loader = nullptr, format_checker = nullptr, content_checker = nullptr);
namespace json_schema json_validator(const json &, schema_loader = nullptr, format_checker = nullptr, content_checker = nullptr);
{
extern json draft7_schema_builtin; json_validator(json &&, schema_loader = nullptr, format_checker = nullptr, content_checker = nullptr);
typedef std::function<void(const json_uri & /*id*/, json & /*value*/)> schema_loader; json_validator(json_validator &&);
typedef std::function<void(const std::string & /*format*/, const std::string & /*value*/)> format_checker;
typedef std::function<void(const std::string & /*contentEncoding*/, const std::string & /*contentMediaType*/, const json & /*instance*/)> content_checker;
// Interface for validation error handlers json_validator &operator=(json_validator &&);
class JSON_SCHEMA_VALIDATOR_API error_handler
{
public:
virtual ~error_handler() {}
virtual void error(const json::json_pointer & /*ptr*/, const json & /*instance*/, const std::string & /*message*/) = 0;
};
class JSON_SCHEMA_VALIDATOR_API basic_error_handler : public error_handler json_validator(json_validator const &) = delete;
{
bool error_{false};
public: json_validator &operator=(json_validator const &) = delete;
void error(const json::json_pointer & /*ptr*/, const json & /*instance*/, const std::string & /*message*/) override
{
error_ = true;
}
virtual void reset() { error_ = false; } ~json_validator();
operator bool() const { return error_; }
};
/** /**
* Checks validity of JSON schema built-in string format specifiers like 'date-time', 'ipv4', ... * insert and set the root-schema
*/ *
void JSON_SCHEMA_VALIDATOR_API default_string_format_check(const std::string &format, const std::string &value); */
void set_root_schema(const json &);
class root_schema; void set_root_schema(json &&);
class JSON_SCHEMA_VALIDATOR_API json_validator /**
{ * validate a json-document based on the root-schema
std::unique_ptr<root_schema> root_; *
* @return
*/
json validate(const json &) const;
public: /**
json_validator(schema_loader = nullptr, format_checker = nullptr, content_checker = nullptr); * validate a json-document based on the root-schema with a custom error-handler
*
* @param initial_uri
* @return
*/
json validate(const json &, error_handler &, const json_uri &initial_uri = json_uri("#")) const;
};
json_validator(const json &, schema_loader = nullptr, format_checker = nullptr, content_checker = nullptr); } // namespace json_schema
json_validator(json &&, schema_loader = nullptr, format_checker = nullptr, content_checker = nullptr);
json_validator(json_validator &&);
json_validator &operator=(json_validator &&);
json_validator(json_validator const &) = delete;
json_validator &operator=(json_validator const &) = delete;
~json_validator();
// insert and set the root-schema
void set_root_schema(const json &);
void set_root_schema(json &&);
// validate a json-document based on the root-schema
json validate(const json &) const;
// validate a json-document based on the root-schema with a custom error-handler
json validate(const json &, error_handler &, const json_uri &initial_uri = json_uri("#")) const;
};
} // namespace json_schema
} // namespace nlohmann } // namespace nlohmann
#endif /* NLOHMANN_JSON_SCHEMA_HPP__ */ #endif /* NLOHMANN_JSON_SCHEMA_HPP__ */