-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimdjsonparser.h
147 lines (125 loc) · 4.61 KB
/
simdjsonparser.h
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
#ifndef SIMDJSONPARSER_H
#define SIMDJSONPARSER_H
#include <stack>
#include <string>
#include <unordered_map>
#include "parser.h"
#include "simdjson.h"
#include "simdjson.cpp"
namespace fuzzyjson {
class SimdjsonTraverser : public Traverser {
struct ContainerInfos {
ValueType type;
bool is_empty;
};
public:
// The SimdjsonTraverser will own the ParsedJson
SimdjsonTraverser(std::string parser_name, ParsingState parsing_state, simdjson::ParsedJson* parsed_json)
: Traverser(parser_name, parsing_state)
, parsed_json(parsed_json)
, iterator(*parsed_json)
{
current_type = simdjsontype_to_fuzzytype(iterator.get_type());
}
~SimdjsonTraverser() override { delete parsed_json; }
ValueType next() override {
ValueType previous_type = current_type;
if (previous_type == ValueType::array || previous_type == ValueType::object) {
bool container_is_empty = false;
bool end_of_container = !iterator.down();
if (end_of_container) {
current_type = ValueType::end_of_container;
container_is_empty = true;
}
else {
if (previous_type == ValueType::object) {
current_type = ValueType::key;
}
else {
current_type = simdjsontype_to_fuzzytype(iterator.get_type());
}
}
ContainerInfos container_infos {previous_type, container_is_empty};
container_stack.push(container_infos);
return current_type;
}
if (previous_type == ValueType::end_of_container) {
// if the container is empty then we're not actually into it.
if (!container_stack.top().is_empty) {
iterator.up();
}
container_stack.pop();
}
if (previous_type == ValueType::end_of_document || container_stack.size() == 0) {
current_type = ValueType::end_of_document;
return current_type;
}
bool end_of_container = !iterator.next();
if (end_of_container) {
current_type = ValueType::end_of_container;
return current_type;
}
if (container_stack.top().type == ValueType::object && previous_type != ValueType::key) {
current_type = ValueType::key;
}
else {
current_type = simdjsontype_to_fuzzytype(iterator.get_type());
}
return current_type;
}
ValueType get_current_type() override { return current_type; }
std::string get_string() override { return std::string(iterator.get_string(), iterator.get_string_length()); }
int64_t get_integer() override { return iterator.get_integer(); }
double get_floating() override { return iterator.get_double(); }
bool get_boolean() override { return iterator.is_true(); }
private:
ValueType current_type;
std::stack<ContainerInfos> container_stack;
simdjson::ParsedJson::Iterator iterator;
simdjson::ParsedJson* parsed_json;
ValueType simdjsontype_to_fuzzytype(char simdjson_type) {
switch (simdjson_type) {
case '{':
return ValueType::object;
case '[':
return ValueType::array;
case '"':
return ValueType::string;
case 'l':
return ValueType::integer;
case 'd':
return ValueType::floating;
case 't':
case 'f':
return ValueType::boolean;
case 'n':
return ValueType::null;
default :
return ValueType::error;
}
}
};
class SimdjsonParser : public Parser {
public:
SimdjsonParser()
: Parser("simdjson")
{}
~SimdjsonParser() = default;
std::unique_ptr<Traverser> parse(const char* json, int size) override
{
simdjson::ParsedJson* parsed_json = new simdjson::ParsedJson(); // The traverser will take ownership of it
bool allocation_is_successful = parsed_json->allocate_capacity(size);
if (!allocation_is_successful) {
return std::make_unique<InvalidTraverser>(get_name());
}
auto simdjson_result = static_cast<simdjson::ErrorValues>(json_parse(json, size, *parsed_json));
if (simdjson_result == simdjson::ErrorValues::SUCCESS) {
return std::make_unique<SimdjsonTraverser>(get_name(), ParsingState::ok, parsed_json);
}
else {
return std::make_unique<InvalidTraverser>(get_name());
}
}
};
}
#endif