-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimdjson.cpp
More file actions
243 lines (208 loc) · 5.41 KB
/
simdjson.cpp
File metadata and controls
243 lines (208 loc) · 5.41 KB
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
#include "simdjson.h"
#include "util.h"
namespace tba{
Decode_error::Decode_error(const char *a,const char *b,const char *c):
path({std::string(a)}),
value(b),
description(c)
{}
Decode_error::Decode_error(const char *a,std::string b,const char *c):
path({a}),
value(b),
description(c)
{}
Decode_error::Decode_error(const char *a,std::string_view b,const char *c):
path({a}),
value(b),
description(c)
{}
Decode_error::Decode_error(const char *s,const char *value1,std::string what):
path({s}),value(value1),description(what)
{}
Decode_error::Decode_error(const char *s,std::string value1,std::string what):
path({s}),value(value1),description(what)
{}
Decode_error::Decode_error(std::string a,std::string b,std::string c):
path({a}),
value(b),
description(c)
{}
std::ostream& operator<<(std::ostream& o,Decode_error const& a){
o<<"Decode_error(\n";
o<<"\tfrom:"<<a.value<<"\n";
o<<"\tpath:"<<a.path<<"\n";
o<<"\tdescription:"<<a.description<<"\n";
return o<<")";
}
std::optional<std::nullptr_t> maybe_decode(JSON_object in,std::nullptr_t const*){
if(in.size()){
return std::nullopt;
}
TBA_NYI
}
std::string decode(JSON_value in,std::string const*){
if(in.type()!=simdjson::dom::element_type::STRING){
throw Decode_error("string",as_string(in),"foo4");
}
try{
std::string_view s=in.get_string();
return std::string{s.begin(),s.end()};
}catch(...){
TBA_NYI
}
}
std::optional<std::string> maybe_decode(JSON_value in,std::string const*){
if(in.type()!=simdjson::dom::element_type::STRING){
return std::nullopt;
}
std::string_view s=in.get_string();
return std::string(s.begin(),s.end());
}
int decode(JSON_value a,int const*){
switch(a.type()){
case simdjson::dom::element_type::INT64:
return a.get_int64();
case simdjson::dom::element_type::NULL_VALUE:
throw Decode_error{"int",as_string(a),"wrong type, got null"};
case simdjson::dom::element_type::ARRAY:
throw Decode_error{"array",as_string(a),"wrong type, got array"};
case simdjson::dom::element_type::DOUBLE:
//could see if it's a whole number and coerce it to be an int.
throw Decode_error{"double",as_string(a),"wrong type, got double"};
case simdjson::dom::element_type::BOOL:
throw Decode_error{"bool",as_string(a),"wrong type, got bool"};
default:
TBA_PRINT(a.type())
TBA_NYI
}
}
std::optional<int> maybe_decode(JSON_value a,int const*){
switch(a.type()){
case simdjson::dom::element_type::INT64:
return a.get_int64();
case simdjson::dom::element_type::NULL_VALUE:
case simdjson::dom::element_type::ARRAY:
return std::nullopt;
case simdjson::dom::element_type::DOUBLE:
//could see if it's a whole number and coerce it to be an int.
return std::nullopt;
case simdjson::dom::element_type::BOOL:
return std::nullopt;
default:
TBA_PRINT(a.type())
TBA_NYI
}
}
short decode(JSON_value a,short const*){
switch(a.type()){
case simdjson::dom::element_type::INT64:{
int64_t r=a.get_int64();
assert(r>=std::numeric_limits<short>::min());
assert(r<=std::numeric_limits<short>::max());
return r;
}
default:
TBA_PRINT(a.type())
TBA_NYI
}
}
std::optional<short> maybe_decode(JSON_value a,short const* x){
return decode(a,x);
}
std::optional<short> maybe_decode(std::nullptr_t,short const*){
return std::nullopt;
}
unsigned decode(JSON_value a,unsigned const*){
switch(a.type()){
case simdjson::dom::element_type::INT64:{
int64_t r=a.get_int64();
assert(r>=0);
return r;
}
default:
TBA_PRINT(a.type())
TBA_NYI
}
}
long decode(JSON_value a,long const*){
switch(a.type()){
case simdjson::dom::element_type::INT64:
return a.get_int64();
default:
throw Decode_error("long",as_string(a),"expected integer input");
}
}
std::optional<long> maybe_decode(JSON_value a,long const*){
switch(a.type()){
case simdjson::dom::element_type::INT64:
return a.get_int64();
default:
return std::nullopt;
}
}
bool decode(JSON_value a,bool const*){
switch(a.type()){
case simdjson::dom::element_type::BOOL:
return a.get_bool();
case simdjson::dom::element_type::INT64:
throw Decode_error("int",as_string(a),"expected bool, got int");
default:
TBA_PRINT(a.type());
TBA_PRINT(a);
TBA_NYI
}
}
std::optional<bool> maybe_decode(JSON_value in,bool const* x){
try{
return decode(in,x);
}catch(...){
return std::nullopt;
}
}
double decode(JSON_value a,double const*){
switch(a.type()){
case simdjson::dom::element_type::DOUBLE:
return a.get_double();
case simdjson::dom::element_type::INT64:
return a.get_int64();
default:
TBA_PRINT(a.type());
TBA_NYI
}
(void)a;
TBA_NYI/*if(!a.is_scalar()){
TBA_NYI
}
return a.get_double();*/
}
std::nullptr_t decode(JSON_value,std::nullptr_t const*){
TBA_NYI
}
std::optional<std::nullptr_t> maybe_decode(JSON_value,std::nullptr_t const*){
TBA_NYI
}
std::nullptr_t decode(JSON_object in,std::nullptr_t const*){
if(in.size()){
throw Decode_error("nullptr_t",as_string(in),"non-empty object");
}
TBA_PRINT(in);
TBA_NYI
}
std::string decode2(std::string_view a,std::string const*){
return std::string(a);
}
std::nullptr_t decode(std::nullptr_t,std::nullptr_t const*){
return nullptr;
}
std::optional<bool> maybe_decode(std::nullptr_t,bool const*){
return std::nullopt;
}
std::optional<int> maybe_decode(std::nullptr_t,int const*){
return std::nullopt;
}
#define X(T) T decode(std::nullptr_t,T const*){ throw Decode_error("nullptr_t","null","Expected "#T); }
X(double)
X(std::string)
X(unsigned)
#undef X
}