Replies: 2 comments 3 replies
-
@roszakg, Apologies for the late response, just returned from vacation. The information that you need is in the |
Beta Was this translation helpful? Give feedback.
-
I've added a #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_string = 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_string);
jsonschema::json_schema<ojson> compiled = jsonschema::make_json_schema(std::move(schema));
std::string data_string = R"(
{
"fruits": [
"apple",
"orange",
"pear"
],
"vegetables": [
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
)";
// Data
ojson data = ojson::parse(data_string);
auto reporter = [](const std::string& keyword,
const ojson& subschema,
const jsoncons::uri& /*schema_location*/,
const ojson& /*instance*/,
const jsoncons::jsonpointer::json_pointer& instance_location) -> jsonschema::walk_result
{
if (keyword == "type")
{
assert(subschema.is_object());
auto it = subschema.find("type");
if (it != subschema.object_range().end())
{
std::cout << instance_location.string() << ": " << it->value() << "\n";
}
}
return jsonschema::walk_result::advance;
};
compiled.walk(data, reporter);
} Output:
The types shown are the allowed types as specified in the schema. No validation is performed in the We now store the original schema document(s) in the This is still a work in progress, need to add a lot of tests. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm trying to work out how to construct the type tree based on the given JSON Schema.
To give you an example let's look at the following schema from json-schema.org:
combined with the following JSON data:
What I'm trying to do is to dump the tree like structure (based just on schema itself and not the data) that describes a type of the particular field (or multiple types in a general case).
In the example above it would yield something like:
/ - [object]
"fruits" - [array]
[0] - [string]
"vegetables" - [array]
[0] - [object]
"veggieName" - string
"veggieLike" - boolean
What I need that for is to facilitate the conversion from data structure that does not convey any type information to JSON.
Is there a way to use jsoncons to facilitate such a schema guided conversion?
Beta Was this translation helpful? Give feedback.
All reactions