simdjson  3.3.0
Ridiculously Fast JSON
element.h
1 #ifndef SIMDJSON_DOM_ELEMENT_H
2 #define SIMDJSON_DOM_ELEMENT_H
3 
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/array.h"
6 
7 namespace simdjson {
8 namespace dom {
9 
14 enum class element_type {
15  ARRAY = '[',
16  OBJECT = '{',
17  INT64 = 'l',
18  UINT64 = 'u',
19  DOUBLE = 'd',
20  STRING = '"',
21  BOOL = 't',
22  NULL_VALUE = 'n'
23 };
24 
31 class element {
32 public:
34  simdjson_inline element() noexcept;
35 
37  simdjson_inline element_type type() const noexcept;
38 
45  inline simdjson_result<array> get_array() const noexcept;
52  inline simdjson_result<object> get_object() const noexcept;
69  inline simdjson_result<const char *> get_c_str() const noexcept;
79  inline simdjson_result<size_t> get_string_length() const noexcept;
89  inline simdjson_result<std::string_view> get_string() const noexcept;
97  inline simdjson_result<int64_t> get_int64() const noexcept;
105  inline simdjson_result<uint64_t> get_uint64() const noexcept;
112  inline simdjson_result<double> get_double() const noexcept;
119  inline simdjson_result<bool> get_bool() const noexcept;
120 
126  inline bool is_array() const noexcept;
132  inline bool is_object() const noexcept;
138  inline bool is_string() const noexcept;
144  inline bool is_int64() const noexcept;
150  inline bool is_uint64() const noexcept;
156  inline bool is_double() const noexcept;
157 
163  inline bool is_number() const noexcept;
164 
170  inline bool is_bool() const noexcept;
174  inline bool is_null() const noexcept;
175 
188  template<typename T>
189  simdjson_inline bool is() const noexcept;
190 
210  template<typename T>
211  inline simdjson_result<T> get() const noexcept {
212  // Unless the simdjson library provides an inline implementation, calling this method should
213  // immediately fail.
214  static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library. "
215  "The supported types are Boolean (bool), numbers (double, uint64_t, int64_t), "
216  "strings (std::string_view, const char *), arrays (dom::array) and objects (dom::object). "
217  "We recommand you use get_double(), get_bool(), get_uint64(), get_int64(), "
218  "get_object(), get_array() or get_string() instead of the get template.");
219  }
220 
237  template<typename T>
238  simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
239 
255  template<typename T>
256  inline void tie(T &value, error_code &error) && noexcept;
257 
258 #if SIMDJSON_EXCEPTIONS
265  inline operator bool() const noexcept(false);
266 
278  inline explicit operator const char*() const noexcept(false);
279 
289  inline operator std::string_view() const noexcept(false);
290 
298  inline operator uint64_t() const noexcept(false);
306  inline operator int64_t() const noexcept(false);
314  inline operator double() const noexcept(false);
321  inline operator array() const noexcept(false);
328  inline operator object() const noexcept(false);
329 
336  inline dom::array::iterator begin() const noexcept(false);
337 
344  inline dom::array::iterator end() const noexcept(false);
345 #endif // SIMDJSON_EXCEPTIONS
346 
360  inline simdjson_result<element> operator[](std::string_view key) const noexcept;
361 
375  inline simdjson_result<element> operator[](const char *key) const noexcept;
376 
399  inline simdjson_result<element> at_pointer(const std::string_view json_pointer) const noexcept;
400 
401 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
422  [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
423  inline simdjson_result<element> at(const std::string_view json_pointer) const noexcept;
424 #endif // SIMDJSON_DISABLE_DEPRECATED_API
425 
432  inline simdjson_result<element> at(size_t index) const noexcept;
433 
446  inline simdjson_result<element> at_key(std::string_view key) const noexcept;
447 
456  inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
457 
464  inline bool operator<(const element &other) const noexcept;
465 
472  inline bool operator==(const element &other) const noexcept;
473 
475  inline bool dump_raw_tape(std::ostream &out) const noexcept;
476 
477 private:
478  simdjson_inline element(const internal::tape_ref &tape) noexcept;
479  internal::tape_ref tape;
480  friend class document;
481  friend class object;
482  friend class array;
483  friend struct simdjson_result<element>;
484  template<typename T>
485  friend class simdjson::internal::string_builder;
486 
487 };
488 
489 } // namespace dom
490 
492 template<>
493 struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom::element> {
494 public:
495  simdjson_inline simdjson_result() noexcept;
496  simdjson_inline simdjson_result(dom::element &&value) noexcept;
497  simdjson_inline simdjson_result(error_code error) noexcept;
498 
499  simdjson_inline simdjson_result<dom::element_type> type() const noexcept;
500  template<typename T>
501  simdjson_inline bool is() const noexcept;
502  template<typename T>
503  simdjson_inline simdjson_result<T> get() const noexcept;
504  template<typename T>
505  simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
506 
507  simdjson_inline simdjson_result<dom::array> get_array() const noexcept;
508  simdjson_inline simdjson_result<dom::object> get_object() const noexcept;
509  simdjson_inline simdjson_result<const char *> get_c_str() const noexcept;
510  simdjson_inline simdjson_result<size_t> get_string_length() const noexcept;
511  simdjson_inline simdjson_result<std::string_view> get_string() const noexcept;
512  simdjson_inline simdjson_result<int64_t> get_int64() const noexcept;
513  simdjson_inline simdjson_result<uint64_t> get_uint64() const noexcept;
514  simdjson_inline simdjson_result<double> get_double() const noexcept;
515  simdjson_inline simdjson_result<bool> get_bool() const noexcept;
516 
517  simdjson_inline bool is_array() const noexcept;
518  simdjson_inline bool is_object() const noexcept;
519  simdjson_inline bool is_string() const noexcept;
520  simdjson_inline bool is_int64() const noexcept;
521  simdjson_inline bool is_uint64() const noexcept;
522  simdjson_inline bool is_double() const noexcept;
523  simdjson_inline bool is_number() const noexcept;
524  simdjson_inline bool is_bool() const noexcept;
525  simdjson_inline bool is_null() const noexcept;
526 
527  simdjson_inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
528  simdjson_inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
529  simdjson_inline simdjson_result<dom::element> at_pointer(const std::string_view json_pointer) const noexcept;
530  [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
531  simdjson_inline simdjson_result<dom::element> at(const std::string_view json_pointer) const noexcept;
532  simdjson_inline simdjson_result<dom::element> at(size_t index) const noexcept;
533  simdjson_inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
534  simdjson_inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
535 
536 #if SIMDJSON_EXCEPTIONS
537  simdjson_inline operator bool() const noexcept(false);
538  simdjson_inline explicit operator const char*() const noexcept(false);
539  simdjson_inline operator std::string_view() const noexcept(false);
540  simdjson_inline operator uint64_t() const noexcept(false);
541  simdjson_inline operator int64_t() const noexcept(false);
542  simdjson_inline operator double() const noexcept(false);
543  simdjson_inline operator dom::array() const noexcept(false);
544  simdjson_inline operator dom::object() const noexcept(false);
545 
546  simdjson_inline dom::array::iterator begin() const noexcept(false);
547  simdjson_inline dom::array::iterator end() const noexcept(false);
548 #endif // SIMDJSON_EXCEPTIONS
549 };
550 
551 } // namespace simdjson
552 
553 #endif // SIMDJSON_DOM_DOCUMENT_H
JSON array.
Definition: array.h:13
A parsed JSON document.
Definition: document.h:16
A JSON element.
Definition: element.h:31
bool is_object() const noexcept
Whether this element is a json object.
Definition: element-inl.h:339
bool is_double() const noexcept
Whether this element is a json number that fits in a double.
Definition: element-inl.h:343
simdjson_inline element_type type() const noexcept
The type of this element.
Definition: element-inl.h:194
bool is_number() const noexcept
Whether this element is a json number.
Definition: element-inl.h:345
simdjson_result< const char * > get_c_str() const noexcept
Cast this element to a null-terminated C string.
Definition: element-inl.h:209
bool operator<(const element &other) const noexcept
operator< defines a total order for element allowing to use them in ordered C++ STL containers
Definition: element-inl.h:413
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:378
simdjson_result< double > get_double() const noexcept
Cast this element to a double floating-point.
Definition: element-inl.h:267
simdjson_result< T > get() const noexcept
Get the value as the provided type (T).
Definition: element.h:211
simdjson_inline bool is() const noexcept
Tell whether the value can be cast to provided type (T).
Definition: element-inl.h:324
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:410
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:200
bool is_string() const noexcept
Whether this element is a json string.
Definition: element-inl.h:340
simdjson_result< element > at_key(std::string_view key) const noexcept
Get the value associated with the given key.
Definition: element-inl.h:407
dom::array::iterator end() const noexcept(false)
Iterate over each element in this array.
Definition: element-inl.h:365
simdjson_result< array > get_array() const noexcept
Cast this element to an array.
Definition: element-inl.h:289
simdjson_result< size_t > get_string_length() const noexcept
Gives the length in bytes of the string.
Definition: element-inl.h:219
simdjson_inline element() noexcept
Create a new, invalid element.
Definition: element-inl.h:191
simdjson_result< uint64_t > get_uint64() const noexcept
Cast this element to an unsigned integer.
Definition: element-inl.h:238
dom::array::iterator begin() const noexcept(false)
Iterate over each element in this array.
Definition: element-inl.h:362
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:397
simdjson_result< int64_t > get_int64() const noexcept
Cast this element to a signed integer.
Definition: element-inl.h:252
simdjson_result< object > get_object() const noexcept
Cast this element to an object.
Definition: element-inl.h:298
bool is_uint64() const noexcept
Whether this element is a json number that fits in an unsigned 64-bit integer.
Definition: element-inl.h:342
simdjson_result< element > operator[](std::string_view key) const noexcept
Get the value associated with the given key.
Definition: element-inl.h:371
bool is_int64() const noexcept
Whether this element is a json number that fits in a signed 64-bit integer.
Definition: element-inl.h:341
bool is_null() const noexcept
Whether this element is a json null.
Definition: element-inl.h:347
simdjson_result< std::string_view > get_string() const noexcept
Cast this element to a string.
Definition: element-inl.h:229
bool is_array() const noexcept
Whether this element is a json array.
Definition: element-inl.h:338
bool operator==(const element &other) const noexcept
operator== allows to verify if two element values reference the same JSON item
Definition: element-inl.h:416
bool is_bool() const noexcept
Whether this element is a json true or false.
Definition: element-inl.h:344
JSON object.
Definition: object.h:14
element_type
The actual concrete type of a JSON element This is the type it is most easily cast to with get<>.
Definition: element.h:14
@ STRING
std::string_view
@ UINT64
uint64_t: any integer that fits in uint64_t but not int64_t
@ DOUBLE
double: Any number with a "." or "e" that fits in double.
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
The result of a JSON conversion that may fail.
Definition: array.h:140
The result of a JSON navigation that may fail.
Definition: element.h:493
The result of a JSON conversion that may fail.
Definition: object.h:230
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.