blob: 721b17de891bdbf6a611c666a0cdbfa358c5a25e (
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
|
#include <qpdf/FileInputSource.hh>
#include <qpdf/JSON.hh>
#include <qpdf/QUtil.hh>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
namespace
{
class Reactor: public JSON::Reactor
{
public:
virtual ~Reactor() = default;
virtual void dictionaryStart() override;
virtual void arrayStart() override;
virtual void containerEnd(JSON const& value) override;
virtual void topLevelScalar() override;
virtual bool
dictionaryItem(std::string const& key, JSON const& value) override;
virtual bool arrayItem(JSON const& value) override;
private:
void printItem(JSON const&);
};
} // namespace
void
Reactor::dictionaryStart()
{
std::cout << "dictionary start" << std::endl;
}
void
Reactor::arrayStart()
{
std::cout << "array start" << std::endl;
}
void
Reactor::containerEnd(JSON const& value)
{
std::cout << "container end: ";
printItem(value);
}
void
Reactor::topLevelScalar()
{
std::cout << "top-level scalar" << std::endl;
}
bool
Reactor::dictionaryItem(std::string const& key, JSON const& value)
{
std::cout << "dictionary item: " << key << " -> ";
printItem(value);
if (key == "keep") {
return false;
}
return true;
}
bool
Reactor::arrayItem(JSON const& value)
{
std::cout << "array item: ";
printItem(value);
std::string n;
if (value.getString(n) && n == "keep") {
return false;
}
return true;
}
void
Reactor::printItem(JSON const& j)
{
std::cout << "[" << j.getStart() << ", " << j.getEnd()
<< "): " << j.unparse() << std::endl;
}
static void
usage()
{
std::cerr << "Usage: json_parse file [--react]" << std::endl;
exit(2);
}
int
main(int argc, char* argv[])
{
if ((argc < 2) || (argc > 3)) {
usage();
return 2;
}
char const* filename = argv[1];
std::shared_ptr<Reactor> reactor;
if (argc == 3) {
if (strcmp(argv[2], "--react") == 0) {
reactor = std::make_shared<Reactor>();
} else {
usage();
}
}
try {
FileInputSource is(filename);
std::cout << JSON::parse(is, reactor.get()).unparse() << std::endl;
} catch (std::exception& e) {
std::cerr << "exception: " << filename << ": " << e.what() << std::endl;
return 2;
}
return 0;
}
|