JSON schema & decode object #300
Replies: 4 comments 1 reply
-
The types What compiler are you using? Does it support |
Beta Was this translation helpful? Give feedback.
-
If you can use C++17, and #include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <jsoncons_ext/jsonpatch/jsonpatch.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <variant>
using json = jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;
namespace jsonpatch = jsoncons::jsonpatch;
struct os_properties {
std::string command;
};
struct db_properties {
std::string query;
};
struct api_properties {
std::string target;
};
struct job_properties {
std::string name;
std::variant<os_properties,db_properties,api_properties> run;
};
JSONCONS_N_MEMBER_TRAITS(os_properties, 1, command)
JSONCONS_N_MEMBER_TRAITS(db_properties, 1, query)
JSONCONS_N_MEMBER_TRAITS(api_properties, 1, target)
JSONCONS_N_MEMBER_TRAITS(job_properties, 2, name, run)
std::string test_schema = R"(
{
"title": "job",
"description": "job properties json schema",
"definitions": {
"os_properties": {
"type": "object",
"properties": {
"command": {
"description": "this is the OS command to run",
"type": "string",
"minLength": 1
}
},
"required": [ "command" ],
"additionalProperties": false
},
"db_properties": {
"type": "object",
"properties": {
"query": {
"description": "this is db query to run",
"type": "string",
"minLength": 1
}
},
"required": [ "query" ],
"additionalProperties": false
},
"api_properties": {
"type": "object",
"properties": {
"target": {
"description": "this is api target to run",
"type": "string",
"minLength": 1
}
},
"required": [ "target" ],
"additionalProperties": false
}
},
"type": "object",
"properties": {
"name": {
"description": "name of the flow",
"type": "string",
"minLength": 1
},
"run": {
"description": "job run properties",
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/os_properties" },
{ "$ref": "#/definitions/db_properties" },
{ "$ref": "#/definitions/api_properties" }
]
}
},
"required": [ "name", "run" ],
"additionalProperties": false
}
)";
std::string test_json = R"(
{
"name": "testing flow",
"run" : {
"command": "some command"
}
}
)";
int main() {
try
{
json schema = json::parse(test_schema);
json data = json::parse(test_json);
// Will throw schema_error if JSON Schema loading fails
auto sch = jsonschema::make_schema(schema);
std::size_t error_count = 0;
auto reporter = [&error_count](const jsonschema::validation_output& o)
{
++error_count;
std::cout << o.instance_location() << ": " << o.message() << "\n";
};
jsonschema::json_validator<json> validator(sch);
// Will call reporter for each schema violation
json patch = validator.validate(data, reporter);
//jsonpatch::apply_patch(patch, data);
std::cout << "\nError count: " << error_count << "\n\n";
//const job_properties v = jsoncons::decode_json<job_properties>(test_json);
const job_properties v = data.as<job_properties>(); // You don't need to reparse test_json
std::string en;
jsoncons::encode_json_pretty(v, en);
std::cout << "Given json is: " << en << std::endl;
// Test that it validates
json test = json::parse(en);
validator.validate(test, reporter);
std::cout << "\nError count: " << error_count << "\n\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << '\n';
}
return 0;
} Output:
|
Beta Was this translation helpful? Give feedback.
-
@danielaparker Thank you very much for your help. I have been targeting the code with C++11 and will take your suggestions If I switch to C++17. |
Beta Was this translation helpful? Give feedback.
-
With C++11, you can use struct run_properties {
jsoncons::optional<std::string> command;
jsoncons::optional<std::string> query;
jsoncons::optional<std::string> target;
};
struct job_properties {
std::string name;
run_properties run;
};
JSONCONS_N_MEMBER_TRAITS(run_properties, 0, command, query, target)
JSONCONS_N_MEMBER_TRAITS(job_properties, 2, name, run) But that corresponds more to "anyOf" than "oneOf". |
Beta Was this translation helpful? Give feedback.
-
Hi Team,
I am playing with the latest JSON schema extension, and it is working as per the json schema drafts.
But, I am having trouble to decode the valid JSON into the object type.
For example, consider the below situation.
I have defined one of the properties of the json schema as like below.
And, defined the object to hold one of the properties as like below.
And defined the traits for the run_properties as like below.
JSONCONS_N_MEMBER_TRAITS(run_properties, 0, os_properties, db_properties, api_properties)
I am passing the above JSON which is schema qualified, but, I am unable to get the "run" object property into the "os_properties" object. I am also not getting any errors, and the os_properties object don't have any value also.
And, here is the os properties object definition.
Beta Was this translation helpful? Give feedback.
All reactions