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"
16 namespace 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);
50 template <
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();
85 template<> simdjson_inline simdjson_result<std::string_view>
value::get() noexcept {
return get_string(
false); }
86 template<> simdjson_inline simdjson_result<number>
value::get() noexcept {
return get_number(); }
87 template<> simdjson_inline simdjson_result<double>
value::get() noexcept {
return get_double(); }
88 template<> simdjson_inline simdjson_result<uint64_t>
value::get() noexcept {
return get_uint64(); }
89 template<> simdjson_inline simdjson_result<int64_t>
value::get() noexcept {
return get_int64(); }
90 template<> simdjson_inline simdjson_result<bool>
value::get() noexcept {
return get_bool(); }
93 template<> simdjson_inline
error_code value::get(array& out) noexcept {
return get_array().get(out); }
94 template<> simdjson_inline
error_code value::get(
object& out) noexcept {
return get_object().get(out); }
95 template<> simdjson_inline
error_code value::get(raw_json_string& out) noexcept {
return get_raw_json_string().get(out); }
96 template<> simdjson_inline
error_code value::get(std::string_view& out) noexcept {
return get_string(
false).get(out); }
97 template<> simdjson_inline
error_code value::get(number& out) noexcept {
return get_number().get(out); }
98 template<> simdjson_inline
error_code value::get(
double& out) noexcept {
return get_double().get(out); }
99 template<> simdjson_inline
error_code value::get(uint64_t& out) noexcept {
return get_uint64().get(out); }
100 template<> simdjson_inline
error_code value::get(int64_t& out) noexcept {
return get_int64().get(out); }
101 template<> simdjson_inline
error_code value::get(
bool& out) noexcept {
return get_bool().get(out); }
103 #if SIMDJSON_EXCEPTIONS
105 simdjson_inline value::operator T() noexcept(false) {
108 simdjson_inline value::operator
array() noexcept(false) {
111 simdjson_inline value::operator
object() noexcept(false) {
114 simdjson_inline value::operator uint64_t() noexcept(false) {
117 simdjson_inline value::operator int64_t() noexcept(false) {
120 simdjson_inline value::operator double() noexcept(false) {
123 simdjson_inline value::operator std::string_view() noexcept(false) {
124 return get_string(
false);
127 return get_raw_json_string();
129 simdjson_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();
248 inline 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);
305 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
306 SIMDJSON_IMPLEMENTATION::ondemand::value &&value
308 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(
309 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::value>(value)
313 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::simdjson_result(
316 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value>(error)
319 simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_elements() & noexcept {
320 if (error()) {
return error(); }
321 return first.count_elements();
323 simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::count_fields() & noexcept {
324 if (error()) {
return error(); }
325 return first.count_fields();
327 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at(
size_t index) noexcept {
328 if (error()) {
return error(); }
329 return first.at(index);
331 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::begin() & noexcept {
332 if (error()) {
return error(); }
333 return first.begin();
335 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::end() & noexcept {
336 if (error()) {
return error(); }
341 if (error()) {
return error(); }
342 return first.find_field(key);
345 if (error()) {
return error(); }
346 return first.find_field(key);
350 if (error()) {
return error(); }
351 return first.find_field_unordered(key);
354 if (error()) {
return error(); }
355 return first.find_field_unordered(key);
358 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](std::string_view key) noexcept {
359 if (error()) {
return error(); }
362 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator[](
const char *key) noexcept {
363 if (error()) {
return error(); }
367 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_array() noexcept {
369 return first.get_array();
371 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_object() noexcept {
373 return first.get_object();
375 simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64() noexcept {
377 return first.get_uint64();
379 simdjson_inline simdjson_result<uint64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_uint64_in_string() noexcept {
381 return first.get_uint64_in_string();
383 simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64() noexcept {
385 return first.get_int64();
387 simdjson_inline simdjson_result<int64_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_int64_in_string() noexcept {
389 return first.get_int64_in_string();
391 simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double() noexcept {
393 return first.get_double();
395 simdjson_inline simdjson_result<double> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_double_in_string() noexcept {
397 return first.get_double_in_string();
399 simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(
bool allow_replacement) noexcept {
400 if (error()) {
return error(); }
401 return first.get_string(allow_replacement);
403 template <
typename string_type>
404 simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_string(string_type& receiver,
bool allow_replacement) noexcept {
405 if (error()) {
return error(); }
406 return first.get_string(receiver, allow_replacement);
408 simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_wobbly_string() noexcept {
410 return first.get_wobbly_string();
412 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_raw_json_string() noexcept {
414 return first.get_raw_json_string();
416 simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_bool() noexcept {
418 return first.get_bool();
420 simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_null() noexcept {
422 return first.is_null();
425 template<> simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>(SIMDJSON_IMPLEMENTATION::ondemand::value &out) noexcept {
426 if (error()) {
return error(); }
433 return first.get<T>();
436 if (error()) {
return error(); }
437 return first.get<T>(out);
440 template<> simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get<SIMDJSON_IMPLEMENTATION::ondemand::value>() noexcept {
441 if (error()) {
return error(); }
442 return std::move(first);
451 return first.is_scalar();
453 simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_string() noexcept {
455 return first.is_string();
457 simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_negative() noexcept {
459 return first.is_negative();
461 simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::is_integer() noexcept {
463 return first.is_integer();
465 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number_type() noexcept {
467 return first.get_number_type();
469 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::get_number() noexcept {
471 return first.get_number();
473 #if SIMDJSON_EXCEPTIONS
475 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator T() noexcept(false) {
476 if (error()) {
throw simdjson_error(error()); }
477 return first.get<T>();
479 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) {
480 if (error()) {
throw simdjson_error(error()); }
483 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false) {
484 if (error()) {
throw simdjson_error(error()); }
487 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator uint64_t() noexcept(false) {
488 if (error()) {
throw simdjson_error(error()); }
491 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator int64_t() noexcept(false) {
492 if (error()) {
throw simdjson_error(error()); }
495 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator double() noexcept(false) {
496 if (error()) {
throw simdjson_error(error()); }
499 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator std::string_view() noexcept(false) {
500 if (error()) {
throw simdjson_error(error()); }
503 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false) {
504 if (error()) {
throw simdjson_error(error()); }
507 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::operator bool() noexcept(false) {
508 if (error()) {
throw simdjson_error(error()); }
515 return first.raw_json_token();
520 return first.raw_json();
525 return first.current_location();
530 return first.current_depth();
534 std::string_view json_pointer) noexcept {
538 return first.at_pointer(json_pointer);
541 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value>::at_path(
542 std::string_view json_path) noexcept {
546 return first.at_path(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 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< T > get() noexcept
Get this value as the given type.
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_inline error_code error() const noexcept
The error.
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.