1#ifndef SIMDJSON_GENERIC_ONDEMAND_VALUE_INL_H
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"
18namespace SIMDJSON_IMPLEMENTATION {
36 return object::start(iter);
39 if (iter.at_start()) {
42 return object::resume(iter);
47 return iter.get_raw_json_string();
50 return iter.get_string(allow_replacement);
52template <
typename string_type>
54 return iter.get_string(receiver, allow_replacement);
57 return iter.get_wobbly_string();
60 return iter.get_double();
63 return iter.get_double_in_string();
66 return iter.get_uint64();
69 return iter.get_uint64_in_string();
72 return iter.get_int64();
75 return iter.get_int64_in_string();
81 return static_cast<uint32_t
>(result);
86 if (result > (std::numeric_limits<int32_t>::max)() || result < (std::numeric_limits<int32_t>::min)()) {
return NUMBER_OUT_OF_RANGE; }
87 return static_cast<int32_t
>(result);
90 return iter.get_bool();
93 return iter.is_null();
99template<> simdjson_inline simdjson_result<std::string_view>
value::get() noexcept {
return get_string(
false); }
100template<> simdjson_inline simdjson_result<number>
value::get() noexcept {
return get_number(); }
101template<> simdjson_inline simdjson_result<double>
value::get() noexcept {
return get_double(); }
102template<> simdjson_inline simdjson_result<uint64_t>
value::get() noexcept {
return get_uint64(); }
103template<> simdjson_inline simdjson_result<int64_t>
value::get() noexcept {
return get_int64(); }
104template<> simdjson_inline simdjson_result<uint32_t>
value::get() noexcept {
return get_uint32(); }
105template<> simdjson_inline simdjson_result<int32_t>
value::get() noexcept {
return get_int32(); }
106template<> simdjson_inline simdjson_result<bool>
value::get() noexcept {
return get_bool(); }
109template<> simdjson_warn_unused simdjson_inline
error_code value::get(array& out)
noexcept {
return get_array().get(out); }
110template<> simdjson_warn_unused simdjson_inline
error_code value::get(
object& out)
noexcept {
return get_object().get(out); }
111template<> simdjson_warn_unused simdjson_inline
error_code value::get(raw_json_string& out)
noexcept {
return get_raw_json_string().get(out); }
112template<> simdjson_warn_unused simdjson_inline
error_code value::get(std::string_view& out)
noexcept {
return get_string(
false).get(out); }
113template<> simdjson_warn_unused simdjson_inline
error_code value::get(number& out)
noexcept {
return get_number().get(out); }
114template<> simdjson_warn_unused simdjson_inline
error_code value::get(
double& out)
noexcept {
return get_double().get(out); }
115template<> simdjson_warn_unused simdjson_inline
error_code value::get(uint64_t& out)
noexcept {
return get_uint64().get(out); }
116template<> simdjson_warn_unused simdjson_inline
error_code value::get(int64_t& out)
noexcept {
return get_int64().get(out); }
117template<> simdjson_warn_unused simdjson_inline
error_code value::get(uint32_t& out)
noexcept {
return get_uint32().get(out); }
118template<> simdjson_warn_unused simdjson_inline
error_code value::get(int32_t& out)
noexcept {
return get_int32().get(out); }
119template<> simdjson_warn_unused simdjson_inline
error_code value::get(
bool& out)
noexcept {
return get_bool().get(out); }
121#if SIMDJSON_EXCEPTIONS
123simdjson_inline value::operator T() noexcept(false) {
126simdjson_inline value::operator
array() noexcept(false) {
129simdjson_inline value::operator
object() noexcept(false) {
132simdjson_inline value::operator uint64_t() noexcept(false) {
135simdjson_inline value::operator int64_t() noexcept(false) {
138simdjson_inline value::operator double() noexcept(false) {
141simdjson_inline value::operator std::string_view() noexcept(false) {
142 return get_string(
false);
145 return get_raw_json_string();
147simdjson_inline value::operator bool() noexcept(false) {
153 return get_array().begin();
160 auto a = get_array();
161 answer = a.count_elements();
165 iter.move_at_start();
170 auto a = get_object();
171 answer = a.count_fields();
172 iter.move_at_start();
176 auto a = get_array();
181 return start_or_resume_object().find_field(key);
184 return start_or_resume_object().find_field(key);
188 return start_or_resume_object().find_field_unordered(key);
191 return start_or_resume_object().find_field_unordered(key);
195 return start_or_resume_object()[key];
198 return start_or_resume_object()[key];
207 auto error =
type().get(this_type);
208 if(error) {
return error; }
214 auto error =
type().get(this_type);
215 if(error) {
return error; }
221 return iter.is_negative();
225 return iter.is_integer();
228 return iter.get_number_type();
231 return iter.get_number();
235 return std::string_view(
reinterpret_cast<const char*
>(iter.peek_start()), iter.peek_start_length());
244 ondemand::array
array;
251 return object.raw_json();
259 return iter.json_iter().current_location();
263 return iter.json_iter().depth();
266inline bool is_pointer_well_formed(std::string_view json_pointer)
noexcept {
267 if (simdjson_unlikely(json_pointer.empty())) {
270 if (simdjson_unlikely(json_pointer[0] !=
'/')) {
273 size_t escape = json_pointer.find(
'~');
274 if (escape == std::string_view::npos) {
277 if (escape == json_pointer.size() - 1) {
280 if (json_pointer[escape + 1] !=
'0' && json_pointer[escape + 1] !=
'1') {
288 SIMDJSON_TRY(type().get(t));
292 return (*this).get_array().at_pointer(json_pointer);
294 return (*this).get_object().at_pointer(json_pointer);
297 if (is_pointer_well_formed(json_pointer)) {
306 SIMDJSON_TRY(type().get(t));
309 return (*this).get_array().at_path(json_path);
311 return (*this).get_object().at_path(json_path);
317#if SIMDJSON_SUPPORTS_CONCEPTS
318template <
typename Func>
319 requires std::invocable<Func, value>
321template <
typename Func>
324 if (
size_t(iter.depth()) >= iter.json_iter().parser->max_depth()) {
return DEPTH_ERROR; }
326 SIMDJSON_TRY(type().get(t));
329 return (*this).get_array().for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
331 return (*this).get_object().for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
343simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
344 SIMDJSON_IMPLEMENTATION::ondemand::value &&value
346 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(
347 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::value>(value)
351simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
354 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(error)
357simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_elements() &
noexcept {
358 if (error()) {
return error(); }
359 return first.count_elements();
361simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_fields() &
noexcept {
362 if (error()) {
return error(); }
363 return first.count_fields();
365simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at(
size_t index)
noexcept {
366 if (error()) {
return error(); }
367 return first.at(index);
369simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::begin() &
noexcept {
370 if (error()) {
return error(); }
371 return first.begin();
373simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::end() &
noexcept {
374 if (error()) {
return error(); }
379 if (error()) {
return error(); }
380 return first.find_field(key);
383 if (error()) {
return error(); }
384 return first.find_field(key);
388 if (error()) {
return error(); }
389 return first.find_field_unordered(key);
392 if (error()) {
return error(); }
393 return first.find_field_unordered(key);
396simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](std::string_view key)
noexcept {
397 if (error()) {
return error(); }
400simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](
const char *key)
noexcept {
401 if (error()) {
return error(); }
405simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_array() noexcept {
406 if (error()) {
return error(); }
407 return first.get_array();
409simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_object() noexcept {
410 if (error()) {
return error(); }
411 return first.get_object();
413simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64() noexcept {
414 if (error()) {
return error(); }
415 return first.get_uint64();
417simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64_in_string() noexcept {
418 if (error()) {
return error(); }
419 return first.get_uint64_in_string();
421simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64() noexcept {
422 if (error()) {
return error(); }
423 return first.get_int64();
425simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64_in_string() noexcept {
426 if (error()) {
return error(); }
427 return first.get_int64_in_string();
429simdjson_inline simdjson_result<uint32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint32() noexcept {
430 if (error()) {
return error(); }
431 return first.get_uint32();
433simdjson_inline simdjson_result<int32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int32() noexcept {
434 if (error()) {
return error(); }
435 return first.get_int32();
437simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double() noexcept {
438 if (error()) {
return error(); }
439 return first.get_double();
441simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double_in_string() noexcept {
442 if (error()) {
return error(); }
443 return first.get_double_in_string();
445simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(
bool allow_replacement)
noexcept {
446 if (error()) {
return error(); }
447 return first.get_string(allow_replacement);
449template <
typename string_type>
450simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(string_type& receiver,
bool allow_replacement)
noexcept {
451 if (error()) {
return error(); }
452 return first.get_string(receiver, allow_replacement);
454simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_wobbly_string() noexcept {
455 if (error()) {
return error(); }
456 return first.get_wobbly_string();
458simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_raw_json_string() noexcept {
459 if (error()) {
return error(); }
460 return first.get_raw_json_string();
462simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_bool() noexcept {
463 if (error()) {
return error(); }
464 return first.get_bool();
466simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_null() noexcept {
467 if (error()) {
return error(); }
468 return first.is_null();
471template<> simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>(SIMDJSON_IMPLEMENTATION::ondemand::value &out)
noexcept {
472 if (error()) {
return error(); }
478 if (error()) {
return error(); }
479 return first.get<T>();
482 if (error()) {
return error(); }
483 return first.get<T>(out);
486template<> simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>() noexcept {
487 if (error()) {
return error(); }
488 return std::move(first);
492 if (error()) {
return error(); }
496 if (error()) {
return error(); }
497 return first.is_scalar();
499simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_string() noexcept {
500 if (error()) {
return error(); }
501 return first.is_string();
503simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_negative() noexcept {
504 if (error()) {
return error(); }
505 return first.is_negative();
507simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_integer() noexcept {
508 if (error()) {
return error(); }
509 return first.is_integer();
511simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number_type() noexcept {
512 if (error()) {
return error(); }
513 return first.get_number_type();
515simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number() noexcept {
516 if (error()) {
return error(); }
517 return first.get_number();
519#if SIMDJSON_EXCEPTIONS
521simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator T() noexcept(false) {
522 if (error()) {
throw simdjson_error(error()); }
523 return first.get<T>();
525simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) {
526 if (error()) {
throw simdjson_error(error()); }
529simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false) {
530 if (error()) {
throw simdjson_error(error()); }
533simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator uint64_t() noexcept(false) {
534 if (error()) {
throw simdjson_error(error()); }
537simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator int64_t() noexcept(false) {
538 if (error()) {
throw simdjson_error(error()); }
541simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator double() noexcept(false) {
542 if (error()) {
throw simdjson_error(error()); }
545simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator std::string_view() noexcept(false) {
546 if (error()) {
throw simdjson_error(error()); }
549simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false) {
550 if (error()) {
throw simdjson_error(error()); }
553simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator bool() noexcept(false) {
554 if (error()) {
throw simdjson_error(error()); }
560 if (error()) {
return error(); }
561 return first.raw_json_token();
565 if (error()) {
return error(); }
566 return first.raw_json();
570 if (error()) {
return error(); }
571 return first.current_location();
575 if (error()) {
return error(); }
576 return first.current_depth();
580 std::string_view json_pointer)
noexcept {
584 return first.at_pointer(json_pointer);
587simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_path(
588 std::string_view json_path)
noexcept {
592 return first.at_path(json_path);
595#if SIMDJSON_SUPPORTS_CONCEPTS
596template <
typename Func>
597 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
599template <
typename Func>
601inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::for_each_at_path_with_wildcard(
602 std::string_view json_path, Func&& callback)
noexcept {
606 return first.for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
A forward-only JSON array.
static simdjson_inline simdjson_result< array > start(value_iterator &iter) noexcept
Begin array iteration.
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...
A forward-only JSON object field iterator.
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
static simdjson_inline value start(const value_iterator &iter) noexcept
Start a value at the current position.
simdjson_inline simdjson_result< bool > is_null() noexcept
Checks if this JSON value is null.
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
simdjson_inline simdjson_result< value > find_field(std::string_view key) noexcept
Look up a field by name on an object (order-sensitive).
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
simdjson_inline simdjson_result< number_type > get_number_type() noexcept
Determine the number type (integer or floating-point number) as quickly as possible.
simdjson_inline simdjson_result< array > get_array() noexcept
Cast this JSON value to an array.
simdjson_inline std::string_view raw_json_token() noexcept
Get the raw JSON for this token.
simdjson_inline simdjson_result< object > get_object() noexcept
Cast this JSON value to an object.
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
simdjson_inline simdjson_result< int64_t > get_int64_in_string() noexcept
Cast this JSON value (inside string) to a signed integer.
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
simdjson_inline simdjson_result< value > at_path(std::string_view at_path) noexcept
Get the value associated with the given JSONPath expression.
simdjson_inline simdjson_result< object > start_or_resume_object() noexcept
Get the object, starting or resuming it as necessary.
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
simdjson_inline simdjson_result< uint64_t > get_uint64_in_string() noexcept
Cast this JSON value (inside string) to a unsigned integer.
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Get a string_view pointing at this value in the JSON document.
static simdjson_inline value resume(const value_iterator &iter) noexcept
Resume a value.
simdjson_inline simdjson_result< double > get_double() noexcept
Cast this JSON value to a double.
simdjson_inline simdjson_result< std::string_view > get_string(bool allow_replacement=false) noexcept
Cast this JSON value to a string.
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
simdjson_inline simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
simdjson_inline simdjson_result< std::string_view > get_wobbly_string() noexcept
Cast this JSON value to a "wobbly" string.
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
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.
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
simdjson_inline simdjson_result< raw_json_string > get_raw_json_string() noexcept
Cast this JSON value to a raw_json_string.
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.
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
simdjson_inline simdjson_result< int32_t > get_int32() noexcept
Cast this JSON value to a 32-bit signed integer.
simdjson_inline simdjson_result< uint32_t > get_uint32() noexcept
Cast this JSON value to a 32-bit unsigned integer.
simdjson_inline simdjson_result< bool > is_string() noexcept
Checks whether the value is a string.
json_type
The type of a JSON value.
@ object
A JSON object ( { "a": 1, "b" 2, ... } )
@ string
A JSON string ( "a" or "hello world\n" ...)
@ array
A JSON array ( [ 1, 2, 3 ... ] )
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ DEPTH_ERROR
Your document exceeds the user-specified depth limitation.
@ NO_SUCH_FIELD
JSON field not found in object.
@ NUMBER_OUT_OF_RANGE
JSON number does not fit in 64 bits.
@ INVALID_JSON_POINTER
Invalid JSON pointer syntax.
The result of a simdjson operation that could fail.
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.