simdjson 5.0.2
Ridiculously Fast JSON
Loading...
Searching...
No Matches
value-inl.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_VALUE_INL_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_VALUE_INL_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/ondemand/array.h"
7#include "simdjson/generic/ondemand/array_iterator.h"
8#include "simdjson/generic/ondemand/json_iterator.h"
9#include "simdjson/generic/ondemand/json_type.h"
10#include "simdjson/generic/ondemand/object.h"
11#include "simdjson/generic/ondemand/raw_json_string.h"
12#include "simdjson/generic/ondemand/value.h"
13#endif // SIMDJSON_CONDITIONAL_INCLUDE
14
15#include <limits>
16
17namespace simdjson {
18namespace SIMDJSON_IMPLEMENTATION {
19namespace ondemand {
20
25template <typename T>
26simdjson_inline simdjson_result<T> narrow_integer(simdjson_result<uint64_t> wide) noexcept {
27 uint64_t result;
28 SIMDJSON_TRY(std::move(wide).get(result));
29 if (result > static_cast<uint64_t>((std::numeric_limits<T>::max)())) { return NUMBER_OUT_OF_RANGE; }
30 return static_cast<T>(result);
31}
32
37template <typename T>
38simdjson_inline simdjson_result<T> narrow_integer(simdjson_result<int64_t> wide) noexcept {
39 int64_t result;
40 SIMDJSON_TRY(std::move(wide).get(result));
41 if (result > static_cast<int64_t>((std::numeric_limits<T>::max)()) || result < static_cast<int64_t>((std::numeric_limits<T>::min)())) { return NUMBER_OUT_OF_RANGE; }
42 return static_cast<T>(result);
43}
44
45#if SIMDJSON_SUPPORTS_FLOAT32_T
46// get_float32() parses with get_float(), so float must be binary32.
47static_assert(std::numeric_limits<float>::is_iec559 && std::numeric_limits<float>::digits == 24,
48 "get_float32() requires float to be IEEE binary32");
49#endif // SIMDJSON_SUPPORTS_FLOAT32_T
50#if SIMDJSON_SUPPORTS_FLOAT64_T
51// get_float64() parses with get_double(), so double must be binary64.
52static_assert(std::numeric_limits<double>::is_iec559 && std::numeric_limits<double>::digits == 53,
53 "get_float64() requires double to be IEEE binary64");
54#endif // SIMDJSON_SUPPORTS_FLOAT64_T
55
56simdjson_inline value::value(const value_iterator &_iter) noexcept
57 : iter{_iter}
58{
59}
60simdjson_inline value value::start(const value_iterator &iter) noexcept {
61 return iter;
62}
63simdjson_inline value value::resume(const value_iterator &iter) noexcept {
64 return iter;
65}
66
67simdjson_inline simdjson_result<array> value::get_array() noexcept {
68 return array::start(iter);
69}
70simdjson_inline simdjson_result<object> value::get_object() noexcept {
71 return object::start(iter);
72}
74 if (iter.at_start()) {
75 return get_object();
76 } else {
77 return object::resume(iter);
78 }
79}
80
82 return iter.get_raw_json_string();
83}
84simdjson_inline simdjson_result<std::string_view> value::get_string(bool allow_replacement) noexcept {
85 return iter.get_string(allow_replacement);
86}
87#if SIMDJSON_SUPPORTS_CHAR8_T
88simdjson_inline simdjson_result<std::u8string_view> value::get_u8string(bool allow_replacement) noexcept {
89 std::string_view content;
90 SIMDJSON_TRY( get_string(allow_replacement).get(content) );
91 return internal::as_u8string_view(content);
92}
93#endif // SIMDJSON_SUPPORTS_CHAR8_T
94template <typename string_type>
95simdjson_warn_unused simdjson_inline error_code value::get_string(string_type& receiver, bool allow_replacement) noexcept {
96 return iter.get_string(receiver, allow_replacement);
97}
99 return iter.get_wobbly_string();
100}
101simdjson_inline simdjson_result<double> value::get_double() noexcept {
102 return iter.get_double();
103}
105 return iter.get_double_in_string();
106}
107simdjson_inline simdjson_result<float> value::get_float() noexcept {
108 return iter.get_float();
109}
111 return iter.get_float_in_string();
112}
114 return iter.get_uint64();
115}
117 return iter.get_uint64_in_string();
118}
119simdjson_inline simdjson_result<int64_t> value::get_int64() noexcept {
120 return iter.get_int64();
121}
123 return iter.get_int64_in_string();
124}
126 return narrow_integer<uint32_t>(get_uint64());
127}
128simdjson_inline simdjson_result<int32_t> value::get_int32() noexcept {
129 return narrow_integer<int32_t>(get_int64());
130}
132 return narrow_integer<uint16_t>(get_uint64());
133}
134simdjson_inline simdjson_result<int16_t> value::get_int16() noexcept {
135 return narrow_integer<int16_t>(get_int64());
136}
137simdjson_inline simdjson_result<uint8_t> value::get_uint8() noexcept {
138 return narrow_integer<uint8_t>(get_uint64());
139}
140simdjson_inline simdjson_result<int8_t> value::get_int8() noexcept {
141 return narrow_integer<int8_t>(get_int64());
142}
143#if SIMDJSON_SUPPORTS_FLOAT32_T
144simdjson_inline simdjson_result<std::float32_t> value::get_float32() noexcept {
145 float result;
146 SIMDJSON_TRY(get_float().get(result));
147 return static_cast<std::float32_t>(result);
148}
149#endif // SIMDJSON_SUPPORTS_FLOAT32_T
150#if SIMDJSON_SUPPORTS_FLOAT64_T
151simdjson_inline simdjson_result<std::float64_t> value::get_float64() noexcept {
152 double result;
153 SIMDJSON_TRY(get_double().get(result));
154 return static_cast<std::float64_t>(result);
155}
156#endif // SIMDJSON_SUPPORTS_FLOAT64_T
157simdjson_inline simdjson_result<bool> value::get_bool() noexcept {
158 return iter.get_bool();
159}
160simdjson_inline simdjson_result<bool> value::is_null() noexcept {
161 return iter.is_null();
162}
163
164template<> simdjson_inline simdjson_result<array> value::get() noexcept { return get_array(); }
165template<> simdjson_inline simdjson_result<object> value::get() noexcept { return get_object(); }
166template<> simdjson_inline simdjson_result<raw_json_string> value::get() noexcept { return get_raw_json_string(); }
167template<> simdjson_inline simdjson_result<std::string_view> value::get() noexcept { return get_string(false); }
168#if SIMDJSON_SUPPORTS_CHAR8_T
169template<> simdjson_inline simdjson_result<std::u8string_view> value::get() noexcept { return get_u8string(false); }
170#endif // SIMDJSON_SUPPORTS_CHAR8_T
171template<> simdjson_inline simdjson_result<number> value::get() noexcept { return get_number(); }
172template<> simdjson_inline simdjson_result<double> value::get() noexcept { return get_double(); }
173template<> simdjson_inline simdjson_result<float> value::get() noexcept { return get_float(); }
174template<> simdjson_inline simdjson_result<uint64_t> value::get() noexcept { return get_uint64(); }
175template<> simdjson_inline simdjson_result<int64_t> value::get() noexcept { return get_int64(); }
176template<> simdjson_inline simdjson_result<uint32_t> value::get() noexcept { return get_uint32(); }
177template<> simdjson_inline simdjson_result<int32_t> value::get() noexcept { return get_int32(); }
178template<> simdjson_inline simdjson_result<uint16_t> value::get() noexcept { return get_uint16(); }
179template<> simdjson_inline simdjson_result<int16_t> value::get() noexcept { return get_int16(); }
180template<> simdjson_inline simdjson_result<uint8_t> value::get() noexcept { return get_uint8(); }
181template<> simdjson_inline simdjson_result<int8_t> value::get() noexcept { return get_int8(); }
182#if SIMDJSON_SUPPORTS_FLOAT32_T
183template<> simdjson_inline simdjson_result<std::float32_t> value::get() noexcept { return get_float32(); }
184#endif // SIMDJSON_SUPPORTS_FLOAT32_T
185#if SIMDJSON_SUPPORTS_FLOAT64_T
186template<> simdjson_inline simdjson_result<std::float64_t> value::get() noexcept { return get_float64(); }
187#endif // SIMDJSON_SUPPORTS_FLOAT64_T
188template<> simdjson_inline simdjson_result<bool> value::get() noexcept { return get_bool(); }
189
190
191template<> simdjson_warn_unused simdjson_inline error_code value::get(array& out) noexcept { return get_array().get(out); }
192template<> simdjson_warn_unused simdjson_inline error_code value::get(object& out) noexcept { return get_object().get(out); }
193template<> simdjson_warn_unused simdjson_inline error_code value::get(raw_json_string& out) noexcept { return get_raw_json_string().get(out); }
194template<> simdjson_warn_unused simdjson_inline error_code value::get(std::string_view& out) noexcept { return get_string(false).get(out); }
195#if SIMDJSON_SUPPORTS_CHAR8_T
196template<> simdjson_warn_unused simdjson_inline error_code value::get(std::u8string_view& out) noexcept { return get_u8string(false).get(out); }
197#endif // SIMDJSON_SUPPORTS_CHAR8_T
198template<> simdjson_warn_unused simdjson_inline error_code value::get(number& out) noexcept { return get_number().get(out); }
199template<> simdjson_warn_unused simdjson_inline error_code value::get(double& out) noexcept { return get_double().get(out); }
200template<> simdjson_warn_unused simdjson_inline error_code value::get(float& out) noexcept { return get_float().get(out); }
201template<> simdjson_warn_unused simdjson_inline error_code value::get(uint64_t& out) noexcept { return get_uint64().get(out); }
202template<> simdjson_warn_unused simdjson_inline error_code value::get(int64_t& out) noexcept { return get_int64().get(out); }
203template<> simdjson_warn_unused simdjson_inline error_code value::get(uint32_t& out) noexcept { return get_uint32().get(out); }
204template<> simdjson_warn_unused simdjson_inline error_code value::get(int32_t& out) noexcept { return get_int32().get(out); }
205template<> simdjson_warn_unused simdjson_inline error_code value::get(uint16_t& out) noexcept { return get_uint16().get(out); }
206template<> simdjson_warn_unused simdjson_inline error_code value::get(int16_t& out) noexcept { return get_int16().get(out); }
207template<> simdjson_warn_unused simdjson_inline error_code value::get(uint8_t& out) noexcept { return get_uint8().get(out); }
208template<> simdjson_warn_unused simdjson_inline error_code value::get(int8_t& out) noexcept { return get_int8().get(out); }
209#if SIMDJSON_SUPPORTS_FLOAT32_T
210template<> simdjson_warn_unused simdjson_inline error_code value::get(std::float32_t& out) noexcept { return get_float32().get(out); }
211#endif // SIMDJSON_SUPPORTS_FLOAT32_T
212#if SIMDJSON_SUPPORTS_FLOAT64_T
213template<> simdjson_warn_unused simdjson_inline error_code value::get(std::float64_t& out) noexcept { return get_float64().get(out); }
214#endif // SIMDJSON_SUPPORTS_FLOAT64_T
215template<> simdjson_warn_unused simdjson_inline error_code value::get(bool& out) noexcept { return get_bool().get(out); }
216
217#if SIMDJSON_EXCEPTIONS
218template <class T>
219simdjson_inline value::operator T() noexcept(false) {
220 return get<T>();
221}
222simdjson_inline value::operator array() noexcept(false) {
223 return get_array();
224}
225simdjson_inline value::operator object() noexcept(false) {
226 return get_object();
227}
228simdjson_inline value::operator uint64_t() noexcept(false) {
229 return get_uint64();
230}
231simdjson_inline value::operator int64_t() noexcept(false) {
232 return get_int64();
233}
234simdjson_inline value::operator double() noexcept(false) {
235 return get_double();
236}
237simdjson_inline value::operator std::string_view() noexcept(false) {
238 return get_string(false);
239}
240simdjson_inline value::operator raw_json_string() noexcept(false) {
241 return get_raw_json_string();
242}
243simdjson_inline value::operator bool() noexcept(false) {
244 return get_bool();
245}
246#endif
247
248simdjson_inline simdjson_result<array_iterator> value::begin() & noexcept {
249 return get_array().begin();
250}
251simdjson_inline simdjson_result<array_iterator> value::end() & noexcept {
252 return {};
253}
254simdjson_inline simdjson_result<size_t> value::count_elements() & noexcept {
256 auto a = get_array();
257 answer = a.count_elements();
258 // count_elements leaves you pointing inside the array, at the first element.
259 // We need to move back so that the user can create a new array (which requires that
260 // we point at '[').
261 iter.move_at_start();
262 return answer;
263}
264simdjson_inline simdjson_result<size_t> value::count_fields() & noexcept {
266 auto a = get_object();
267 answer = a.count_fields();
268 iter.move_at_start();
269 return answer;
270}
271simdjson_inline simdjson_result<value> value::at(size_t index) noexcept {
272 auto a = get_array();
273 return a.at(index);
274}
275
276simdjson_inline simdjson_result<value> value::find_field(std::string_view key) noexcept {
277 return start_or_resume_object().find_field(key);
278}
279simdjson_inline simdjson_result<value> value::find_field(const char *key) noexcept {
280 return start_or_resume_object().find_field(key);
281}
282
283simdjson_inline simdjson_result<value> value::find_field_unordered(std::string_view key) noexcept {
284 return start_or_resume_object().find_field_unordered(key);
285}
286simdjson_inline simdjson_result<value> value::find_field_unordered(const char *key) noexcept {
287 return start_or_resume_object().find_field_unordered(key);
288}
289
290simdjson_inline simdjson_result<value> value::operator[](std::string_view key) noexcept {
291 return start_or_resume_object()[key];
292}
293simdjson_inline simdjson_result<value> value::operator[](const char *key) noexcept {
294 return start_or_resume_object()[key];
295}
296
297simdjson_inline simdjson_result<json_type> value::type() noexcept {
298 return iter.type();
299}
300
301simdjson_inline simdjson_result<bool> value::is_scalar() noexcept {
302 json_type this_type;
303 auto error = type().get(this_type);
304 if(error) { return error; }
305 return ! ((this_type == json_type::array) || (this_type == json_type::object));
306}
307
308simdjson_inline simdjson_result<bool> value::is_string() noexcept {
309 json_type this_type;
310 auto error = type().get(this_type);
311 if(error) { return error; }
312 return (this_type == json_type::string);
313}
314
315
316simdjson_inline bool value::is_negative() noexcept {
317 return iter.is_negative();
318}
319
320simdjson_inline simdjson_result<bool> value::is_integer() noexcept {
321 return iter.is_integer();
322}
323simdjson_warn_unused simdjson_inline simdjson_result<number_type> value::get_number_type() noexcept {
324 return iter.get_number_type();
325}
326simdjson_warn_unused simdjson_inline simdjson_result<number> value::get_number() noexcept {
327 return iter.get_number();
328}
329
330simdjson_inline std::string_view value::raw_json_token() noexcept {
331 return std::string_view(reinterpret_cast<const char*>(iter.peek_start()), iter.peek_start_length());
332}
333
335 json_type t;
336 SIMDJSON_TRY(type().get(t));
337 switch (t)
338 {
339 case json_type::array: {
340 ondemand::array array;
341 SIMDJSON_TRY(get_array().get(array));
342 return array.raw_json();
343 }
344 case json_type::object: {
345 ondemand::object object;
346 SIMDJSON_TRY(get_object().get(object));
347 return object.raw_json();
348 }
349 default:
350 return raw_json_token();
351 }
352}
353
355 return iter.json_iter().current_location();
356}
357
358simdjson_inline int32_t value::current_depth() const noexcept{
359 return iter.json_iter().depth();
360}
361
362inline bool is_pointer_well_formed(std::string_view json_pointer) noexcept {
363 if (simdjson_unlikely(json_pointer.empty())) { // can't be
364 return false;
365 }
366 if (simdjson_unlikely(json_pointer[0] != '/')) {
367 return false;
368 }
369 size_t escape = json_pointer.find('~');
370 if (escape == std::string_view::npos) {
371 return true;
372 }
373 if (escape == json_pointer.size() - 1) {
374 return false;
375 }
376 if (json_pointer[escape + 1] != '0' && json_pointer[escape + 1] != '1') {
377 return false;
378 }
379 return true;
380}
381
382simdjson_inline simdjson_result<value> value::at_pointer(std::string_view json_pointer) noexcept {
383 json_type t;
384 SIMDJSON_TRY(type().get(t));
385 switch (t)
386 {
387 case json_type::array:
388 return (*this).get_array().at_pointer(json_pointer);
390 return (*this).get_object().at_pointer(json_pointer);
391 default:
392 // a non-empty string can be invalid, or accessing a primitive (issue 2154)
393 if (is_pointer_well_formed(json_pointer)) {
394 return NO_SUCH_FIELD;
395 }
397 }
398}
399
400simdjson_inline simdjson_result<value> value::at_path(std::string_view json_path) noexcept {
401 json_type t;
402 SIMDJSON_TRY(type().get(t));
403 switch (t) {
404 case json_type::array:
405 return (*this).get_array().at_path(json_path);
407 return (*this).get_object().at_path(json_path);
408 default:
410 }
411}
412
413#if SIMDJSON_SUPPORTS_CONCEPTS
414template <typename Func>
415 requires std::invocable<Func, value>
416#else
417template <typename Func>
418#endif
419inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept {
420 // Every recursive step of for_each_at_path_with_wildcard goes through this
421 // function, and each one descends one level into the document. A path with
422 // many segments applied to a deeply nested document would otherwise recurse
423 // without bound and overflow the stack.
424 if (size_t(iter.depth()) >= iter.json_iter().parser->max_depth()) { return DEPTH_ERROR; }
425 json_type t;
426 SIMDJSON_TRY(type().get(t));
427 switch (t) {
428 case json_type::array:
429 return (*this).get_array().for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
431 return (*this).get_object().for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
432 default:
434 }
435}
436
437} // namespace ondemand
438} // namespace SIMDJSON_IMPLEMENTATION
439} // namespace simdjson
440
441namespace simdjson {
442
443simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
444 SIMDJSON_IMPLEMENTATION::ondemand::value &&value
445) noexcept :
446 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(
447 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::value>(value)
448 )
449{
450}
451simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
452 error_code error
453) noexcept :
454 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(error)
455{
456}
457simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_elements() & noexcept {
458 if (error()) { return error(); }
459 return first.count_elements();
460}
461simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_fields() & noexcept {
462 if (error()) { return error(); }
463 return first.count_fields();
464}
465simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at(size_t index) noexcept {
466 if (error()) { return error(); }
467 return first.at(index);
468}
469simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::begin() & noexcept {
470 if (error()) { return error(); }
471 return first.begin();
472}
473simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::end() & noexcept {
474 if (error()) { return error(); }
475 return {};
476}
477
479 if (error()) { return error(); }
480 return first.find_field(key);
481}
483 if (error()) { return error(); }
484 return first.find_field(key);
485}
486
488 if (error()) { return error(); }
489 return first.find_field_unordered(key);
490}
492 if (error()) { return error(); }
493 return first.find_field_unordered(key);
494}
495
496simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](std::string_view key) noexcept {
497 if (error()) { return error(); }
498 return first[key];
499}
500simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](const char *key) noexcept {
501 if (error()) { return error(); }
502 return first[key];
503}
504
505simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_array() noexcept {
506 if (error()) { return error(); }
507 return first.get_array();
508}
509simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_object() noexcept {
510 if (error()) { return error(); }
511 return first.get_object();
512}
513simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64() noexcept {
514 if (error()) { return error(); }
515 return first.get_uint64();
516}
517simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64_in_string() noexcept {
518 if (error()) { return error(); }
519 return first.get_uint64_in_string();
520}
521simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64() noexcept {
522 if (error()) { return error(); }
523 return first.get_int64();
524}
525simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64_in_string() noexcept {
526 if (error()) { return error(); }
527 return first.get_int64_in_string();
528}
529simdjson_inline simdjson_result<uint32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint32() noexcept {
530 if (error()) { return error(); }
531 return first.get_uint32();
532}
533simdjson_inline simdjson_result<int32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int32() noexcept {
534 if (error()) { return error(); }
535 return first.get_int32();
536}
537simdjson_inline simdjson_result<uint16_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint16() noexcept {
538 if (error()) { return error(); }
539 return first.get_uint16();
540}
541simdjson_inline simdjson_result<int16_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int16() noexcept {
542 if (error()) { return error(); }
543 return first.get_int16();
544}
545simdjson_inline simdjson_result<uint8_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint8() noexcept {
546 if (error()) { return error(); }
547 return first.get_uint8();
548}
549simdjson_inline simdjson_result<int8_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int8() noexcept {
550 if (error()) { return error(); }
551 return first.get_int8();
552}
553simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double() noexcept {
554 if (error()) { return error(); }
555 return first.get_double();
556}
557simdjson_inline simdjson_result<float> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_float() noexcept {
558 if (error()) { return error(); }
559 return first.get_float();
560}
561simdjson_inline simdjson_result<float> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_float_in_string() noexcept {
562 if (error()) { return error(); }
563 return first.get_float_in_string();
564}
565#if SIMDJSON_SUPPORTS_FLOAT32_T
566simdjson_inline simdjson_result<std::float32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_float32() noexcept {
567 if (error()) { return error(); }
568 return first.get_float32();
569}
570#endif // SIMDJSON_SUPPORTS_FLOAT32_T
571#if SIMDJSON_SUPPORTS_FLOAT64_T
572simdjson_inline simdjson_result<std::float64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_float64() noexcept {
573 if (error()) { return error(); }
574 return first.get_float64();
575}
576#endif // SIMDJSON_SUPPORTS_FLOAT64_T
577simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double_in_string() noexcept {
578 if (error()) { return error(); }
579 return first.get_double_in_string();
580}
581simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(bool allow_replacement) noexcept {
582 if (error()) { return error(); }
583 return first.get_string(allow_replacement);
584}
585#if SIMDJSON_SUPPORTS_CHAR8_T
586simdjson_inline simdjson_result<std::u8string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_u8string(bool allow_replacement) noexcept {
587 if (error()) { return error(); }
588 return first.get_u8string(allow_replacement);
589}
590#endif // SIMDJSON_SUPPORTS_CHAR8_T
591template <typename string_type>
592simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(string_type& receiver, bool allow_replacement) noexcept {
593 if (error()) { return error(); }
594 return first.get_string(receiver, allow_replacement);
595}
596simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_wobbly_string() noexcept {
597 if (error()) { return error(); }
598 return first.get_wobbly_string();
599}
600simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_raw_json_string() noexcept {
601 if (error()) { return error(); }
602 return first.get_raw_json_string();
603}
604simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_bool() noexcept {
605 if (error()) { return error(); }
606 return first.get_bool();
607}
608simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_null() noexcept {
609 if (error()) { return error(); }
610 return first.is_null();
611}
612
613template<> simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>(SIMDJSON_IMPLEMENTATION::ondemand::value &out) noexcept {
614 if (error()) { return error(); }
615 out = first;
616 return SUCCESS;
617}
618
619template<typename T> simdjson_inline simdjson_result<T> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get()
620#if SIMDJSON_SUPPORTS_CONCEPTS
621 noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::value>)
622#else
623 noexcept
624#endif
625{
626 if (error()) { return error(); }
627 return first.get<T>();
628}
629template<typename T> simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get(T &out)
630#if SIMDJSON_SUPPORTS_CONCEPTS
631 noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::value>)
632#else
633 noexcept
634#endif
635{
636 if (error()) { return error(); }
637 return first.get<T>(out);
638}
639
640template<> simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>() noexcept {
641 if (error()) { return error(); }
642 return std::move(first);
643}
644
646 if (error()) { return error(); }
647 return first.type();
648}
650 if (error()) { return error(); }
651 return first.is_scalar();
652}
653simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_string() noexcept {
654 if (error()) { return error(); }
655 return first.is_string();
656}
657simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_negative() noexcept {
658 if (error()) { return error(); }
659 return first.is_negative();
660}
661simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_integer() noexcept {
662 if (error()) { return error(); }
663 return first.is_integer();
664}
665simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number_type() noexcept {
666 if (error()) { return error(); }
667 return first.get_number_type();
668}
669simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number() noexcept {
670 if (error()) { return error(); }
671 return first.get_number();
672}
673#if SIMDJSON_EXCEPTIONS
674template <class T>
675simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator T() noexcept(false) {
676 if (error()) { throw simdjson_error(error()); }
677 return first.get<T>();
678}
679simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) {
680 if (error()) { throw simdjson_error(error()); }
681 return first;
682}
683simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false) {
684 if (error()) { throw simdjson_error(error()); }
685 return first;
686}
687simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator uint64_t() noexcept(false) {
688 if (error()) { throw simdjson_error(error()); }
689 return first;
690}
691simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator int64_t() noexcept(false) {
692 if (error()) { throw simdjson_error(error()); }
693 return first;
694}
695simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator double() noexcept(false) {
696 if (error()) { throw simdjson_error(error()); }
697 return first;
698}
699simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator std::string_view() noexcept(false) {
700 if (error()) { throw simdjson_error(error()); }
701 return first;
702}
703simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false) {
704 if (error()) { throw simdjson_error(error()); }
705 return first;
706}
707simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator bool() noexcept(false) {
708 if (error()) { throw simdjson_error(error()); }
709 return first;
710}
711#endif
712
714 if (error()) { return error(); }
715 return first.raw_json_token();
716}
717
719 if (error()) { return error(); }
720 return first.raw_json();
721}
722
724 if (error()) { return error(); }
725 return first.current_location();
726}
727
729 if (error()) { return error(); }
730 return first.current_depth();
731}
732
734 std::string_view json_pointer) noexcept {
735 if (error()) {
736 return error();
737 }
738 return first.at_pointer(json_pointer);
739}
740
741simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_path(
742 std::string_view json_path) noexcept {
743 if (error()) {
744 return error();
745 }
746 return first.at_path(json_path);
747}
748
749#if SIMDJSON_SUPPORTS_CONCEPTS
750template <typename Func>
751 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
752#else
753template <typename Func>
754#endif
755inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::for_each_at_path_with_wildcard(
756 std::string_view json_path, Func&& callback) noexcept {
757 if (error()) {
758 return error();
759 }
760 return first.for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
761}
762
763} // namespace simdjson
764
765#endif // SIMDJSON_GENERIC_ONDEMAND_VALUE_INL_H
static simdjson_inline simdjson_result< array > start(value_iterator &iter) noexcept
Begin array iteration.
Definition array-inl.h:61
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Consumes the array and returns a string_view instance corresponding to the array as represented in JS...
Definition array-inl.h:102
A forward-only JSON object field iterator.
Definition object.h:130
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition value.h:22
static simdjson_inline value start(const value_iterator &iter) noexcept
Start a value at the current position.
Definition value-inl.h:60
simdjson_inline simdjson_result< bool > is_null() noexcept
Checks if this JSON value is null.
Definition value-inl.h:160
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
Definition value-inl.h:354
simdjson_inline simdjson_result< float > get_float_in_string() noexcept
Cast this JSON value (inside string) to a float (binary32).
Definition value-inl.h:110
simdjson_inline simdjson_result< value > find_field(std::string_view key) noexcept
Look up a field by name on an object (order-sensitive).
Definition value-inl.h:276
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
Definition value.h:47
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
Definition value-inl.h:316
simdjson_inline simdjson_result< float > get_float() noexcept
Cast this JSON value to a float (binary32).
Definition value-inl.h:107
simdjson_inline simdjson_result< number_type > get_number_type() noexcept
Determine the number type (integer or floating-point number) as quickly as possible.
Definition value-inl.h:323
simdjson_inline simdjson_result< array > get_array() noexcept
Cast this JSON value to an array.
Definition value-inl.h:67
simdjson_inline std::string_view raw_json_token() noexcept
Get the raw JSON for this token.
Definition value-inl.h:330
simdjson_inline simdjson_result< object > get_object() noexcept
Cast this JSON value to an object.
Definition value-inl.h:70
simdjson_inline simdjson_result< uint8_t > get_uint8() noexcept
Cast this JSON value to an 8-bit unsigned integer.
Definition value-inl.h:137
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition value-inl.h:254
simdjson_inline simdjson_result< int64_t > get_int64_in_string() noexcept
Cast this JSON value (inside string) to a signed integer.
Definition value-inl.h:122
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
Definition value-inl.h:264
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
Definition value-inl.h:320
simdjson_inline simdjson_result< int8_t > get_int8() noexcept
Cast this JSON value to an 8-bit signed integer.
Definition value-inl.h:140
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
Definition value-inl.h:251
simdjson_inline simdjson_result< value > at_path(std::string_view at_path) noexcept
Get the value associated with the given JSONPath expression.
Definition value-inl.h:400
simdjson_inline simdjson_result< object > start_or_resume_object() noexcept
Get the object, starting or resuming it as necessary.
Definition value-inl.h:73
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
Definition value-inl.h:248
simdjson_inline simdjson_result< uint64_t > get_uint64_in_string() noexcept
Cast this JSON value (inside string) to a unsigned integer.
Definition value-inl.h:116
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
Definition value-inl.h:358
simdjson_inline simdjson_result< int16_t > get_int16() noexcept
Cast this JSON value to a 16-bit signed integer.
Definition value-inl.h:134
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
Definition value-inl.h:301
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Get a string_view pointing at this value in the JSON document.
Definition value-inl.h:334
static simdjson_inline value resume(const value_iterator &iter) noexcept
Resume a value.
Definition value-inl.h:63
simdjson_inline simdjson_result< double > get_double() noexcept
Cast this JSON value to a double.
Definition value-inl.h:101
simdjson_inline simdjson_result< std::string_view > get_string(bool allow_replacement=false) noexcept
Cast this JSON value to a string.
Definition value-inl.h:84
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
Definition value-inl.h:157
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
Definition value-inl.h:326
simdjson_inline simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition value-inl.h:382
simdjson_inline simdjson_result< std::string_view > get_wobbly_string() noexcept
Cast this JSON value to a "wobbly" string.
Definition value-inl.h:98
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
Definition value-inl.h:119
simdjson_inline value() noexcept=default
Create a new invalid value.
simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func &&callback) noexcept
Call the provided callback for each value matching the given JSONPath expression with wildcard suppor...
simdjson_inline simdjson_result< json_type > type() noexcept
Get the type of this JSON value.
Definition value-inl.h:297
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
Definition value-inl.h:104
simdjson_inline simdjson_result< raw_json_string > get_raw_json_string() noexcept
Cast this JSON value to a raw_json_string.
Definition value-inl.h:81
simdjson_inline simdjson_result< value > find_field_unordered(std::string_view key) noexcept
Look up a field by name on an object, without regard to key order.
Definition value-inl.h:283
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
Definition value-inl.h:271
simdjson_inline simdjson_result< uint16_t > get_uint16() noexcept
Cast this JSON value to a 16-bit unsigned integer.
Definition value-inl.h:131
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
Definition value-inl.h:113
simdjson_inline simdjson_result< int32_t > get_int32() noexcept
Cast this JSON value to a 32-bit signed integer.
Definition value-inl.h:128
simdjson_inline simdjson_result< uint32_t > get_uint32() noexcept
Cast this JSON value to a 32-bit unsigned integer.
Definition value-inl.h:125
simdjson_inline simdjson_result< bool > is_string() noexcept
Checks whether the value is a string.
Definition value-inl.h:308
@ object
A JSON object ( { "a": 1, "b" 2, ... } )
@ string
A JSON string ( "a" or "hello world\n" ...)
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
error_code
All possible errors returned by simdjson.
Definition error.h:19
@ DEPTH_ERROR
Your document exceeds the user-specified depth limitation.
Definition error.h:24
@ NO_SUCH_FIELD
JSON field not found in object.
Definition error.h:40
@ NUMBER_OUT_OF_RANGE
JSON number does not fit in 64 bits.
Definition error.h:38
@ SUCCESS
No error.
Definition error.h:20
@ INVALID_JSON_POINTER
Invalid JSON pointer syntax.
Definition error.h:42
The result of a simdjson operation that could fail.
Definition error.h:281
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition error-inl.h:163