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"
16namespace SIMDJSON_IMPLEMENTATION {
34 return object::start(iter);
37 if (iter.at_start()) {
40 return object::resume(iter);
45 return iter.get_raw_json_string();
48 return iter.get_string(allow_replacement);
50template <
typename string_type>
52 return iter.get_string(receiver, allow_replacement);
55 return iter.get_wobbly_string();
58 return iter.get_double();
61 return iter.get_double_in_string();
64 return iter.get_uint64();
67 return iter.get_uint64_in_string();
70 return iter.get_int64();
73 return iter.get_int64_in_string();
76 return iter.get_bool();
79 return iter.is_null();
85template<> simdjson_inline simdjson_result<std::string_view>
value::get() noexcept {
return get_string(
false); }
88template<> simdjson_inline simdjson_result<uint64_t>
value::get() noexcept {
return get_uint64(); }
89template<> simdjson_inline simdjson_result<int64_t>
value::get() noexcept {
return get_int64(); }
90template<> simdjson_inline simdjson_result<bool>
value::get() noexcept {
return get_bool(); }
93template<> simdjson_warn_unused simdjson_inline
error_code value::get(array& out)
noexcept {
return get_array().get(out); }
94template<> simdjson_warn_unused simdjson_inline
error_code value::get(
object& out)
noexcept {
return get_object().get(out); }
95template<> simdjson_warn_unused simdjson_inline
error_code value::get(raw_json_string& out)
noexcept {
return get_raw_json_string().get(out); }
96template<> simdjson_warn_unused simdjson_inline
error_code value::get(std::string_view& out)
noexcept {
return get_string(
false).get(out); }
97template<> simdjson_warn_unused simdjson_inline
error_code value::get(number& out)
noexcept {
return get_number().get(out); }
98template<> simdjson_warn_unused simdjson_inline
error_code value::get(
double& out)
noexcept {
return get_double().get(out); }
99template<> simdjson_warn_unused simdjson_inline
error_code value::get(uint64_t& out)
noexcept {
return get_uint64().get(out); }
100template<> simdjson_warn_unused simdjson_inline
error_code value::get(int64_t& out)
noexcept {
return get_int64().get(out); }
101template<> simdjson_warn_unused simdjson_inline
error_code value::get(
bool& out)
noexcept {
return get_bool().get(out); }
103#if SIMDJSON_EXCEPTIONS
105simdjson_inline value::operator T() noexcept(false) {
108simdjson_inline value::operator
array() noexcept(false) {
111simdjson_inline value::operator
object() noexcept(false) {
114simdjson_inline value::operator uint64_t() noexcept(false) {
117simdjson_inline value::operator int64_t() noexcept(false) {
120simdjson_inline value::operator double() noexcept(false) {
123simdjson_inline value::operator std::string_view() noexcept(false) {
124 return get_string(
false);
127 return get_raw_json_string();
129simdjson_inline value::operator bool() noexcept(false) {
135 return get_array().begin();
142 auto a = get_array();
143 answer = a.count_elements();
147 iter.move_at_start();
152 auto a = get_object();
153 answer = a.count_fields();
154 iter.move_at_start();
158 auto a = get_array();
163 return start_or_resume_object().find_field(key);
166 return start_or_resume_object().find_field(key);
170 return start_or_resume_object().find_field_unordered(key);
173 return start_or_resume_object().find_field_unordered(key);
177 return start_or_resume_object()[key];
180 return start_or_resume_object()[key];
189 auto error =
type().get(this_type);
190 if(error) {
return error; }
196 auto error =
type().get(this_type);
197 if(error) {
return error; }
203 return iter.is_negative();
207 return iter.is_integer();
210 return iter.get_number_type();
213 return iter.get_number();
217 return std::string_view(
reinterpret_cast<const char*
>(iter.peek_start()), iter.peek_start_length());
226 ondemand::array
array;
233 return object.raw_json();
241 return iter.json_iter().current_location();
245 return iter.json_iter().depth();
248inline bool is_pointer_well_formed(std::string_view json_pointer)
noexcept {
249 if (simdjson_unlikely(json_pointer.empty())) {
252 if (simdjson_unlikely(json_pointer[0] !=
'/')) {
255 size_t escape = json_pointer.find(
'~');
256 if (escape == std::string_view::npos) {
259 if (escape == json_pointer.size() - 1) {
262 if (json_pointer[escape + 1] !=
'0' && json_pointer[escape + 1] !=
'1') {
270 SIMDJSON_TRY(type().get(t));
274 return (*this).get_array().at_pointer(json_pointer);
276 return (*this).get_object().at_pointer(json_pointer);
279 if (is_pointer_well_formed(json_pointer)) {
288 SIMDJSON_TRY(type().get(t));
291 return (*this).get_array().at_path(json_path);
293 return (*this).get_object().at_path(json_path);
301 SIMDJSON_TRY(type().get(t));
304 return (*this).get_array().at_path_with_wildcard(json_path);
306 return (*this).get_object().at_path_with_wildcard(json_path);
318simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
319 SIMDJSON_IMPLEMENTATION::ondemand::value &&value
321 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(
322 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::value>(value)
326simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
329 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(error)
332simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_elements() &
noexcept {
333 if (error()) {
return error(); }
334 return first.count_elements();
336simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_fields() &
noexcept {
337 if (error()) {
return error(); }
338 return first.count_fields();
340simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at(
size_t index)
noexcept {
341 if (error()) {
return error(); }
342 return first.at(index);
344simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::begin() &
noexcept {
345 if (error()) {
return error(); }
346 return first.begin();
348simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::end() &
noexcept {
349 if (error()) {
return error(); }
354 if (error()) {
return error(); }
355 return first.find_field(key);
358 if (error()) {
return error(); }
359 return first.find_field(key);
363 if (error()) {
return error(); }
364 return first.find_field_unordered(key);
367 if (error()) {
return error(); }
368 return first.find_field_unordered(key);
371simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](std::string_view key)
noexcept {
372 if (error()) {
return error(); }
375simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](
const char *key)
noexcept {
376 if (error()) {
return error(); }
380simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_array() noexcept {
381 if (error()) {
return error(); }
382 return first.get_array();
384simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_object() noexcept {
385 if (error()) {
return error(); }
386 return first.get_object();
388simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64() noexcept {
389 if (error()) {
return error(); }
390 return first.get_uint64();
392simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64_in_string() noexcept {
393 if (error()) {
return error(); }
394 return first.get_uint64_in_string();
396simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64() noexcept {
397 if (error()) {
return error(); }
398 return first.get_int64();
400simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64_in_string() noexcept {
401 if (error()) {
return error(); }
402 return first.get_int64_in_string();
404simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double() noexcept {
405 if (error()) {
return error(); }
406 return first.get_double();
408simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double_in_string() noexcept {
409 if (error()) {
return error(); }
410 return first.get_double_in_string();
412simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(
bool allow_replacement)
noexcept {
413 if (error()) {
return error(); }
414 return first.get_string(allow_replacement);
416template <
typename string_type>
417simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(string_type& receiver,
bool allow_replacement)
noexcept {
418 if (error()) {
return error(); }
419 return first.get_string(receiver, allow_replacement);
421simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_wobbly_string() noexcept {
422 if (error()) {
return error(); }
423 return first.get_wobbly_string();
425simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_raw_json_string() noexcept {
426 if (error()) {
return error(); }
427 return first.get_raw_json_string();
429simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_bool() noexcept {
430 if (error()) {
return error(); }
431 return first.get_bool();
433simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_null() noexcept {
434 if (error()) {
return error(); }
435 return first.is_null();
438template<> simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>(SIMDJSON_IMPLEMENTATION::ondemand::value &out)
noexcept {
439 if (error()) {
return error(); }
445 if (error()) {
return error(); }
446 return first.get<T>();
449 if (error()) {
return error(); }
450 return first.get<T>(out);
453template<> simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>() noexcept {
454 if (error()) {
return error(); }
455 return std::move(first);
459 if (error()) {
return error(); }
463 if (error()) {
return error(); }
464 return first.is_scalar();
466simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_string() noexcept {
467 if (error()) {
return error(); }
468 return first.is_string();
470simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_negative() noexcept {
471 if (error()) {
return error(); }
472 return first.is_negative();
474simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_integer() noexcept {
475 if (error()) {
return error(); }
476 return first.is_integer();
478simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number_type() noexcept {
479 if (error()) {
return error(); }
480 return first.get_number_type();
482simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number() noexcept {
483 if (error()) {
return error(); }
484 return first.get_number();
486#if SIMDJSON_EXCEPTIONS
488simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator T() noexcept(false) {
489 if (error()) {
throw simdjson_error(error()); }
490 return first.get<T>();
492simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) {
493 if (error()) {
throw simdjson_error(error()); }
496simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false) {
497 if (error()) {
throw simdjson_error(error()); }
500simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator uint64_t() noexcept(false) {
501 if (error()) {
throw simdjson_error(error()); }
504simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator int64_t() noexcept(false) {
505 if (error()) {
throw simdjson_error(error()); }
508simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator double() noexcept(false) {
509 if (error()) {
throw simdjson_error(error()); }
512simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator std::string_view() noexcept(false) {
513 if (error()) {
throw simdjson_error(error()); }
516simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false) {
517 if (error()) {
throw simdjson_error(error()); }
520simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator bool() noexcept(false) {
521 if (error()) {
throw simdjson_error(error()); }
527 if (error()) {
return error(); }
528 return first.raw_json_token();
532 if (error()) {
return error(); }
533 return first.raw_json();
537 if (error()) {
return error(); }
538 return first.current_location();
542 if (error()) {
return error(); }
543 return first.current_depth();
547 std::string_view json_pointer)
noexcept {
551 return first.at_pointer(json_pointer);
554simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_path(
555 std::string_view json_path)
noexcept {
559 return first.at_path(json_path);
562inline simdjson_result<std::vector<SIMDJSON_IMPLEMENTATION::ondemand::value>> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_path_with_wildcard(
563 std::string_view json_path)
noexcept {
567 return first.at_path_with_wildcard(json_path);
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 simdjson_result< std::vector< value > > at_path_with_wildcard(std::string_view json_path) noexcept
Get all values matching the given JSONPath expression with wildcard support.
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 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< 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.
@ 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.