-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_impl.cpp
166 lines (152 loc) · 4.76 KB
/
parse_impl.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <minjsoncpp.h>
#include <iostream>
#include <iterator>
using namespace std::string_view_literals;
// auxiliary stuff defined below
std::string readStdin();
void print(const minjson::Value &value);
void printIssues(std::string_view input, const minjson::ParsingResult::Issues &issues);
minjson::Array parseNDJson(std::string_view input) {
minjson::Array values;
while (!input.empty()) {
// parsing JSON
// 'minjson::impl::parse()' ends parsing at the end of a valid JSON value and never parses beyond that
auto [value, status, parsedSize, issues] = minjson::impl::parse(input, {}, std::allocator<char>{});
// 'status' tells if the parsing succeeded
// 'parsedSize' tells how many bytes are successfully parsed from the input
// 'issues' contain information about different issues and errors encountered during parsing
if (status == minjson::ParsingResultStatus::Failure) {
std::cerr << "failed to parse line " << values.size();
printIssues(input, issues);
std::cerr << '\n';
break;
}
// storing successfuly parsed JSON value
values.push_back(std::move(value));
// skipping '\r' before '\n' if there is one
const auto n = input.find_first_not_of('\r', parsedSize);
if (n == std::string_view::npos)
break; // reached end of input
if (input[n] != '\n') {
std::cerr << "unexpected character\n";
break; // not NDJSON
}
input.remove_prefix(n + 1);
}
return values;
}
int main(int argc, const char *argv[]) {
if (argc <= 1) {
const auto string = R"({"decimal": 3.14}
{"null": null}
{"boolean": true}
{"integer": 42}
{"string": "hello there"}
{"array": [ 1, 2, 3 ]}
{"object": { "nested number": 23, "nested string": "General Kenobi", "nested array": [], "nested object": {} } })"sv;
// parsing newline delimeted JSON values
const auto values = parseNDJson(string);
if (!values.empty()) {
std::cout << "parsed JSON values:";
for (const auto &value : values) {
std::cout << "\n\n";
print(value);
}
std::cout << '\n';
}
return 0;
}
std::string_view input;
// read stdin if '-i' switch is provided in the command line
std::string cin;
if (argv[1] == "-i"sv) {
cin = readStdin();
input = cin; // using input from stdin to parse
} else {
input = argv[1]; // using command line argument to parse
}
// parsing newline delimeted JSON values
const auto values = parseNDJson(input);
if (values.empty()) {
std::cout << "input is empty\n";
} else {
std::cout << "parsed JSON values:";
for (const auto &value : values) {
std::cout << "\n\n";
print(value);
}
std::cout << '\n';
}
}
// auxiliary stuff
std::string readStdin() {
std::cin >> std::noskipws;
return std::string{ std::istream_iterator<char>{ std::cin }, std::istream_iterator<char>{} };
}
void print(const minjson::Value &value) {
minjson::SerializationOptions options;
options.indent = 2;
options.objectKeyValueSeparator = ": "sv;
minjson::serializeToStream(std::cout, value, options);
}
size_t printChar(char c) {
constexpr auto hexDigits = "0123456789ABCDEF"sv;
if ('\0' <= c && c < '\x20') {
std::cerr <<
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
"\x1b[4m"
#endif
"\\x" << hexDigits[static_cast<uint8_t>(c) >> 4]
<< hexDigits[c & '\xf'];
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
std::cerr << "\x1B[0m";
#endif
return 4;
} else {
std::cerr << c;
return 1;
}
}
size_t printString(std::string_view s) {
size_t size = 0;
for (char c : s) {
size += printChar(c);
}
return size;
}
void printIssues(std::string_view input, const minjson::ParsingResult::Issues &issues) {
if (issues.empty())
return;
std::cerr << "\n*** issues: ***";
for (const auto &i : issues) {
std::cerr << "\n " << i.description;
// also 'i.code' can be used to distinguish issues and errors
if (i.offset != input.size())
std::cerr << " @ " << i.offset;
std::cerr << "\n ";
const size_t start = std::max(i.offset, size_t{ 9 }) - 9;
if (start > 0)
std::cerr << "...";
size_t offset = printString(input.substr(start, i.offset - start));
const size_t count = std::min(i.offset + 9, input.size()) - i.offset;
if (i.offset != input.size()) {
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
std::cerr << "\x1b[91m";
#endif
printChar(input[i.offset]);
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
std::cerr << "\x1B[0m";
#endif
printString(input.substr(i.offset + 1, count - 1));
if (i.offset + count < input.size())
std::cerr << "...";
}
std::cerr << '\n';
for (; offset; --offset)
std::cerr << ' ';
if (start > 0)
std::cerr << " ";
std::cerr << " ^";
}
std::cerr << "\n***************\n";
}