MQTTSuite
Loading...
Searching...
No Matches
id-ref.cpp
Go to the documentation of this file.
1#include <nlohmann/json.hpp>
2
3#include <nlohmann/json-schema.hpp>
4
5#include <iostream>
6
7using nlohmann::json;
8
9auto schema_draft = R"(
10 {
11 "$id": "http://example.com/root.json",
12 "definitions": {
13 "A": { "$id": "#foo" },
14 "B": {
15 "$id": "other.json",
16 "definitions": {
17 "X": { "$id": "#bar" },
18 "Y": { "$id": "t/inner.json" }
19 }
20 },
21 "C": {
22
23 "$id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f",
24 "definitions": {
25 "Z": { "$id": "#bar" },
26 "9": { "$id": "http://example.com/drole.json" }
27 }
28 }
29 }
30 }
31)"_json;
32
33/* # (document root)
34
35 http://example.com/root.json
36 http://example.com/root.json#
37
38 #/definitions/A
39
40 http://example.com/root.json#foo
41 http://example.com/root.json#/definitions/A
42
43 #/definitions/B
44
45 http://example.com/other.json
46 http://example.com/other.json#
47 http://example.com/root.json#/definitions/B
48
49 #/definitions/B/definitions/X
50
51 http://example.com/other.json#bar
52 http://example.com/other.json#/definitions/X
53 http://example.com/root.json#/definitions/B/definitions/X
54
55 #/definitions/B/definitions/Y
56
57 http://example.com/t/inner.json
58 http://example.com/t/inner.json#
59 http://example.com/other.json#/definitions/Y
60 http://example.com/root.json#/definitions/B/definitions/Y
61
62 #/definitions/C
63
64 urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f
65 urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f#
66 http://example.com/root.json#/definitions/C
67 */
68
69auto schema = R"(
70{
71 "id": "http://localhost:1234/scope_change_defs2.json",
72 "type" : "object",
73 "properties": {
74 "list": {"$ref": "#/definitions/baz/definitions/bar"}
75 },
76 "definitions": {
77 "baz": {
78 "id": "folder/",
79 "definitions": {
80 "bar": {
81 "type": "array",
82 "items": {"$ref": "folderInteger.json"}
83 }
84 }
85 }
86 }
87})"_json;
88
90{
91public:
92 std::vector<json> schemas_;
93 std::map<nlohmann::json_uri, const json *> schema_store_;
94
95public:
96 void insert_schema(json &s, std::vector<nlohmann::json_uri> base_uris)
97 {
98 auto id = s.find("$id");
99 if (id != s.end())
100 base_uris.push_back(base_uris.back().derive(id.value()));
101
102 for (auto &u : base_uris)
103 schema_store_[u] = &s;
104
105 for (auto i = s.begin();
106 i != s.end();
107 ++i) {
108
109 switch (i.value().type()) {
110 case json::value_t::object: { // child is object, thus a schema
111 std::vector<nlohmann::json_uri> subschema_uri = base_uris;
112
113 for (auto &ss : subschema_uri)
115
116 insert_schema(i.value(), subschema_uri);
117 } break;
118
119 case json::value_t::string:
120 // this schema is a reference
121 if (i.key() == "$ref") {
122 auto id = base_uris.back().derive(i.value());
123 i.value() = id.to_string();
124 }
125
126 break;
127
128 default:
129 break;
130 }
131 }
132 }
133};
134
135int main(void)
136{
138
140
141 for (auto &i : store.schema_store_) {
142 std::cerr << i.first << " " << *i.second << "\n";
143 }
144
145 return 0;
146}
std::map< nlohmann::json_uri, const json * > schema_store_
Definition id-ref.cpp:93
std::vector< json > schemas_
Definition id-ref.cpp:92
void insert_schema(json &s, std::vector< nlohmann::json_uri > base_uris)
Definition id-ref.cpp:96
std::string to_string() const
Definition json-uri.cpp:118
static std::string escape(const std::string &)
Definition json-uri.cpp:137
json_uri derive(const std::string &uri) const
json_uri append(const std::string &field) const
int main()
Definition format.cpp:34
auto schema_draft
Definition id-ref.cpp:9
auto schema
Definition id-ref.cpp:69