1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
### jsoncons::jsonschema::json_schema<Json>::walk
```cpp
template <typename WalkReporter>
void walk(const Json& instance, const WalkReporter& reporter) const; (since 0.175.0)
```
Walks through a JSON schema to collect information.
#### Parameters
<table>
<tr>
<td>instance</td>
<td>Input Json</td>
</tr>
<tr>
<td>reporter</td>
<td>A function object with signature equivalent to
<pre>
walk_result fun(const std::string& keyword,
const Json& schema, const uri& schema_location,
const Json& instance, const jsonpointer::json_pointer& instance_location)</pre>
that returns a <a href="../walk_result.md">walk_result</a> to indicate whether to continue or stop.
</td>
</tr>
</table>
#### Return value
None
#### Exceptions
None
### Examples
#### Construct a type tree based on a JSON Schema and an instance
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>
#include <cassert>
using jsoncons::ojson;
namespace jsonschema = jsoncons::jsonschema;
int main()
{
std::string schema_str = R"(
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": {
"$ref": "#/$defs/veggie"
}
}
},
"$defs": {
"veggie": {
"type": "object",
"required": [
"veggieName",
"veggieLike"
],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
)";
ojson schema = ojson::parse(schema_str);
jsonschema::json_schema<ojson> compiled = jsonschema::make_json_schema(std::move(schema));
std::string data_str = R"(
{
"fruits": [
"apple",
"orange",
"pear"
],
"vegetables": [
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
)";
// Data
ojson data = ojson::parse(data_str);
auto reporter = [](const std::string& keyword,
const ojson& schema,
const jsoncons::uri& /*schema_location*/,
const ojson& /*instance*/,
const jsoncons::jsonpointer::json_pointer& instance_location) -> jsonschema::walk_result
{
if (keyword == "type")
{
assert(schema.is_object());
auto it = schema.find("type");
if (it != schema.object_range().end())
{
std::cout << instance_location.string() << ": " << it->value() << "\n";
}
}
return jsonschema::walk_result::advance;
};
compiled.walk(data, reporter);
}
```
Output:
```
/fruits/0: "string"
/fruits/1: "string"
/fruits/2: "string"
/fruits: "array"
/vegetables/0/veggieName: "string"
/vegetables/0/veggieLike: "boolean"
/vegetables/0: "object"
/vegetables/1/veggieName: "string"
/vegetables/1/veggieLike: "boolean"
/vegetables/1: "object"
/vegetables: "array"
: "object"
```
The type tree shows the allowable types for the data values as specifed in the schema.
No validation of the data is performed during its construction.
|