aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/JSONHandler.cc
blob: 909aa7db4ee4c870d8377ae03214b01728875647 (plain) (blame)
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
#include <qpdf/JSONHandler.hh>

#include <qpdf/QPDFUsage.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>

JSONHandler::JSONHandler() :
    m(new Members())
{
}

void
JSONHandler::usage(std::string const& msg)
{
    throw QPDFUsage(msg);
}

void
JSONHandler::addAnyHandler(json_handler_t fn)
{
    this->m->h.any_handler = fn;
}

void
JSONHandler::addNullHandler(void_handler_t fn)
{
    this->m->h.null_handler = fn;
}

void
JSONHandler::addStringHandler(string_handler_t fn)
{
    this->m->h.string_handler = fn;
}

void
JSONHandler::addNumberHandler(string_handler_t fn)
{
    this->m->h.number_handler = fn;
}

void
JSONHandler::addBoolHandler(bool_handler_t fn)
{
    this->m->h.bool_handler = fn;
}

void
JSONHandler::addDictHandlers(json_handler_t start_fn, void_handler_t end_fn)
{
    this->m->h.dict_start_handler = start_fn;
    this->m->h.dict_end_handler = end_fn;
}

void
JSONHandler::addDictKeyHandler(
    std::string const& key, std::shared_ptr<JSONHandler> dkh)
{
    this->m->h.dict_handlers[key] = dkh;
}

void
JSONHandler::addFallbackDictHandler(std::shared_ptr<JSONHandler> fdh)
{
    this->m->h.fallback_dict_handler = fdh;
}

void
JSONHandler::addArrayHandlers(
    json_handler_t start_fn,
    void_handler_t end_fn,
    std::shared_ptr<JSONHandler> ah)
{
    this->m->h.array_start_handler = start_fn;
    this->m->h.array_end_handler = end_fn;
    this->m->h.array_item_handler = ah;
}

void
JSONHandler::handle(std::string const& path, JSON j)
{
    if (this->m->h.any_handler) {
        this->m->h.any_handler(path, j);
        return;
    }
    bool handled = false;
    bool bvalue = false;
    std::string s_value;
    if (this->m->h.null_handler && j.isNull()) {
        this->m->h.null_handler(path);
        handled = true;
    }
    if (this->m->h.string_handler && j.getString(s_value)) {
        this->m->h.string_handler(path, s_value);
        handled = true;
    }
    if (this->m->h.number_handler && j.getNumber(s_value)) {
        this->m->h.number_handler(path, s_value);
        handled = true;
    }
    if (this->m->h.bool_handler && j.getBool(bvalue)) {
        this->m->h.bool_handler(path, bvalue);
        handled = true;
    }
    if (this->m->h.dict_start_handler && j.isDictionary()) {
        this->m->h.dict_start_handler(path, j);
        std::string path_base = path;
        if (path_base != ".") {
            path_base += ".";
        }
        j.forEachDictItem([&path, &path_base, this](
                              std::string const& k, JSON v) {
            auto i = this->m->h.dict_handlers.find(k);
            if (i == this->m->h.dict_handlers.end()) {
                if (this->m->h.fallback_dict_handler.get()) {
                    this->m->h.fallback_dict_handler->handle(path_base + k, v);
                } else {
                    QTC::TC("libtests", "JSONHandler unexpected key");
                    usage(
                        "JSON handler found unexpected key " + k +
                        " in object at " + path);
                }
            } else {
                i->second->handle(path_base + k, v);
            }
        });
        this->m->h.dict_end_handler(path);
        handled = true;
    }
    if (this->m->h.array_start_handler && j.isArray()) {
        this->m->h.array_start_handler(path, j);
        size_t i = 0;
        j.forEachArrayItem([&i, &path, this](JSON v) {
            this->m->h.array_item_handler->handle(
                path + "[" + QUtil::uint_to_string(i) + "]", v);
            ++i;
        });
        this->m->h.array_end_handler(path);
        handled = true;
    }

    if (!handled) {
        // It would be nice to include information about what type the
        // object was and what types were allowed, but we're relying
        // on schema validation to make sure input is properly
        // structured before calling the handlers. It would be
        // different if this code were trying to be part of a
        // general-purpose JSON package.
        QTC::TC("libtests", "JSONHandler unhandled value");
        usage("JSON handler: value at " + path + " is not of expected type");
    }
}