simdjson  3.1.6
Ridiculously Fast JSON
element.h
1 #ifndef SIMDJSON_DOM_ELEMENT_H
2 #define SIMDJSON_DOM_ELEMENT_H
3 
4 #include "simdjson/common_defs.h"
5 #include "simdjson/error.h"
6 #include "simdjson/internal/tape_ref.h"
7 #include <ostream>
8 
9 namespace simdjson {
10 namespace internal {
11 template<typename T>
12 class string_builder;
13 }
14 namespace dom {
15 class array;
16 class document;
17 class object;
18 
23 enum class element_type {
24  ARRAY = '[',
25  OBJECT = '{',
26  INT64 = 'l',
27  UINT64 = 'u',
28  DOUBLE = 'd',
29  STRING = '"',
30  BOOL = 't',
31  NULL_VALUE = 'n'
32 };
33 
40 class element {
41 public:
43  simdjson_inline element() noexcept;
44 
46  simdjson_inline element_type type() const noexcept;
47 
54  inline simdjson_result<array> get_array() const noexcept;
61  inline simdjson_result<object> get_object() const noexcept;
78  inline simdjson_result<const char *> get_c_str() const noexcept;
88  inline simdjson_result<size_t> get_string_length() const noexcept;
98  inline simdjson_result<std::string_view> get_string() const noexcept;
106  inline simdjson_result<int64_t> get_int64() const noexcept;
114  inline simdjson_result<uint64_t> get_uint64() const noexcept;
121  inline simdjson_result<double> get_double() const noexcept;
128  inline simdjson_result<bool> get_bool() const noexcept;
129 
135  inline bool is_array() const noexcept;
141  inline bool is_object() const noexcept;
147  inline bool is_string() const noexcept;
153  inline bool is_int64() const noexcept;
159  inline bool is_uint64() const noexcept;
165  inline bool is_double() const noexcept;
166 
172  inline bool is_number() const noexcept;
173 
179  inline bool is_bool() const noexcept;
183  inline bool is_null() const noexcept;
184 
197  template<typename T>
198  simdjson_inline bool is() const noexcept;
199 
219  template<typename T>
220  inline simdjson_result<T> get() const noexcept {
221  // Unless the simdjson library provides an inline implementation, calling this method should
222  // immediately fail.
223  static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library.");
224  }
225 
242  template<typename T>
243  simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
244 
260  template<typename T>
261  inline void tie(T &value, error_code &error) && noexcept;
262 
263 #if SIMDJSON_EXCEPTIONS
270  inline operator bool() const noexcept(false);
271 
283  inline explicit operator const char*() const noexcept(false);
284 
294  inline operator std::string_view() const noexcept(false);
295 
303  inline operator uint64_t() const noexcept(false);
311  inline operator int64_t() const noexcept(false);
319  inline operator double() const noexcept(false);
326  inline operator array() const noexcept(false);
333  inline operator object() const noexcept(false);
334 
341  inline dom::array::iterator begin() const noexcept(false);
342 
349  inline dom::array::iterator end() const noexcept(false);
350 #endif // SIMDJSON_EXCEPTIONS
351 
365  inline simdjson_result<element> operator[](std::string_view key) const noexcept;
366 
380  inline simdjson_result<element> operator[](const char *key) const noexcept;
381 
404  inline simdjson_result<element> at_pointer(const std::string_view json_pointer) const noexcept;
405 
406 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
427  [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
428  inline simdjson_result<element> at(const std::string_view json_pointer) const noexcept;
429 #endif // SIMDJSON_DISABLE_DEPRECATED_API
430 
437  inline simdjson_result<element> at(size_t index) const noexcept;
438 
451  inline simdjson_result<element> at_key(std::string_view key) const noexcept;
452 
461  inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
462 
464  inline bool dump_raw_tape(std::ostream &out) const noexcept;
465 
466 private:
467  simdjson_inline element(const internal::tape_ref &tape) noexcept;
468  internal::tape_ref tape;
469  friend class document;
470  friend class object;
471  friend class array;
472  friend struct simdjson_result<element>;
473  template<typename T>
474  friend class simdjson::internal::string_builder;
475 
476 };
477 
478 } // namespace dom
479 
481 template<>
482 struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom::element> {
483 public:
484  simdjson_inline simdjson_result() noexcept;
485  simdjson_inline simdjson_result(dom::element &&value) noexcept;
486  simdjson_inline simdjson_result(error_code error) noexcept;
487 
488  simdjson_inline simdjson_result<dom::element_type> type() const noexcept;
489  template<typename T>
490  simdjson_inline bool is() const noexcept;
491  template<typename T>
492  simdjson_inline simdjson_result<T> get() const noexcept;
493  template<typename T>
494  simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
495 
496  simdjson_inline simdjson_result<dom::array> get_array() const noexcept;
497  simdjson_inline simdjson_result<dom::object> get_object() const noexcept;
498  simdjson_inline simdjson_result<const char *> get_c_str() const noexcept;
499  simdjson_inline simdjson_result<size_t> get_string_length() const noexcept;
500  simdjson_inline simdjson_result<std::string_view> get_string() const noexcept;
501  simdjson_inline simdjson_result<int64_t> get_int64() const noexcept;
502  simdjson_inline simdjson_result<uint64_t> get_uint64() const noexcept;
503  simdjson_inline simdjson_result<double> get_double() const noexcept;
504  simdjson_inline simdjson_result<bool> get_bool() const noexcept;
505 
506  simdjson_inline bool is_array() const noexcept;
507  simdjson_inline bool is_object() const noexcept;
508  simdjson_inline bool is_string() const noexcept;
509  simdjson_inline bool is_int64() const noexcept;
510  simdjson_inline bool is_uint64() const noexcept;
511  simdjson_inline bool is_double() const noexcept;
512  simdjson_inline bool is_number() const noexcept;
513  simdjson_inline bool is_bool() const noexcept;
514  simdjson_inline bool is_null() const noexcept;
515 
516  simdjson_inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
517  simdjson_inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
518  simdjson_inline simdjson_result<dom::element> at_pointer(const std::string_view json_pointer) const noexcept;
519  [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
520  simdjson_inline simdjson_result<dom::element> at(const std::string_view json_pointer) const noexcept;
521  simdjson_inline simdjson_result<dom::element> at(size_t index) const noexcept;
522  simdjson_inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
523  simdjson_inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
524 
525 #if SIMDJSON_EXCEPTIONS
526  simdjson_inline operator bool() const noexcept(false);
527  simdjson_inline explicit operator const char*() const noexcept(false);
528  simdjson_inline operator std::string_view() const noexcept(false);
529  simdjson_inline operator uint64_t() const noexcept(false);
530  simdjson_inline operator int64_t() const noexcept(false);
531  simdjson_inline operator double() const noexcept(false);
532  simdjson_inline operator dom::array() const noexcept(false);
533  simdjson_inline operator dom::object() const noexcept(false);
534 
535  simdjson_inline dom::array::iterator begin() const noexcept(false);
536  simdjson_inline dom::array::iterator end() const noexcept(false);
537 #endif // SIMDJSON_EXCEPTIONS
538 };
539 
540 
541 } // namespace simdjson
542 
543 #endif // SIMDJSON_DOM_DOCUMENT_H
JSON array.
Definition: array.h:22
A parsed JSON document.
Definition: document.h:18
A JSON element.
Definition: element.h:40
bool is_object() const noexcept
Whether this element is a json object.
Definition: element-inl.h:333
bool is_double() const noexcept
Whether this element is a json number that fits in a double.
Definition: element-inl.h:337
simdjson_inline element_type type() const noexcept
The type of this element.
Definition: element-inl.h:188
bool is_number() const noexcept
Whether this element is a json number.
Definition: element-inl.h:339
simdjson_result< const char * > get_c_str() const noexcept
Cast this element to a null-terminated C string.
Definition: element-inl.h:203
simdjson_result< element > at_pointer(const std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
Definition: element-inl.h:372
simdjson_result< double > get_double() const noexcept
Cast this element to a double floating-point.
Definition: element-inl.h:261
simdjson_result< T > get() const noexcept
Get the value as the provided type (T).
Definition: element.h:220
simdjson_inline bool is() const noexcept
Tell whether the value can be cast to provided type (T).
Definition: element-inl.h:318
simdjson_result< element > at_key_case_insensitive(std::string_view key) const noexcept
Get the value associated with the given key in a case-insensitive manner.
Definition: element-inl.h:404
void tie(T &value, error_code &error) &&noexcept
Get the value as the provided type (T), setting error if it's not the given type.
simdjson_result< bool > get_bool() const noexcept
Cast this element to a bool.
Definition: element-inl.h:194
bool is_string() const noexcept
Whether this element is a json string.
Definition: element-inl.h:334
simdjson_result< element > at_key(std::string_view key) const noexcept
Get the value associated with the given key.
Definition: element-inl.h:401
dom::array::iterator end() const noexcept(false)
Iterate over each element in this array.
Definition: element-inl.h:359
simdjson_result< array > get_array() const noexcept
Cast this element to an array.
Definition: element-inl.h:283
simdjson_result< size_t > get_string_length() const noexcept
Gives the length in bytes of the string.
Definition: element-inl.h:213
simdjson_inline element() noexcept
Create a new, invalid element.
Definition: element-inl.h:185
simdjson_result< uint64_t > get_uint64() const noexcept
Cast this element to an unsigned integer.
Definition: element-inl.h:232
dom::array::iterator begin() const noexcept(false)
Iterate over each element in this array.
Definition: element-inl.h:356
simdjson_result< element > at(const std::string_view json_pointer) const noexcept
Version 0.4 of simdjson used an incorrect interpretation of the JSON Pointer standard and allowed the...
Definition: element-inl.h:391
simdjson_result< int64_t > get_int64() const noexcept
Cast this element to a signed integer.
Definition: element-inl.h:246
simdjson_result< object > get_object() const noexcept
Cast this element to an object.
Definition: element-inl.h:292
bool is_uint64() const noexcept
Whether this element is a json number that fits in an unsigned 64-bit integer.
Definition: element-inl.h:336
simdjson_result< element > operator[](std::string_view key) const noexcept
Get the value associated with the given key.
Definition: element-inl.h:365
bool is_int64() const noexcept
Whether this element is a json number that fits in a signed 64-bit integer.
Definition: element-inl.h:335
bool is_null() const noexcept
Whether this element is a json null.
Definition: element-inl.h:341
simdjson_result< std::string_view > get_string() const noexcept
Cast this element to a string.
Definition: element-inl.h:223
bool is_array() const noexcept
Whether this element is a json array.
Definition: element-inl.h:332
bool is_bool() const noexcept
Whether this element is a json true or false.
Definition: element-inl.h:338
JSON object.
Definition: object.h:22
@ 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
The result of a JSON conversion that may fail.
Definition: array.h:149
The result of a JSON navigation that may fail.
Definition: element.h:482
The result of a JSON conversion that may fail.
Definition: object.h:238
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.