File: boost_json_example.cpp

package info (click to toggle)
valijson 1.0.3%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,756 kB
  • sloc: cpp: 19,769; sh: 134; makefile: 24
file content (107 lines) | stat: -rw-r--r-- 2,948 bytes parent folder | download
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
#include <boost/json/src.hpp>
#include <valijson/adapters/boost_json_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validator.hpp>
#include <valijson/validation_results.hpp>
#include <iostream>

using namespace std::string_literals;

namespace json = boost::json;

const auto schemaJson = R"({
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "id": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },
    "name": {
      "description": "Name of the product",
      "type": "string"
    },
    "price": {
      "type": "number",
      "minimum": 0
    },
    "tags": {
      "type": "array",
      "items": { "type": "string" },
      "minItems": 1,
      "uniqueItems": true
    }
  },
  "required": ["id", "name", "price" ]
})"s;

const auto targetJson = R"({
  "id": 123,
  "name": "Test"
})"s;

int main()
{
    boost::system::error_code ec;
    auto schemaDoc = json::parse(schemaJson, ec);
    if (ec) {
        std::cerr << "Error parsing schema json: " << ec.message() << std::endl;
        return 1;
    }

    auto obj = schemaDoc.as_object();
    auto iter = obj.find("$schema");
    if (iter == obj.cend()) {
        std::cerr << "Error finding key $schema" << std::endl;
        return 2;
    }

    iter = obj.find("$ref");
    if (iter != obj.cend()) {
        std::cerr << "Invalid iterator for non-existent key $ref" << std::endl;
        return 3;
    }

    valijson::Schema schema;
    valijson::SchemaParser schemaParser;

    // Won't compile because the necessary constructor has been deleted
    // valijson::adapters::BoostJsonAdapter schemaAdapter(obj);

    valijson::adapters::BoostJsonAdapter schemaAdapter(schemaDoc);
    std::cerr << "Populating schema..." << std::endl;
    schemaParser.populateSchema(schemaAdapter, schema);

    auto targetDoc = json::parse(targetJson, ec);
    if (ec) {
        std::cerr << "Error parsing target json: " << ec.message() << std::endl;
        return 1;
    }

    valijson::Validator validator;
    valijson::ValidationResults results;
    valijson::adapters::BoostJsonAdapter targetAdapter(targetDoc);
    if (validator.validate(schema, targetAdapter, &results)) {
        std::cerr << "Validation succeeded." << std::endl;
        return 0;
    }

    std::cerr << "Validation failed." << std::endl;
    valijson::ValidationResults::Error error;
    unsigned int errorNum = 1;
    while (results.popError(error)) {
        std::cerr << "Error #" << errorNum << std::endl;
        std::cerr << "  ";
        for (const std::string &contextElement : error.context) {
            std::cerr << contextElement << " ";
        }
        std::cerr << std::endl;
        std::cerr << "    - " << error.description << std::endl;
        ++errorNum;
    }

    return 1;
}