-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamjson.hpp
643 lines (547 loc) · 16.2 KB
/
streamjson.hpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright 2024 Pablo Garrido
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <string>
#include <string_view>
#include <cstring>
#include <array>
#include <vector>
#include <functional>
#include <ctre.hpp>
namespace streamjson
{
/**
* @class JSONValue
*
* @brief A simple class to hold a JSON value
*/
struct JSONValue
{
enum class Type
{
STRING,
FLOATING,
INTEGER,
BOOLEAN,
INVALID
};
Type type;
std::string string;
union
{
double floating;
int64_t integer;
bool boolean;
};
JSONValue(const char * string, size_t size)
{
bool success = parse_string(string, size);
success = success || parse_number(string, size);
success = success || parse_boolean(string, size);
if (!success)
{
type = Type::INVALID;
}
}
std::string to_string() const
{
switch (type)
{
case Type::STRING:
return string;
case Type::FLOATING:
return std::to_string(floating);
case Type::INTEGER:
return std::to_string(integer);
case Type::BOOLEAN:
return boolean ? "true" : "false";
default:
return "INVALID";
}
}
protected:
bool parse_string(const char * data, size_t size)
{
auto regex = ctre::search<"\"(.*)\"">(std::string_view(data, size + 1));
if (regex)
{
string = std::string(regex.get<1>().to_view());
type = Type::STRING;
return true;
}
return false;
}
bool parse_boolean(const char * data, size_t size)
{
auto regex = ctre::search<"true|false|True|False|TRUE|FALSE">(std::string_view(data, size + 1));
if (regex)
{
bool value = regex.to_view() == "true" || regex.to_view() == "True" || regex.to_view() == "TRUE";
boolean = value;
type = Type::BOOLEAN;
return true;
}
return false;
}
bool parse_number(const char * data, size_t size)
{
auto regex = ctre::search<"-?[0-9]+\\.?[0-9]*|-?[0-9]*\\.?[0-9]+">(std::string_view(data, size + 1));
if (regex)
{
std::string_view number = regex.to_view();
if (number.find('.') != std::string_view::npos)
{
floating = std::stod(std::string(number));
type = Type::FLOATING;
}
else
{
integer = std::stoll(std::string(number));
type = Type::INTEGER;
}
return true;
}
return false;
}
};
/**
* @class IJSONListener
*
* @brief Low level interface for JSON listeners
*/
struct IJSONListener
{
virtual void on_object_start() {};
virtual void on_object_end() {};
virtual void on_array_start() {};
virtual void on_array_end() {};
virtual void on_array_next_element() {};
virtual void on_key(const std::string_view & key) {};
virtual void on_value(const JSONValue & value) {};
};
/**
* @class JSONListener
*
* @brief A simple JSON listener that builds a aggregated key string
*/
struct JSONListener : public IJSONListener
{
void on_object_start() override {
aggregate_key_ += (aggregate_key_.size() ? "." : "");
if (key_.empty())
{
aggregate_key_ += "_";
}
else
{
aggregate_key_ += std::string(key_.data(), key_.size());
}
key_.clear();
};
void on_object_end() override {
size_t pos = aggregate_key_.rfind(".");
if (pos != std::string::npos)
{
aggregate_key_ = aggregate_key_.substr(0, pos);
}
else
{
aggregate_key_.clear();
}
};
void on_array_start() override {
array_depth_.push_back(0);
aggregate_key_ += (aggregate_key_.size() ? "." : "");
if (key_.empty())
{
aggregate_key_ += "_";
}
else
{
aggregate_key_ += std::string(key_.data(), key_.size());
}
aggregate_key_ += "[" + std::to_string(array_depth_.back()) + "]";
key_.clear();
};
void on_array_end() override {
array_depth_.pop_back();
size_t pos = aggregate_key_.rfind(".");
if (pos != std::string::npos)
{
aggregate_key_ = aggregate_key_.substr(0, pos);
}
else
{
aggregate_key_.clear();
}
};
void on_array_next_element() override {
array_depth_.back()++;
aggregate_key_ = aggregate_key_.substr(0, aggregate_key_.rfind("[")) + "[" + std::to_string(array_depth_.back()) + "]";
};
void on_key(const std::string_view& string) override {
key_ = std::string(string.data(), string.size());
};
void on_value(const JSONValue & /* value */) override {
if(!key_.empty())
{
// Got a key and a value
key_.clear();
}
};
protected:
std::string key_;
std::string aggregate_key_;
std::vector<size_t> array_depth_;
};
/**
* @class FilterListener
*
* @brief A JSON listener that filters keys based on a string and calls a callback on match
*/
template<CTRE_REGEX_INPUT_TYPE filter>
struct FilterListener : public JSONListener
{
using CallBackType = std::function<void(const std::string_view &, const JSONValue &, const std::vector<size_t>&)>;
FilterListener(CallBackType callback)
: callback_(callback)
{
}
void on_value(const JSONValue & value) override {
// Find and replace "_."
std::string query = aggregate_key_ + "." + key_;
size_t pos = query.find("_.");
while (pos != std::string::npos)
{
query.replace(pos, 2, "");
pos = query.find("_.");
}
if (ctre::match<filter>(std::string_view(query)))
{
callback_(query, value, array_depth_);
}
JSONListener::on_value(value);
}
protected:
// const std::string filter_;
CallBackType callback_;
};
/**
* @class MultiListener
*
* @brief A JSON listener that acts as a multiplexer
*/
class MultiListener : public IJSONListener
{
public:
MultiListener() = default;
MultiListener(std::initializer_list<IJSONListener *> listeners)
: listeners_(listeners)
{
}
void on_object_start() override
{
for (auto listener : listeners_)
{
listener->on_object_start();
}
};
void on_object_end() override
{
for (auto listener : listeners_)
{
listener->on_object_end();
}
};
void on_array_start() override
{
for (auto listener : listeners_)
{
listener->on_array_start();
}
};
void on_array_end() override
{
for (auto listener : listeners_)
{
listener->on_array_end();
}
};
void on_array_next_element() override
{
for (auto listener : listeners_)
{
listener->on_array_next_element();
}
};
void on_key(const std::string_view& key) override
{
for (auto listener : listeners_)
{
listener->on_key(key);
}
};
void on_value(const JSONValue& value) override
{
for (auto listener : listeners_)
{
listener->on_value(value);
}
};
void add_listener(IJSONListener & listener)
{
listeners_.push_back(&listener);
}
private:
std::vector<IJSONListener *> listeners_;
};
/**
* @class StreamJson
*
* @brief A JSON parser that can be fed with chunks of data
*/
class StreamJson {
public:
StreamJson()
: listener_(&dummy_listener_)
{
}
StreamJson(IJSONListener & listener)
: listener_(&listener)
{
}
size_t feed(const char * chunk, size_t size, size_t offset = 0)
{
if (offset != 0)
{
value_start_ = chunk;
value_size_ = offset - 1;
}
else if(after_colon_)
{
value_start_ = chunk;
value_size_ = 0;
}
for (size_t i = offset; i < size; i++)
{
const char & c = *(chunk + i);
Token token = get_token(c);
value_size_++;
// If we are processing a string, the only valid token is the quote
if (!state_stack_.empty() && state_stack_.back() == State::IN_STRING && token != Token::QUOTE)
{
continue;
}
switch (token)
{
case Token::QUOTE:
// If we are in a string, we are going to end it
if (state_stack_.back() == State::IN_STRING)
{
state_stack_.pop_back();
// If this string is after a colon, it is a value
if (after_colon_)
{
after_colon_ = false;
listener_->on_value(JSONValue(value_start_, value_size_));
}
else
{
listener_->on_key(std::string_view(value_start_ + 1, value_size_ - 1));
}
value_start_ = nullptr;
value_size_ = 0;
}
else
{
value_start_ = &c;
value_size_ = 0;
state_stack_.push_back(State::IN_STRING);
}
break;
case Token::OBJECT_START:
after_colon_ = false;
listener_->on_object_start();
state_stack_.push_back(State::IN_OBJECT);
break;
case Token::OBJECT_END:
if( after_colon_)
{
listener_->on_value(JSONValue(value_start_, value_size_ - 1));
}
after_colon_ = false;
if(state_stack_.back() == State::IN_OBJECT)
{
listener_->on_object_end();
state_stack_.pop_back();
}
value_start_ = nullptr;
value_size_ = 0;
break;
case Token::ARRAY_START:
after_colon_ = false;
value_start_ = &c + 1;
value_size_ = 0;
listener_->on_array_start();
state_stack_.push_back(State::IN_ARRAY);
break;
case Token::ARRAY_END:
after_colon_ = false;
if(value_start_ != nullptr)
{
listener_->on_value(JSONValue(value_start_, value_size_ - 1));
value_start_ = nullptr;
value_size_ = 0;
}
if(state_stack_.back() == State::IN_ARRAY)
{
listener_->on_array_end();
state_stack_.pop_back();
}
break;
case Token::COLON:
after_colon_ = true;
value_start_ = &c + 1;
value_size_ = 0;
break;
case Token::COMMA:
// Maybe we found a value
if( after_colon_)
{
listener_->on_value(JSONValue(value_start_, value_size_ - 1));
value_start_ = nullptr;
value_size_ = 0;
}
else if (state_stack_.back() == State::IN_ARRAY && value_start_ != nullptr)
{
listener_->on_value(JSONValue(value_start_, value_size_ - 1));
value_start_ = &c + 1;
value_size_ = 0;
}
if (state_stack_.back() == State::IN_ARRAY)
{
listener_->on_array_next_element();
}
after_colon_ = false;
break;
case Token::NONE:
break;
default:
break;
}
}
// Return the required point to keep
size_t allow_to_remove = value_start_ ? value_start_ - chunk : size;
return allow_to_remove;
}
virtual void reset(IJSONListener & listener)
{
listener_ = &listener;
state_stack_.clear();
after_colon_ = false;
value_start_ = nullptr;
value_size_ = 0;
}
protected:
enum class State : uint8_t
{
IN_OBJECT,
IN_ARRAY,
IN_STRING,
};
enum class Token
{
OBJECT_START,
OBJECT_END,
ARRAY_START,
ARRAY_END,
QUOTE,
COLON,
COMMA,
NONE
};
using TokenMapType = std::array<std::pair<Token, char>, static_cast<size_t>(Token::NONE)>;
const TokenMapType token_map_ = {
std::make_pair(Token::OBJECT_START, '{'),
std::make_pair(Token::OBJECT_END, '}'),
std::make_pair(Token::ARRAY_START, '['),
std::make_pair(Token::ARRAY_END, ']'),
std::make_pair(Token::QUOTE, '"'),
std::make_pair(Token::COLON, ':'),
std::make_pair(Token::COMMA, ',')
};
Token get_token(const char c)
{
for (const auto & pair : token_map_)
{
if (pair.second == c)
{
return pair.first;
}
}
return Token::NONE;
}
IJSONListener * listener_;
IJSONListener dummy_listener_;
// State variables
std::vector<State> state_stack_;
bool after_colon_ = false;
char const * value_start_ = nullptr;
size_t value_size_ = 0;
};
/**
* @class AutofeedStreamJson
*
* @brief A JSON parser that can be fed with chunks of data and automatically calls the feed method
*/
template<size_t CHUNK_SIZE>
class AutofeedStreamJson : public StreamJson
{
public:
AutofeedStreamJson()
: StreamJson()
{
}
AutofeedStreamJson(IJSONListener & listener)
: StreamJson(listener)
{
}
void feed(const char * chunk, size_t size)
{
if(!failed_)
{
// Copy new data to the buffer
memcpy(buffer_.data() + next_offset_, chunk, size);
// Actual size of the buffer
size += next_offset_;
// Feed the buffer
size_t to_remove = StreamJson::feed(buffer_.data(), size, next_offset_);
// Remove the processed data
memmove(buffer_.data(), buffer_.data() + to_remove, size - to_remove + 1);
next_offset_ = size - to_remove;
if (next_offset_ >= CHUNK_SIZE)
{
failed_ = true;
}
}
};
void reset(IJSONListener & listener)
{
StreamJson::reset(listener);
next_offset_ = 0;
failed_ = false;
}
protected:
std::array<char, CHUNK_SIZE> buffer_;
size_t next_offset_ = 0;
bool failed_ = false;
};
} // namespace streamjson