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>
325 SIMDJSON_TRY(type().get(t));
328 return (*this).get_array().for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
330 return (*this).get_object().for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
342simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
343 SIMDJSON_IMPLEMENTATION::ondemand::value &&value
345 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(
346 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::value>(value)
350simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
353 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(error)
356simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_elements() &
noexcept {
357 if (error()) {
return error(); }
358 return first.count_elements();
360simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_fields() &
noexcept {
361 if (error()) {
return error(); }
362 return first.count_fields();
364simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at(
size_t index)
noexcept {
365 if (error()) {
return error(); }
366 return first.at(index);
368simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::begin() &
noexcept {
369 if (error()) {
return error(); }
370 return first.begin();
372simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::end() &
noexcept {
373 if (error()) {
return error(); }
378 if (error()) {
return error(); }
379 return first.find_field(key);
382 if (error()) {
return error(); }
383 return first.find_field(key);
387 if (error()) {
return error(); }
388 return first.find_field_unordered(key);
391 if (error()) {
return error(); }
392 return first.find_field_unordered(key);
395simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](std::string_view key)
noexcept {
396 if (error()) {
return error(); }
399simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](
const char *key)
noexcept {
400 if (error()) {
return error(); }
404simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_array() noexcept {
405 if (error()) {
return error(); }
406 return first.get_array();
408simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_object() noexcept {
409 if (error()) {
return error(); }
410 return first.get_object();
412simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64() noexcept {
413 if (error()) {
return error(); }
414 return first.get_uint64();
416simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64_in_string() noexcept {
417 if (error()) {
return error(); }
418 return first.get_uint64_in_string();
420simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64() noexcept {
421 if (error()) {
return error(); }
422 return first.get_int64();
424simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64_in_string() noexcept {
425 if (error()) {
return error(); }
426 return first.get_int64_in_string();
428simdjson_inline simdjson_result<uint32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint32() noexcept {
429 if (error()) {
return error(); }
430 return first.get_uint32();
432simdjson_inline simdjson_result<int32_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int32() noexcept {
433 if (error()) {
return error(); }
434 return first.get_int32();
436simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double() noexcept {
437 if (error()) {
return error(); }
438 return first.get_double();
440simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double_in_string() noexcept {
441 if (error()) {
return error(); }
442 return first.get_double_in_string();
444simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(
bool allow_replacement)
noexcept {
445 if (error()) {
return error(); }
446 return first.get_string(allow_replacement);
448template <
typename string_type>
449simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(string_type& receiver,
bool allow_replacement)
noexcept {
450 if (error()) {
return error(); }
451 return first.get_string(receiver, allow_replacement);
453simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_wobbly_string() noexcept {
454 if (error()) {
return error(); }
455 return first.get_wobbly_string();
457simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_raw_json_string() noexcept {
458 if (error()) {
return error(); }
459 return first.get_raw_json_string();
461simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_bool() noexcept {
462 if (error()) {
return error(); }
463 return first.get_bool();
465simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_null() noexcept {
466 if (error()) {
return error(); }
467 return first.is_null();
470template<> simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>(SIMDJSON_IMPLEMENTATION::ondemand::value &out)
noexcept {
471 if (error()) {
return error(); }
477 if (error()) {
return error(); }
478 return first.get<T>();
481 if (error()) {
return error(); }
482 return first.get<T>(out);
485template<> simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>() noexcept {
486 if (error()) {
return error(); }
487 return std::move(first);
491 if (error()) {
return error(); }
495 if (error()) {
return error(); }
496 return first.is_scalar();
498simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_string() noexcept {
499 if (error()) {
return error(); }
500 return first.is_string();
502simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_negative() noexcept {
503 if (error()) {
return error(); }
504 return first.is_negative();
506simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_integer() noexcept {
507 if (error()) {
return error(); }
508 return first.is_integer();
510simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number_type() noexcept {
511 if (error()) {
return error(); }
512 return first.get_number_type();
514simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number() noexcept {
515 if (error()) {
return error(); }
516 return first.get_number();
518#if SIMDJSON_EXCEPTIONS
520simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator T() noexcept(false) {
521 if (error()) {
throw simdjson_error(error()); }
522 return first.get<T>();
524simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) {
525 if (error()) {
throw simdjson_error(error()); }
528simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false) {
529 if (error()) {
throw simdjson_error(error()); }
532simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator uint64_t() noexcept(false) {
533 if (error()) {
throw simdjson_error(error()); }
536simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator int64_t() noexcept(false) {
537 if (error()) {
throw simdjson_error(error()); }
540simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator double() noexcept(false) {
541 if (error()) {
throw simdjson_error(error()); }
544simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator std::string_view() noexcept(false) {
545 if (error()) {
throw simdjson_error(error()); }
548simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false) {
549 if (error()) {
throw simdjson_error(error()); }
552simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator bool() noexcept(false) {
553 if (error()) {
throw simdjson_error(error()); }
559 if (error()) {
return error(); }
560 return first.raw_json_token();
564 if (error()) {
return error(); }
565 return first.raw_json();
569 if (error()) {
return error(); }
570 return first.current_location();
574 if (error()) {
return error(); }
575 return first.current_depth();
579 std::string_view json_pointer)
noexcept {
583 return first.at_pointer(json_pointer);
586simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_path(
587 std::string_view json_path)
noexcept {
591 return first.at_path(json_path);
594#if SIMDJSON_SUPPORTS_CONCEPTS
595template <
typename Func>
596 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
598template <
typename Func>
600inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::for_each_at_path_with_wildcard(
601 std::string_view json_path, Func&& callback)
noexcept {
605 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.
@ 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.