simdjson  3.1.6
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(bool allow_replacement = false) noexcept;
133 
134 
148  simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
157  simdjson_inline simdjson_result<raw_json_string> get_raw_json_string() noexcept;
158 
165  simdjson_inline simdjson_result<bool> get_bool() noexcept;
166 
175  simdjson_inline simdjson_result<bool> is_null() noexcept;
176 
177 #if SIMDJSON_EXCEPTIONS
184  simdjson_inline operator array() noexcept(false);
191  simdjson_inline operator object() noexcept(false);
198  simdjson_inline operator uint64_t() noexcept(false);
205  simdjson_inline operator int64_t() noexcept(false);
212  simdjson_inline operator double() noexcept(false);
224  simdjson_inline operator std::string_view() noexcept(false);
233  simdjson_inline operator raw_json_string() noexcept(false);
240  simdjson_inline operator bool() noexcept(false);
241 #endif
242 
250  simdjson_inline simdjson_result<array_iterator> begin() & noexcept;
256  simdjson_inline simdjson_result<array_iterator> end() & noexcept;
271  simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
289  simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
297  simdjson_inline simdjson_result<value> at(size_t index) noexcept;
320  simdjson_inline simdjson_result<value> find_field(std::string_view key) noexcept;
322  simdjson_inline simdjson_result<value> find_field(const char *key) noexcept;
323 
346  simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) noexcept;
348  simdjson_inline simdjson_result<value> find_field_unordered(const char *key) noexcept;
350  simdjson_inline simdjson_result<value> operator[](std::string_view key) noexcept;
352  simdjson_inline simdjson_result<value> operator[](const char *key) noexcept;
353 
367  simdjson_inline simdjson_result<json_type> type() noexcept;
368 
376  simdjson_inline simdjson_result<bool> is_scalar() noexcept;
377 
383  simdjson_inline bool is_negative() noexcept;
397  simdjson_inline simdjson_result<bool> is_integer() noexcept;
420  simdjson_inline simdjson_result<number_type> get_number_type() noexcept;
421 
452  simdjson_warn_unused simdjson_inline simdjson_result<number> get_number() noexcept;
453 
454 
478  simdjson_inline std::string_view raw_json_token() noexcept;
479 
483  simdjson_inline simdjson_result<const char *> current_location() noexcept;
484 
494  simdjson_inline int32_t current_depth() const noexcept;
495 
538  simdjson_inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
539 
540 protected:
544  simdjson_inline value(const value_iterator &iter) noexcept;
545 
549  simdjson_inline void skip() noexcept;
550 
556  static simdjson_inline value start(const value_iterator &iter) noexcept;
557 
561  static simdjson_inline value resume(const value_iterator &iter) noexcept;
562 
566  simdjson_inline simdjson_result<object> start_or_resume_object() noexcept;
567 
568  // simdjson_inline void log_value(const char *type) const noexcept;
569  // simdjson_inline void log_error(const char *message) const noexcept;
570 
571  value_iterator iter{};
572 
573  friend class document;
574  friend class array_iterator;
575  friend class field;
576  friend class object;
577  friend struct simdjson_result<value>;
578  friend struct simdjson_result<field>;
579 };
580 
581 } // namespace ondemand
582 } // namespace SIMDJSON_IMPLEMENTATION
583 } // namespace simdjson
584 
585 namespace simdjson {
586 
587 template<>
588 struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value> {
589 public:
591  simdjson_inline simdjson_result(error_code error) noexcept;
592  simdjson_inline simdjson_result() noexcept = default;
593 
594  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> get_array() noexcept;
595  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> get_object() noexcept;
596 
597  simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
598  simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
599  simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
600  simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
601  simdjson_inline simdjson_result<double> get_double() noexcept;
602  simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
603  simdjson_inline simdjson_result<std::string_view> get_string(bool allow_replacement = false) noexcept;
604  simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
605  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> get_raw_json_string() noexcept;
606  simdjson_inline simdjson_result<bool> get_bool() noexcept;
607  simdjson_inline simdjson_result<bool> is_null() noexcept;
608 
609  template<typename T> simdjson_inline simdjson_result<T> get() noexcept;
610 
611  template<typename T> simdjson_inline error_code get(T &out) noexcept;
612 
613 #if SIMDJSON_EXCEPTIONS
614  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false);
615  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false);
616  simdjson_inline operator uint64_t() noexcept(false);
617  simdjson_inline operator int64_t() noexcept(false);
618  simdjson_inline operator double() noexcept(false);
619  simdjson_inline operator std::string_view() noexcept(false);
620  simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false);
621  simdjson_inline operator bool() noexcept(false);
622 #endif
623  simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
624  simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
625  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
628 
649  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) noexcept;
651  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(const char *key) noexcept;
652 
672  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) noexcept;
674  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(const char *key) noexcept;
676  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) noexcept;
678  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](const char *key) noexcept;
679 
688  simdjson_inline simdjson_result<bool> is_scalar() noexcept;
689  simdjson_inline simdjson_result<bool> is_negative() noexcept;
690  simdjson_inline simdjson_result<bool> is_integer() noexcept;
691  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number_type> get_number_type() noexcept;
692  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> get_number() noexcept;
693 
695  simdjson_inline simdjson_result<std::string_view> raw_json_token() noexcept;
696 
698  simdjson_inline simdjson_result<const char *> current_location() noexcept;
700  simdjson_inline simdjson_result<int32_t> current_depth() const noexcept;
701  simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
702 };
703 
704 } // 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:60
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
Definition: value-inl.h:182
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:132
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
Definition: value-inl.h:164
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:171
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:178
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:110
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:54
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:120
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
Definition: value-inl.h:168
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
Definition: value-inl.h:107
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:104
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:48
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
Definition: value-inl.h:186
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
Definition: value-inl.h:157
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
Definition: value.h:37
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:39
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:33
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
Definition: value-inl.h:57
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
Definition: value-inl.h:174
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:190
simdjson_inline simdjson_result< std::string_view > get_wobbly_string() noexcept
Cast this JSON value to a "wobbly" string.
Definition: value-inl.h:36
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
Definition: value-inl.h:51
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:153
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
Definition: value-inl.h:42
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:139
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
Definition: value-inl.h:127
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
Definition: value-inl.h:45
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.