simdjson  3.1.1
Ridiculously Fast JSON
value.h
1 #include "simdjson/error.h"
2 
3 namespace simdjson {
4 namespace SIMDJSON_IMPLEMENTATION {
5 namespace ondemand {
6 
7 class array;
8 class document;
9 class field;
10 class object;
11 class raw_json_string;
12 
17 class value {
18 public:
24  simdjson_inline value() noexcept = default;
25 
37  template<typename T> simdjson_inline simdjson_result<T> get() noexcept {
38  // Unless the simdjson library provides an inline implementation, calling this method should
39  // immediately fail.
40  static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library.");
41  }
42 
52  template<typename T> simdjson_inline error_code get(T &out) noexcept;
53 
60  simdjson_inline simdjson_result<array> get_array() noexcept;
61 
68  simdjson_inline simdjson_result<object> get_object() noexcept;
69 
76  simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
77 
84  simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
85 
92  simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
93 
100  simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
101 
108  simdjson_inline simdjson_result<double> get_double() noexcept;
109 
116  simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
117 
132  simdjson_inline simdjson_result<std::string_view> get_string() noexcept;
133 
142  simdjson_inline simdjson_result<raw_json_string> get_raw_json_string() noexcept;
143 
150  simdjson_inline simdjson_result<bool> get_bool() noexcept;
151 
160  simdjson_inline simdjson_result<bool> is_null() noexcept;
161 
162 #if SIMDJSON_EXCEPTIONS
169  simdjson_inline operator array() noexcept(false);
176  simdjson_inline operator object() noexcept(false);
183  simdjson_inline operator uint64_t() noexcept(false);
190  simdjson_inline operator int64_t() noexcept(false);
197  simdjson_inline operator double() noexcept(false);
209  simdjson_inline operator std::string_view() noexcept(false);
218  simdjson_inline operator raw_json_string() noexcept(false);
225  simdjson_inline operator bool() noexcept(false);
226 #endif
227 
235  simdjson_inline simdjson_result<array_iterator> begin() & noexcept;
241  simdjson_inline simdjson_result<array_iterator> end() & noexcept;
256  simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
274  simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
282  simdjson_inline simdjson_result<value> at(size_t index) noexcept;
305  simdjson_inline simdjson_result<value> find_field(std::string_view key) noexcept;
307  simdjson_inline simdjson_result<value> find_field(const char *key) noexcept;
308 
331  simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) noexcept;
333  simdjson_inline simdjson_result<value> find_field_unordered(const char *key) noexcept;
335  simdjson_inline simdjson_result<value> operator[](std::string_view key) noexcept;
337  simdjson_inline simdjson_result<value> operator[](const char *key) noexcept;
338 
352  simdjson_inline simdjson_result<json_type> type() noexcept;
353 
361  simdjson_inline simdjson_result<bool> is_scalar() noexcept;
362 
368  simdjson_inline bool is_negative() noexcept;
382  simdjson_inline simdjson_result<bool> is_integer() noexcept;
405  simdjson_inline simdjson_result<number_type> get_number_type() noexcept;
406 
437  simdjson_warn_unused simdjson_inline simdjson_result<number> get_number() noexcept;
438 
439 
463  simdjson_inline std::string_view raw_json_token() noexcept;
464 
468  simdjson_inline simdjson_result<const char *> current_location() noexcept;
469 
479  simdjson_inline int32_t current_depth() const noexcept;
480 
523  simdjson_inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
524 
525 protected:
529  simdjson_inline value(const value_iterator &iter) noexcept;
530 
534  simdjson_inline void skip() noexcept;
535 
541  static simdjson_inline value start(const value_iterator &iter) noexcept;
542 
546  static simdjson_inline value resume(const value_iterator &iter) noexcept;
547 
551  simdjson_inline simdjson_result<object> start_or_resume_object() noexcept;
552 
553  // simdjson_inline void log_value(const char *type) const noexcept;
554  // simdjson_inline void log_error(const char *message) const noexcept;
555 
556  value_iterator iter{};
557 
558  friend class document;
559  friend class array_iterator;
560  friend class field;
561  friend class object;
562  friend struct simdjson_result<value>;
563  friend struct simdjson_result<field>;
564 };
565 
566 } // namespace ondemand
567 } // namespace SIMDJSON_IMPLEMENTATION
568 } // namespace simdjson
569 
570 namespace simdjson {
571 
572 template<>
573 struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value> {
574 public:
576  simdjson_inline simdjson_result(error_code error) noexcept;
577  simdjson_inline simdjson_result() noexcept = default;
578 
579  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> get_array() noexcept;
580  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> get_object() noexcept;
581 
582  simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
583  simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
584  simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
585  simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
586  simdjson_inline simdjson_result<double> get_double() noexcept;
587  simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
588  simdjson_inline simdjson_result<std::string_view> get_string() noexcept;
589  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> get_raw_json_string() noexcept;
590  simdjson_inline simdjson_result<bool> get_bool() noexcept;
591  simdjson_inline simdjson_result<bool> is_null() noexcept;
592 
593  template<typename T> simdjson_inline simdjson_result<T> get() noexcept;
594 
595  template<typename T> simdjson_inline error_code get(T &out) noexcept;
596 
597 #if SIMDJSON_EXCEPTIONS
598  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false);
599  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false);
600  simdjson_inline operator uint64_t() noexcept(false);
601  simdjson_inline operator int64_t() noexcept(false);
602  simdjson_inline operator double() noexcept(false);
603  simdjson_inline operator std::string_view() noexcept(false);
604  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false);
605  simdjson_inline operator bool() noexcept(false);
606 #endif
607  simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
608  simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
609  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
612 
633  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) noexcept;
635  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(const char *key) noexcept;
636 
656  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) noexcept;
658  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(const char *key) noexcept;
660  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) noexcept;
662  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](const char *key) noexcept;
663 
672  simdjson_inline simdjson_result<bool> is_scalar() noexcept;
673  simdjson_inline simdjson_result<bool> is_negative() noexcept;
674  simdjson_inline simdjson_result<bool> is_integer() noexcept;
675  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number_type> get_number_type() noexcept;
676  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> get_number() noexcept;
677 
679  simdjson_inline simdjson_result<std::string_view> raw_json_token() noexcept;
680 
682  simdjson_inline simdjson_result<const char *> current_location() noexcept;
684  simdjson_inline simdjson_result<int32_t> current_depth() const noexcept;
685  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
686 };
687 
688 } // namespace simdjson
A JSON field (key/value pair) in an object.
Definition: field.h:14
A forward-only JSON object field iterator.
Definition: object.h:10
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition: value.h:17
static simdjson_inline value start(const value_iterator &iter) noexcept
Start a value at the current position.
Definition: value-inl.h:9
simdjson_inline simdjson_result< bool > is_null() noexcept
Checks if this JSON value is null.
Definition: value-inl.h:57
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
Definition: value-inl.h:179
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:129
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
Definition: value-inl.h:161
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:168
simdjson_inline simdjson_result< array > get_array() noexcept
Cast this JSON value to an array.
Definition: value-inl.h:16
simdjson_inline std::string_view raw_json_token() noexcept
Get the raw JSON for this token.
Definition: value-inl.h:175
simdjson_inline simdjson_result< object > get_object() noexcept
Cast this JSON value to an object.
Definition: value-inl.h:19
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition: value-inl.h:107
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:51
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:117
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
Definition: value-inl.h:165
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
Definition: value-inl.h:104
simdjson_inline simdjson_result< object > start_or_resume_object() noexcept
Get the object, starting or resuming it as necessary.
Definition: value-inl.h:22
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
Definition: value-inl.h:101
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:45
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
Definition: value-inl.h:183
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
Definition: value-inl.h:154
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
Definition: value.h:37
simdjson_inline simdjson_result< std::string_view > get_string() noexcept
Cast this JSON value to a string.
Definition: value-inl.h:33
static simdjson_inline value resume(const value_iterator &iter) noexcept
Resume a value.
Definition: value-inl.h:12
simdjson_inline simdjson_result< double > get_double() noexcept
Cast this JSON value to a double.
Definition: value-inl.h:36
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
Definition: value-inl.h:54
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
Definition: value-inl.h:171
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:187
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
Definition: value-inl.h:48
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:150
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
Definition: value-inl.h:39
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:30
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:136
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
Definition: value-inl.h:124
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
Definition: value-inl.h:42
simdjson_inline void skip() noexcept
Skip this value, allowing iteration to continue.
@ object
A JSON object ( { "a": 1, "b" 2, ... } )
We want to support argument-dependent lookup (ADL).
error_code
All possible errors returned by simdjson.
Definition: error.h:17
A type representing a JSON number.
Definition: json_type.h:24
The result of a simdjson operation that could fail.
Definition: error.h:205
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:132
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition: error-inl.h:127
simdjson_inline T & value() &noexcept(false)
Get the result value.