MQTTSuite
Loading...
Searching...
No Matches
nlohmann::json_uri Class Reference

#include <json-schema.hpp>

Collaboration diagram for nlohmann::json_uri:

Public Member Functions

 json_uri (const std::string &uri)
const std::string & scheme () const
const std::string & authority () const
const std::string & path () const
const json::json_pointer & pointer () const
const std::string & identifier () const
std::string fragment () const
std::string url () const
std::string location () const
json_uri derive (const std::string &uri) const
json_uri append (const std::string &field) const
std::string to_string () const

Static Public Member Functions

static std::string escape (const std::string &)

Protected Member Functions

void update (const std::string &uri)
std::tuple< std::string, std::string, std::string, std::string, std::string > as_tuple () const

Private Attributes

std::string urn_
std::string scheme_
std::string authority_
std::string path_
json::json_pointer pointer_
std::string identifier_

Friends

bool operator< (const json_uri &l, const json_uri &r)
bool operator== (const json_uri &l, const json_uri &r)
std::ostream & operator<< (std::ostream &os, const json_uri &u)

Detailed Description

Definition at line 47 of file json-schema.hpp.

Constructor & Destructor Documentation

◆ json_uri()

nlohmann::json_uri::json_uri ( const std::string & uri)
inline

Definition at line 68 of file json-schema.hpp.

69 {
70 update(uri);
71 }
void update(const std::string &uri)
Definition json-uri.cpp:16

References update().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ append()

json_uri nlohmann::json_uri::append ( const std::string & field) const
inline

Definition at line 103 of file json-schema.hpp.

104 {
105 if (identifier_ != "")
106 return *this;
107
108 json_uri u = *this;
109 u.pointer_ /= field;
110 return u;
111 }
json::json_pointer pointer_
json_uri(const std::string &uri)
std::string identifier_

References pointer_.

Referenced by json_schema_validator::insert_schema(), nlohmann::json_schema::root_schema::insert_unknown_keyword(), and anonymous_namespace{json-validator.cpp}::schema::make().

Here is the caller graph for this function:

◆ as_tuple()

std::tuple< std::string, std::string, std::string, std::string, std::string > nlohmann::json_uri::as_tuple ( ) const
inlineprotected

Definition at line 62 of file json-schema.hpp.

63 {
64 return std::make_tuple(urn_, scheme_, authority_, path_, identifier_ != "" ? identifier_ : pointer_.to_string());
65 }
std::string authority_
std::string scheme_
json::json_pointer pointer_

References authority_, identifier_, path_, pointer_, scheme_, and urn_.

Referenced by operator<, and operator==.

Here is the caller graph for this function:

◆ authority()

const std::string & nlohmann::json_uri::authority ( ) const
inline

Definition at line 74 of file json-schema.hpp.

74{ return authority_; }

References authority_.

◆ derive()

json_uri nlohmann::json_uri::derive ( const std::string & uri) const
inline

Definition at line 95 of file json-schema.hpp.

96 {
97 json_uri u = *this;
98 u.update(uri);
99 return u;
100 }
void update(const std::string &uri)
Definition json-uri.cpp:16

References update().

Referenced by json_schema_validator::insert_schema(), anonymous_namespace{json-validator.cpp}::schema::make(), paths(), and pointer_plain_name().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ escape()

std::string nlohmann::json_uri::escape ( const std::string & src)
static

Definition at line 137 of file json-uri.cpp.

138{
139 std::vector<std::pair<std::string, std::string>> chars = {
140 {"~", "~0"},
141 {"/", "~1"}};
142
143 std::string l = src;
144
145 for (const auto &c : chars) {
146 std::size_t pos = 0;
147 do {
148 pos = l.find(c.first, pos);
149 if (pos == std::string::npos)
150 break;
151 l.replace(pos, 1, c.second);
152 pos += c.second.size();
153 } while (1);
154 }
155
156 return l;
157}

Referenced by json_schema_validator::insert_schema().

Here is the caller graph for this function:

◆ fragment()

std::string nlohmann::json_uri::fragment ( ) const
inline

Definition at line 80 of file json-schema.hpp.

81 {
82 if (identifier_ == "")
83 return pointer_.to_string();
84 else
85 return identifier_;
86 }

References identifier_, and pointer_.

Referenced by nlohmann::json_schema::root_schema::get_or_create_ref(), nlohmann::json_schema::root_schema::insert(), and nlohmann::json_schema::root_schema::validate().

Here is the caller graph for this function:

◆ identifier()

const std::string & nlohmann::json_uri::identifier ( ) const
inline

Definition at line 78 of file json-schema.hpp.

78{ return identifier_; }

References identifier_.

Referenced by pointer_plain_name().

Here is the caller graph for this function:

◆ location()

std::string nlohmann::json_uri::location ( ) const

Definition at line 102 of file json-uri.cpp.

103{
104 if (urn_.size())
105 return urn_;
106
107 std::stringstream s;
108
109 if (scheme_.size() > 0)
110 s << scheme_ << "://";
111
112 s << authority_
113 << path_;
114
115 return s.str();
116}

References authority_, path_, scheme_, and urn_.

Referenced by nlohmann::json_schema::root_schema::get_or_create_ref(), nlohmann::json_schema::root_schema::insert(), nlohmann::json_schema::root_schema::insert_unknown_keyword(), loader(), to_string(), url(), and nlohmann::json_schema::root_schema::validate().

Here is the caller graph for this function:

◆ path()

const std::string & nlohmann::json_uri::path ( ) const
inline

Definition at line 75 of file json-schema.hpp.

75{ return path_; }

References path_.

Referenced by loader(), loader(), and loader().

Here is the caller graph for this function:

◆ pointer()

const json::json_pointer & nlohmann::json_uri::pointer ( ) const
inline

Definition at line 77 of file json-schema.hpp.

77{ return pointer_; }

References pointer_.

Referenced by nlohmann::json_schema::root_schema::get_or_create_ref(), nlohmann::json_schema::root_schema::insert_unknown_keyword(), and pointer_plain_name().

Here is the caller graph for this function:

◆ scheme()

const std::string & nlohmann::json_uri::scheme ( ) const
inline

Definition at line 73 of file json-schema.hpp.

73{ return scheme_; }

References scheme_.

◆ to_string()

std::string nlohmann::json_uri::to_string ( ) const

Definition at line 118 of file json-uri.cpp.

119{
120 std::stringstream s;
121
122 s << location() << " # ";
123
124 if (identifier_ == "")
125 s << pointer_.to_string();
126 else
127 s << identifier_;
128
129 return s.str();
130}
std::string location() const
Definition json-uri.cpp:102

References identifier_, location(), and pointer_.

Referenced by EXPECT_EQ(), nlohmann::json_schema::root_schema::get_or_create_ref(), nlohmann::json_schema::root_schema::insert(), json_schema_validator::insert_schema(), anonymous_namespace{json-validator.cpp}::schema::make(), anonymous_namespace{json-validator.cpp}::schema_ref::make_for_default_(), operator<<, and nlohmann::json_schema::root_schema::validate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

void nlohmann::json_uri::update ( const std::string & uri)
protected

Definition at line 16 of file json-uri.cpp.

17{
18 std::string pointer = ""; // default pointer is document-root
19
20 // first split the URI into location and pointer
21 auto pointer_separator = uri.find('#');
22 if (pointer_separator != std::string::npos) { // and extract the pointer-string if found
23 pointer = uri.substr(pointer_separator + 1); // remove #
24
25 // unescape %-values IOW, decode JSON-URI-formatted JSON-pointer
26 std::size_t pos = pointer.size() - 1;
27 do {
28 pos = pointer.rfind('%', pos);
29 if (pos == std::string::npos)
30 break;
31
32 if (pos >= pointer.size() - 2) {
33 pos--;
34 continue;
35 }
36
37 std::string hex = pointer.substr(pos + 1, 2);
38 char ascii = static_cast<char>(std::strtoul(hex.c_str(), nullptr, 16));
39 pointer.replace(pos, 3, 1, ascii);
40
41 pos--;
42 } while (1);
43 }
44
45 auto location = uri.substr(0, pointer_separator);
46
47 if (location.size()) { // a location part has been found
48
49 // if it is an URN take it as it is
50 if (location.find("urn:") == 0) {
51 urn_ = location;
52
53 // and clear URL members
54 scheme_ = "";
55 authority_ = "";
56 path_ = "";
57
58 } else { // it is an URL
59
60 // split URL in protocol, hostname and path
61 std::size_t pos = 0;
62 auto proto = location.find("://", pos);
63 if (proto != std::string::npos) { // extract the protocol
64
65 urn_ = ""; // clear URN-member if URL is parsed
66
67 scheme_ = location.substr(pos, proto - pos);
68 pos = 3 + proto; // 3 == "://"
69
70 auto authority = location.find("/", pos);
71 if (authority != std::string::npos) { // and the hostname (no proto without hostname)
72 authority_ = location.substr(pos, authority - pos);
73 pos = authority;
74 }
75 }
76
77 auto path = location.substr(pos);
78
79 // URNs cannot of have paths
80 if (urn_.size() && path.size())
81 throw std::invalid_argument("Cannot add a path (" + path + ") to an URN URI (" + urn_ + ")");
82
83 if (path[0] == '/') // if it starts with a / it is root-path
84 path_ = path;
85 else if (pos == 0) { // the URL contained only a path and the current path has no / at the end, strip last element until / and append
86 auto last_slash = path_.rfind('/');
87 path_ = path_.substr(0, last_slash) + '/' + path;
88 } else // otherwise it is a subfolder
89 path_.append(path);
90 }
91 }
92
93 pointer_ = ""_json_pointer;
94 identifier_ = "";
95
96 if (pointer[0] == '/')
97 pointer_ = json::json_pointer(pointer);
98 else
100}
const std::string & authority() const
const std::string & path() const
const json::json_pointer & pointer() const

References authority_, identifier_, path_, pointer_, scheme_, and urn_.

Referenced by derive(), and json_uri().

Here is the caller graph for this function:

◆ url()

std::string nlohmann::json_uri::url ( ) const
inline

Definition at line 88 of file json-schema.hpp.

88{ return location(); }

References location().

Referenced by loader(), loader(), and loader().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator<

bool operator< ( const json_uri & l,
const json_uri & r )
friend

Definition at line 115 of file json-schema.hpp.

116 {
117 return l.as_tuple() < r.as_tuple();
118 }
std::tuple< std::string, std::string, std::string, std::string, std::string > as_tuple() const

References as_tuple().

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const json_uri & u )
friend

Definition at line 132 of file json-uri.cpp.

133{
134 return os << u.to_string();
135}
std::string to_string() const
Definition json-uri.cpp:118

References to_string().

◆ operator==

bool operator== ( const json_uri & l,
const json_uri & r )
friend

Definition at line 120 of file json-schema.hpp.

121 {
122 return l.as_tuple() == r.as_tuple();
123 }

References as_tuple().

Member Data Documentation

◆ authority_

std::string nlohmann::json_uri::authority_
private

Definition at line 52 of file json-schema.hpp.

Referenced by as_tuple(), authority(), location(), and update().

◆ identifier_

std::string nlohmann::json_uri::identifier_
private

Definition at line 56 of file json-schema.hpp.

Referenced by as_tuple(), fragment(), identifier(), to_string(), and update().

◆ path_

std::string nlohmann::json_uri::path_
private

Definition at line 53 of file json-schema.hpp.

Referenced by as_tuple(), location(), path(), and update().

◆ pointer_

json::json_pointer nlohmann::json_uri::pointer_
private

Definition at line 55 of file json-schema.hpp.

Referenced by append(), as_tuple(), fragment(), pointer(), to_string(), and update().

◆ scheme_

std::string nlohmann::json_uri::scheme_
private

Definition at line 51 of file json-schema.hpp.

Referenced by as_tuple(), location(), scheme(), and update().

◆ urn_

std::string nlohmann::json_uri::urn_
private

Definition at line 49 of file json-schema.hpp.

Referenced by as_tuple(), location(), and update().


The documentation for this class was generated from the following files: