simdjson  3.3.0
Ridiculously Fast JSON
value.h
1 #ifndef SIMDJSON_GENERIC_ONDEMAND_VALUE_H
2 
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_VALUE_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/implementation_simdjson_result_base.h"
7 #include "simdjson/generic/ondemand/value_iterator.h"
8 #endif // SIMDJSON_CONDITIONAL_INCLUDE
9 
10 namespace simdjson {
11 namespace SIMDJSON_IMPLEMENTATION {
12 namespace ondemand {
13 
18 class value {
19 public:
25  simdjson_inline value() noexcept = default;
26 
38  template<typename T> simdjson_inline simdjson_result<T> get() noexcept {
39  // Unless the simdjson library provides an inline implementation, calling this method should
40  // immediately fail.
41  static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library. "
42  "The supported types are ondemand::object, ondemand::array, raw_json_string, std::string_view, uint64_t, "
43  "int64_t, double, and bool. We recommend you use get_double(), get_bool(), get_uint64(), get_int64(), "
44  " get_object(), get_array(), get_raw_json_string(), or get_string() instead of the get template.");
45  }
46 
56  template<typename T> simdjson_inline error_code get(T &out) noexcept;
57 
64  simdjson_inline simdjson_result<array> get_array() noexcept;
65 
72  simdjson_inline simdjson_result<object> get_object() noexcept;
73 
80  simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
81 
88  simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
89 
96  simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
97 
104  simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
105 
112  simdjson_inline simdjson_result<double> get_double() noexcept;
113 
120  simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
121 
136  simdjson_inline simdjson_result<std::string_view> get_string(bool allow_replacement = false) noexcept;
137 
151  simdjson_inline error_code get_string(std::string& receiver, bool allow_replacement = false) noexcept;
152 
166  simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
175  simdjson_inline simdjson_result<raw_json_string> get_raw_json_string() noexcept;
176 
183  simdjson_inline simdjson_result<bool> get_bool() noexcept;
184 
193  simdjson_inline simdjson_result<bool> is_null() noexcept;
194 
195 #if SIMDJSON_EXCEPTIONS
202  simdjson_inline operator array() noexcept(false);
209  simdjson_inline operator object() noexcept(false);
216  simdjson_inline operator uint64_t() noexcept(false);
223  simdjson_inline operator int64_t() noexcept(false);
230  simdjson_inline operator double() noexcept(false);
242  simdjson_inline operator std::string_view() noexcept(false);
251  simdjson_inline operator raw_json_string() noexcept(false);
258  simdjson_inline operator bool() noexcept(false);
259 #endif
260 
268  simdjson_inline simdjson_result<array_iterator> begin() & noexcept;
274  simdjson_inline simdjson_result<array_iterator> end() & noexcept;
289  simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
307  simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
315  simdjson_inline simdjson_result<value> at(size_t index) noexcept;
338  simdjson_inline simdjson_result<value> find_field(std::string_view key) noexcept;
340  simdjson_inline simdjson_result<value> find_field(const char *key) noexcept;
341 
364  simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) noexcept;
366  simdjson_inline simdjson_result<value> find_field_unordered(const char *key) noexcept;
368  simdjson_inline simdjson_result<value> operator[](std::string_view key) noexcept;
370  simdjson_inline simdjson_result<value> operator[](const char *key) noexcept;
371 
385  simdjson_inline simdjson_result<json_type> type() noexcept;
386 
394  simdjson_inline simdjson_result<bool> is_scalar() noexcept;
395 
401  simdjson_inline bool is_negative() noexcept;
415  simdjson_inline simdjson_result<bool> is_integer() noexcept;
438  simdjson_inline simdjson_result<number_type> get_number_type() noexcept;
439 
470  simdjson_warn_unused simdjson_inline simdjson_result<number> get_number() noexcept;
471 
472 
496  simdjson_inline std::string_view raw_json_token() noexcept;
497 
501  simdjson_inline simdjson_result<const char *> current_location() noexcept;
502 
512  simdjson_inline int32_t current_depth() const noexcept;
513 
556  simdjson_inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
557 
558 protected:
562  simdjson_inline value(const value_iterator &iter) noexcept;
563 
567  simdjson_inline void skip() noexcept;
568 
574  static simdjson_inline value start(const value_iterator &iter) noexcept;
575 
579  static simdjson_inline value resume(const value_iterator &iter) noexcept;
580 
584  simdjson_inline simdjson_result<object> start_or_resume_object() noexcept;
585 
586  // simdjson_inline void log_value(const char *type) const noexcept;
587  // simdjson_inline void log_error(const char *message) const noexcept;
588 
589  value_iterator iter{};
590 
591  friend class document;
592  friend class array_iterator;
593  friend class field;
594  friend class object;
595  friend struct simdjson_result<value>;
596  friend struct simdjson_result<field>;
597 };
598 
599 } // namespace ondemand
600 } // namespace SIMDJSON_IMPLEMENTATION
601 } // namespace simdjson
602 
603 namespace simdjson {
604 
605 template<>
606 struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value> {
607 public:
609  simdjson_inline simdjson_result(error_code error) noexcept;
610  simdjson_inline simdjson_result() noexcept = default;
611 
612  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> get_array() noexcept;
613  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> get_object() noexcept;
614 
615  simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
616  simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
617  simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
618  simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
619  simdjson_inline simdjson_result<double> get_double() noexcept;
620  simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
621  simdjson_inline simdjson_result<std::string_view> get_string(bool allow_replacement = false) noexcept;
622  simdjson_inline error_code get_string(std::string& receiver, bool allow_replacement = false) noexcept;
623  simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
624  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> get_raw_json_string() noexcept;
625  simdjson_inline simdjson_result<bool> get_bool() noexcept;
626  simdjson_inline simdjson_result<bool> is_null() noexcept;
627 
628  template<typename T> simdjson_inline simdjson_result<T> get() noexcept;
629 
630  template<typename T> simdjson_inline error_code get(T &out) noexcept;
631 
632 #if SIMDJSON_EXCEPTIONS
633  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false);
634  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false);
635  simdjson_inline operator uint64_t() noexcept(false);
636  simdjson_inline operator int64_t() noexcept(false);
637  simdjson_inline operator double() noexcept(false);
638  simdjson_inline operator std::string_view() noexcept(false);
639  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false);
640  simdjson_inline operator bool() noexcept(false);
641 #endif
642  simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
643  simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
644  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
647 
668  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) noexcept;
670  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(const char *key) noexcept;
671 
691  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) noexcept;
693  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(const char *key) noexcept;
695  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) noexcept;
697  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](const char *key) noexcept;
698 
707  simdjson_inline simdjson_result<bool> is_scalar() noexcept;
708  simdjson_inline simdjson_result<bool> is_negative() noexcept;
709  simdjson_inline simdjson_result<bool> is_integer() noexcept;
710  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> get_number_type() noexcept;
711  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> get_number() noexcept;
712 
714  simdjson_inline simdjson_result<std::string_view> raw_json_token() noexcept;
715 
717  simdjson_inline simdjson_result<const char *> current_location() noexcept;
719  simdjson_inline simdjson_result<int32_t> current_depth() const noexcept;
720  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
721 };
722 
723 } // namespace simdjson
724 
725 #endif // SIMDJSON_GENERIC_ONDEMAND_VALUE_H
A JSON field (key/value pair) in an object.
Definition: field.h:22
A forward-only JSON object field iterator.
Definition: object.h:17
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition: value.h:18
static simdjson_inline value start(const value_iterator &iter) noexcept
Start a value at the current position.
Definition: value-inl.h:23
simdjson_inline simdjson_result< bool > is_null() noexcept
Checks if this JSON value is null.
Definition: value-inl.h:77
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
Definition: value-inl.h:199
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:149
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
Definition: value-inl.h:181
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:188
simdjson_inline simdjson_result< array > get_array() noexcept
Cast this JSON value to an array.
Definition: value-inl.h:30
simdjson_inline std::string_view raw_json_token() noexcept
Get the raw JSON for this token.
Definition: value-inl.h:195
simdjson_inline simdjson_result< object > get_object() noexcept
Cast this JSON value to an object.
Definition: value-inl.h:33
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition: value-inl.h:127
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:71
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:137
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
Definition: value-inl.h:185
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
Definition: value-inl.h:124
simdjson_inline simdjson_result< object > start_or_resume_object() noexcept
Get the object, starting or resuming it as necessary.
Definition: value-inl.h:36
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
Definition: value-inl.h:121
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:65
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
Definition: value-inl.h:203
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
Definition: value-inl.h:174
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
Definition: value.h:38
static simdjson_inline value resume(const value_iterator &iter) noexcept
Resume a value.
Definition: value-inl.h:26
simdjson_inline simdjson_result< double > get_double() noexcept
Cast this JSON value to a double.
Definition: value-inl.h:56
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:47
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
Definition: value-inl.h:74
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
Definition: value-inl.h:191
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:207
simdjson_inline simdjson_result< std::string_view > get_wobbly_string() noexcept
Cast this JSON value to a "wobbly" string.
Definition: value-inl.h:53
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
Definition: value-inl.h:68
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.
Definition: value-inl.h:170
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
Definition: value-inl.h:59
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:44
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:156
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
Definition: value-inl.h:144
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
Definition: value-inl.h:62
simdjson_inline void skip() noexcept
Skip this value, allowing iteration to continue.
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
A type representing a JSON number.
Definition: json_type.h:32
The result of a simdjson operation that could fail.
Definition: error.h:207
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:131
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition: error-inl.h:126
simdjson_inline T & value() &noexcept(false)
Get the result value.