-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
139 lines (110 loc) · 2.85 KB
/
main.cpp
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
#include <string.h> // strcmp
#include <fstream>
#include <iostream>
#include <cassert>
#include <chrono>
#include <sstream>
#include "anyjson.hpp"
#include "anyjson_tools.hpp"
#if defined(WITH_CHRONO)
class Chrono {
std::chrono::time_point<std::chrono::steady_clock> const m_start;
std::string const m_text;
public:
Chrono(const std::string& l_text)
: m_start(std::chrono::steady_clock::now())
, m_text(l_text)
{
}
~Chrono()
{
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - m_start;
std::cout << "Elapsed time for " << m_text << " = " << elapsed_seconds.count() << "s\n";
}
};
#endif
int test_input_file(const char* filePath) {
int retcode = 0;
std::fstream ifs(filePath);
Chrono* chrono = nullptr;
if (ifs) {
#if defined(WITH_CHRONO)
chrono = new Chrono("parsing");
#endif
anyjson::Value&& data = anyjson::parse(ifs);
#if defined(WITH_CHRONO)
delete chrono;
#endif
if (data.has_value()) {
std::stringstream oss;
#if defined(WITH_CHRONO)
chrono = new Chrono("stringifying");
#endif
retcode += (anyjson::stringify(data, oss)) ? 0 : 1;
#if defined(WITH_CHRONO)
delete chrono;
#endif
if (retcode == 0) {
//std::cout << oss.str() << std::endl;
}
}
} else {
std::cerr << "cant open file: " << filePath << std::endl;
retcode = 4;
}
return retcode;
}
int test_simple_json() {
int retcode = 0;
std::stringstream iss("{ \"item\": { \"value\": 0 } }");
anyjson::Value&& data = anyjson::parse(iss);
if (data.has_value()) {
try {
anyjson::Object& root = to<anyjson::Object&>(data);
anyjson::Object::iterator rootIt = root.find("item");
if (rootIt != root.end()) {
anyjson::Object& item = to<anyjson::Object&>(rootIt->second);
anyjson::Object::iterator itemIt = item.find("value");
if (itemIt != item.end()) {
anyjson::Number& value = to<anyjson::Number&>(itemIt->second);
value = 42;
}
}
retcode += anyjson::stringify(data, std::cout);
std::cout << std::endl;
} catch (const std::out_of_range& oof) {
std::cerr << "key not mapped : " << oof.what() << std::endl;
retcode = 2;
} catch (const std::bad_any_cast&) {
std::cerr << "bad any cast" << std::endl;
retcode = 4;
} catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
retcode = 8;
}
} else {
std::cerr << "parse failed" << std::endl;
retcode = 1;
}
if (retcode > 0) {
retcode += 64;
}
return retcode;
}
int main(int argc, char* argv[])
{
int retcode = 0;
if (argc > 1) {
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
std::cerr << "anyjson parser" << std::endl;
std::cerr << "\tusage: " << argv[0] << " [json file]?" << std::endl;
retcode = 128;
} else {
retcode = test_input_file(argv[1]);
}
} else {
retcode = test_simple_json();
}
return retcode;
}