1 | namespace simdjson { |
---|---|
2 | namespace SIMDJSON_IMPLEMENTATION { |
3 | namespace ondemand { |
4 | |
5 | simdjson_inline value::value(const value_iterator &_iter) noexcept |
6 | : iter{_iter} |
7 | { |
8 | } |
9 | simdjson_inline value value::start(const value_iterator &iter) noexcept { |
10 | return iter; |
11 | } |
12 | simdjson_inline value value::resume(const value_iterator &iter) noexcept { |
13 | return iter; |
14 | } |
15 | |
16 | simdjson_inline simdjson_result<array> value::get_array() noexcept { |
17 | return array::start(iter); |
18 | } |
19 | simdjson_inline simdjson_result<object> value::get_object() noexcept { |
20 | return object::start(iter); |
21 | } |
22 | simdjson_inline simdjson_result<object> value::start_or_resume_object() noexcept { |
23 | if (iter.at_start()) { |
24 | return get_object(); |
25 | } else { |
26 | return object::resume(iter); |
27 | } |
28 | } |
29 | |
30 | simdjson_inline simdjson_result<raw_json_string> value::get_raw_json_string() noexcept { |
31 | return iter.get_raw_json_string(); |
32 | } |
33 | simdjson_inline simdjson_result<std::string_view> value::get_string(bool allow_replacement) noexcept { |
34 | return iter.get_string(allow_replacement); |
35 | } |
36 | simdjson_inline simdjson_result<std::string_view> value::get_wobbly_string() noexcept { |
37 | return iter.get_wobbly_string(); |
38 | } |
39 | simdjson_inline simdjson_result<double> value::get_double() noexcept { |
40 | return iter.get_double(); |
41 | } |
42 | simdjson_inline simdjson_result<double> value::get_double_in_string() noexcept { |
43 | return iter.get_double_in_string(); |
44 | } |
45 | simdjson_inline simdjson_result<uint64_t> value::get_uint64() noexcept { |
46 | return iter.get_uint64(); |
47 | } |
48 | simdjson_inline simdjson_result<uint64_t> value::get_uint64_in_string() noexcept { |
49 | return iter.get_uint64_in_string(); |
50 | } |
51 | simdjson_inline simdjson_result<int64_t> value::get_int64() noexcept { |
52 | return iter.get_int64(); |
53 | } |
54 | simdjson_inline simdjson_result<int64_t> value::get_int64_in_string() noexcept { |
55 | return iter.get_int64_in_string(); |
56 | } |
57 | simdjson_inline simdjson_result<bool> value::get_bool() noexcept { |
58 | return iter.get_bool(); |
59 | } |
60 | simdjson_inline simdjson_result<bool> value::is_null() noexcept { |
61 | return iter.is_null(); |
62 | } |
63 | template<> simdjson_inline simdjson_result<array> value::get() noexcept { return get_array(); } |
64 | template<> simdjson_inline simdjson_result<object> value::get() noexcept { return get_object(); } |
65 | template<> simdjson_inline simdjson_result<raw_json_string> value::get() noexcept { return get_raw_json_string(); } |
66 | template<> simdjson_inline simdjson_result<std::string_view> value::get() noexcept { return get_string(allow_replacement: false); } |
67 | template<> simdjson_inline simdjson_result<number> value::get() noexcept { return get_number(); } |
68 | template<> simdjson_inline simdjson_result<double> value::get() noexcept { return get_double(); } |
69 | template<> simdjson_inline simdjson_result<uint64_t> value::get() noexcept { return get_uint64(); } |
70 | template<> simdjson_inline simdjson_result<int64_t> value::get() noexcept { return get_int64(); } |
71 | template<> simdjson_inline simdjson_result<bool> value::get() noexcept { return get_bool(); } |
72 | |
73 | template<typename T> simdjson_inline error_code value::get(T &out) noexcept { |
74 | return get<T>().get(out); |
75 | } |
76 | |
77 | #if SIMDJSON_EXCEPTIONS |
78 | simdjson_inline value::operator array() noexcept(false) { |
79 | return get_array(); |
80 | } |
81 | simdjson_inline value::operator object() noexcept(false) { |
82 | return get_object(); |
83 | } |
84 | simdjson_inline value::operator uint64_t() noexcept(false) { |
85 | return get_uint64(); |
86 | } |
87 | simdjson_inline value::operator int64_t() noexcept(false) { |
88 | return get_int64(); |
89 | } |
90 | simdjson_inline value::operator double() noexcept(false) { |
91 | return get_double(); |
92 | } |
93 | simdjson_inline value::operator std::string_view() noexcept(false) { |
94 | return get_string(allow_replacement: false); |
95 | } |
96 | simdjson_inline value::operator raw_json_string() noexcept(false) { |
97 | return get_raw_json_string(); |
98 | } |
99 | simdjson_inline value::operator bool() noexcept(false) { |
100 | return get_bool(); |
101 | } |
102 | #endif |
103 | |
104 | simdjson_inline simdjson_result<array_iterator> value::begin() & noexcept { |
105 | return get_array().begin(); |
106 | } |
107 | simdjson_inline simdjson_result<array_iterator> value::end() & noexcept { |
108 | return {}; |
109 | } |
110 | simdjson_inline simdjson_result<size_t> value::count_elements() & noexcept { |
111 | simdjson_result<size_t> answer; |
112 | auto a = get_array(); |
113 | answer = a.count_elements(); |
114 | // count_elements leaves you pointing inside the array, at the first element. |
115 | // We need to move back so that the user can create a new array (which requires that |
116 | // we point at '['). |
117 | iter.move_at_start(); |
118 | return answer; |
119 | } |
120 | simdjson_inline simdjson_result<size_t> value::count_fields() & noexcept { |
121 | simdjson_result<size_t> answer; |
122 | auto a = get_object(); |
123 | answer = a.count_fields(); |
124 | iter.move_at_start(); |
125 | return answer; |
126 | } |
127 | simdjson_inline simdjson_result<value> value::at(size_t index) noexcept { |
128 | auto a = get_array(); |
129 | return a.at(index); |
130 | } |
131 | |
132 | simdjson_inline simdjson_result<value> value::find_field(std::string_view key) noexcept { |
133 | return start_or_resume_object().find_field(key); |
134 | } |
135 | simdjson_inline simdjson_result<value> value::find_field(const char *key) noexcept { |
136 | return start_or_resume_object().find_field(key); |
137 | } |
138 | |
139 | simdjson_inline simdjson_result<value> value::find_field_unordered(std::string_view key) noexcept { |
140 | return start_or_resume_object().find_field_unordered(key); |
141 | } |
142 | simdjson_inline simdjson_result<value> value::find_field_unordered(const char *key) noexcept { |
143 | return start_or_resume_object().find_field_unordered(key); |
144 | } |
145 | |
146 | simdjson_inline simdjson_result<value> value::operator[](std::string_view key) noexcept { |
147 | return start_or_resume_object()[key]; |
148 | } |
149 | simdjson_inline simdjson_result<value> value::operator[](const char *key) noexcept { |
150 | return start_or_resume_object()[key]; |
151 | } |
152 | |
153 | simdjson_inline simdjson_result<json_type> value::type() noexcept { |
154 | return iter.type(); |
155 | } |
156 | |
157 | simdjson_inline simdjson_result<bool> value::is_scalar() noexcept { |
158 | json_type this_type; |
159 | auto error = type().get(value&: this_type); |
160 | if(error) { return error; } |
161 | return ! ((this_type == json_type::array) || (this_type == json_type::object)); |
162 | } |
163 | |
164 | simdjson_inline bool value::is_negative() noexcept { |
165 | return iter.is_negative(); |
166 | } |
167 | |
168 | simdjson_inline simdjson_result<bool> value::is_integer() noexcept { |
169 | return iter.is_integer(); |
170 | } |
171 | simdjson_warn_unused simdjson_inline simdjson_result<number_type> value::get_number_type() noexcept { |
172 | return iter.get_number_type(); |
173 | } |
174 | simdjson_warn_unused simdjson_inline simdjson_result<number> value::get_number() noexcept { |
175 | return iter.get_number(); |
176 | } |
177 | |
178 | simdjson_inline std::string_view value::raw_json_token() noexcept { |
179 | return std::string_view(reinterpret_cast<const char*>(iter.peek_start()), iter.peek_start_length()); |
180 | } |
181 | |
182 | simdjson_inline simdjson_result<const char *> value::current_location() noexcept { |
183 | return iter.json_iter().current_location(); |
184 | } |
185 | |
186 | simdjson_inline int32_t value::current_depth() const noexcept{ |
187 | return iter.json_iter().depth(); |
188 | } |
189 | |
190 | simdjson_inline simdjson_result<value> value::at_pointer(std::string_view json_pointer) noexcept { |
191 | json_type t; |
192 | SIMDJSON_TRY(type().get(t)); |
193 | switch (t) |
194 | { |
195 | case json_type::array: |
196 | return (*this).get_array().at_pointer(json_pointer); |
197 | case json_type::object: |
198 | return (*this).get_object().at_pointer(json_pointer); |
199 | default: |
200 | return INVALID_JSON_POINTER; |
201 | } |
202 | } |
203 | |
204 | } // namespace ondemand |
205 | } // namespace SIMDJSON_IMPLEMENTATION |
206 | } // namespace simdjson |
207 | |
208 | namespace simdjson { |
209 | |
210 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result( |
211 | SIMDJSON_IMPLEMENTATION::ondemand::value &&value |
212 | ) noexcept : |
213 | implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>( |
214 | std::forward<SIMDJSON_IMPLEMENTATION::ondemand::value>(t&: value) |
215 | ) |
216 | { |
217 | } |
218 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result( |
219 | error_code error |
220 | ) noexcept : |
221 | implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(error) |
222 | { |
223 | } |
224 | simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_elements() & noexcept { |
225 | if (error()) { return error(); } |
226 | return first.count_elements(); |
227 | } |
228 | simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_fields() & noexcept { |
229 | if (error()) { return error(); } |
230 | return first.count_fields(); |
231 | } |
232 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at(size_t index) noexcept { |
233 | if (error()) { return error(); } |
234 | return first.at(index); |
235 | } |
236 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::begin() & noexcept { |
237 | if (error()) { return error(); } |
238 | return first.begin(); |
239 | } |
240 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::end() & noexcept { |
241 | if (error()) { return error(); } |
242 | return {}; |
243 | } |
244 | |
245 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::find_field(std::string_view key) noexcept { |
246 | if (error()) { return error(); } |
247 | return first.find_field(key); |
248 | } |
249 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::find_field(const char *key) noexcept { |
250 | if (error()) { return error(); } |
251 | return first.find_field(key); |
252 | } |
253 | |
254 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::find_field_unordered(std::string_view key) noexcept { |
255 | if (error()) { return error(); } |
256 | return first.find_field_unordered(key); |
257 | } |
258 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::find_field_unordered(const char *key) noexcept { |
259 | if (error()) { return error(); } |
260 | return first.find_field_unordered(key); |
261 | } |
262 | |
263 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](std::string_view key) noexcept { |
264 | if (error()) { return error(); } |
265 | return first[key]; |
266 | } |
267 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](const char *key) noexcept { |
268 | if (error()) { return error(); } |
269 | return first[key]; |
270 | } |
271 | |
272 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_array() noexcept { |
273 | if (error()) { return error(); } |
274 | return first.get_array(); |
275 | } |
276 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_object() noexcept { |
277 | if (error()) { return error(); } |
278 | return first.get_object(); |
279 | } |
280 | simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64() noexcept { |
281 | if (error()) { return error(); } |
282 | return first.get_uint64(); |
283 | } |
284 | simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64_in_string() noexcept { |
285 | if (error()) { return error(); } |
286 | return first.get_uint64_in_string(); |
287 | } |
288 | simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64() noexcept { |
289 | if (error()) { return error(); } |
290 | return first.get_int64(); |
291 | } |
292 | simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64_in_string() noexcept { |
293 | if (error()) { return error(); } |
294 | return first.get_int64_in_string(); |
295 | } |
296 | simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double() noexcept { |
297 | if (error()) { return error(); } |
298 | return first.get_double(); |
299 | } |
300 | simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double_in_string() noexcept { |
301 | if (error()) { return error(); } |
302 | return first.get_double_in_string(); |
303 | } |
304 | simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(bool allow_replacement) noexcept { |
305 | if (error()) { return error(); } |
306 | return first.get_string(allow_replacement); |
307 | } |
308 | simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_wobbly_string() noexcept { |
309 | if (error()) { return error(); } |
310 | return first.get_wobbly_string(); |
311 | } |
312 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_raw_json_string() noexcept { |
313 | if (error()) { return error(); } |
314 | return first.get_raw_json_string(); |
315 | } |
316 | simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_bool() noexcept { |
317 | if (error()) { return error(); } |
318 | return first.get_bool(); |
319 | } |
320 | simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_null() noexcept { |
321 | if (error()) { return error(); } |
322 | return first.is_null(); |
323 | } |
324 | |
325 | template<typename T> simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get() noexcept { |
326 | if (error()) { return error(); } |
327 | return first.get<T>(); |
328 | } |
329 | template<typename T> simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get(T &out) noexcept { |
330 | if (error()) { return error(); } |
331 | return first.get<T>(out); |
332 | } |
333 | |
334 | template<> simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>() noexcept { |
335 | if (error()) { return error(); } |
336 | return std::move(first); |
337 | } |
338 | template<> simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>(SIMDJSON_IMPLEMENTATION::ondemand::value &out) noexcept { |
339 | if (error()) { return error(); } |
340 | out = first; |
341 | return SUCCESS; |
342 | } |
343 | |
344 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::type() noexcept { |
345 | if (error()) { return error(); } |
346 | return first.type(); |
347 | } |
348 | simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_scalar() noexcept { |
349 | if (error()) { return error(); } |
350 | return first.is_scalar(); |
351 | } |
352 | simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_negative() noexcept { |
353 | if (error()) { return error(); } |
354 | return first.is_negative(); |
355 | } |
356 | simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_integer() noexcept { |
357 | if (error()) { return error(); } |
358 | return first.is_integer(); |
359 | } |
360 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number_type() noexcept { |
361 | if (error()) { return error(); } |
362 | return first.get_number_type(); |
363 | } |
364 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number() noexcept { |
365 | if (error()) { return error(); } |
366 | return first.get_number(); |
367 | } |
368 | #if SIMDJSON_EXCEPTIONS |
369 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) { |
370 | if (error()) { throw simdjson_error(error()); } |
371 | return first; |
372 | } |
373 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false) { |
374 | if (error()) { throw simdjson_error(error()); } |
375 | return first; |
376 | } |
377 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator uint64_t() noexcept(false) { |
378 | if (error()) { throw simdjson_error(error()); } |
379 | return first; |
380 | } |
381 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator int64_t() noexcept(false) { |
382 | if (error()) { throw simdjson_error(error()); } |
383 | return first; |
384 | } |
385 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator double() noexcept(false) { |
386 | if (error()) { throw simdjson_error(error()); } |
387 | return first; |
388 | } |
389 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator std::string_view() noexcept(false) { |
390 | if (error()) { throw simdjson_error(error()); } |
391 | return first; |
392 | } |
393 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false) { |
394 | if (error()) { throw simdjson_error(error()); } |
395 | return first; |
396 | } |
397 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator bool() noexcept(false) { |
398 | if (error()) { throw simdjson_error(error()); } |
399 | return first; |
400 | } |
401 | #endif |
402 | |
403 | simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::raw_json_token() noexcept { |
404 | if (error()) { return error(); } |
405 | return first.raw_json_token(); |
406 | } |
407 | |
408 | simdjson_inline simdjson_result<const char *> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::current_location() noexcept { |
409 | if (error()) { return error(); } |
410 | return first.current_location(); |
411 | } |
412 | |
413 | simdjson_inline simdjson_result<int32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::current_depth() const noexcept { |
414 | if (error()) { return error(); } |
415 | return first.current_depth(); |
416 | } |
417 | |
418 | simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_pointer(std::string_view json_pointer) noexcept { |
419 | if (error()) { return error(); } |
420 | return first.at_pointer(json_pointer); |
421 | } |
422 | |
423 | } // namespace simdjson |
424 |