MQTTSuite
Loading...
Searching...
No Matches
readme.cpp File Reference
#include <iomanip>
#include <iostream>
#include <nlohmann/json-schema.hpp>
Include dependency graph for readme.cpp:

Go to the source code of this file.

Functions

int main ()

Variables

static json person_schema
static json bad_person = {{"age", 42}}
static json good_person = {{"name", "Albert"}, {"age", 42}, {"address", {{"street", "Main Street"}}}}
static json good_defaulted_person = {{"name", "Knut"}, {"age", 69}, {"address", {}}}

Function Documentation

◆ main()

int main ( void )

Definition at line 49 of file readme.cpp.

50{
51 /* json-parse the schema */
52
53 json_validator validator; // create validator
54
55 try {
56 validator.set_root_schema(person_schema); // insert root-schema
57 } catch (const std::exception &e) {
58 std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
59 return EXIT_FAILURE;
60 }
61
62 /* json-parse the people - API of 1.0.0, default throwing error handler */
63
64 for (auto &person : {bad_person, good_person, good_defaulted_person}) {
65 std::cout << "About to validate this person:\n"
66 << std::setw(2) << person << std::endl;
67 try {
68 auto defaultPatch = validator.validate(person); // validate the document - uses the default throwing error-handler
69 std::cout << "Validation succeeded\n";
70 std::cout << "Patch with defaults: " << defaultPatch.dump(2) << std::endl;
71 } catch (const std::exception &e) {
72 std::cerr << "Validation failed, here is why: " << e.what() << "\n";
73 }
74 }
75
76 /* json-parse the people - with custom error handler */
78 {
79 void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
80 {
82 std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
83 }
84 };
85
86 for (auto &person : {bad_person, good_person}) {
87 std::cout << "About to validate this person:\n"
88 << std::setw(2) << person << std::endl;
89
91 validator.validate(person, err); // validate the document
92
93 if (err)
94 std::cerr << "Validation failed\n";
95 else
96 std::cout << "Validation succeeded\n";
97 }
98
99 return EXIT_SUCCESS;
100}
nlohmann::json_schema::json_validator validator
nlohmann::json json
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
void error(const json::json_pointer &, const json &, const std::string &) override
static const auto instance
Definition issue-93.cpp:14
static json person_schema
Definition readme.cpp:10
static json good_defaulted_person
Definition readme.cpp:47
static json bad_person
Definition readme.cpp:45
static json good_person
Definition readme.cpp:46

References bad_person, nlohmann::json_schema::basic_error_handler::error(), good_defaulted_person, good_person, person_schema, nlohmann::json_schema::json_validator::set_root_schema(), nlohmann::json_schema::json_validator::validate(), and nlohmann::json_schema::json_validator::validate().

Here is the call graph for this function:

Variable Documentation

◆ bad_person

json bad_person = {{"age", 42}}
static

Definition at line 45 of file readme.cpp.

45{{"age", 42}};

Referenced by main().

◆ good_defaulted_person

json good_defaulted_person = {{"name", "Knut"}, {"age", 69}, {"address", {}}}
static

Definition at line 47 of file readme.cpp.

47{{"name", "Knut"}, {"age", 69}, {"address", {}}};

Referenced by main().

◆ good_person

json good_person = {{"name", "Albert"}, {"age", 42}, {"address", {{"street", "Main Street"}}}}
static

Definition at line 46 of file readme.cpp.

46{{"name", "Albert"}, {"age", 42}, {"address", {{"street", "Main Street"}}}};

Referenced by main().

◆ person_schema

json person_schema
static

Definition at line 10 of file readme.cpp.

Referenced by main().