simdjson 5.0.2
Ridiculously Fast JSON
Loading...
Searching...
No Matches
element.h
1#ifndef SIMDJSON_DOM_ELEMENT_H
2#define SIMDJSON_DOM_ELEMENT_H
3
4#include <vector>
5
6#include "simdjson/dom/base.h"
7#include "simdjson/dom/array.h"
8
9namespace simdjson {
10namespace dom {
11
16enum class element_type {
17 ARRAY = '[',
18 OBJECT = '{',
19 INT64 = 'l',
20 UINT64 = 'u',
21 DOUBLE = 'd',
22 STRING = '"',
23 BOOL = 't',
24 NULL_VALUE = 'n',
26 // if you set parser.number_as_string(true).
27 BIGINT = 'Z'
28};
29
36class element {
37public:
39 simdjson_inline element() noexcept;
40
42 simdjson_inline element_type type() const noexcept;
43
50 inline simdjson_result<array> get_array() const noexcept;
57 inline simdjson_result<object> get_object() const noexcept;
74 inline simdjson_result<const char *> get_c_str() const noexcept;
84 inline simdjson_result<size_t> get_string_length() const noexcept;
94 inline simdjson_result<std::string_view> get_string() const noexcept;
95
96 #if SIMDJSON_SUPPORTS_CHAR8_T
106 inline simdjson_result<std::u8string_view> get_u8string() const noexcept;
107 #endif
108
116 inline simdjson_result<int64_t> get_int64() const noexcept;
124 inline simdjson_result<uint64_t> get_uint64() const noexcept;
131 inline simdjson_result<double> get_double() const noexcept;
138 inline simdjson_result<bool> get_bool() const noexcept;
139
146 inline simdjson_result<std::string_view> get_bigint() const noexcept;
147
153 inline bool is_array() const noexcept;
159 inline bool is_object() const noexcept;
165 inline bool is_string() const noexcept;
171 inline bool is_int64() const noexcept;
177 inline bool is_uint64() const noexcept;
183 inline bool is_double() const noexcept;
184
190 inline bool is_number() const noexcept;
191
197 inline bool is_bool() const noexcept;
201 inline bool is_null() const noexcept;
202
206 inline bool is_bigint() const noexcept;
207
220 template<typename T>
221 simdjson_inline bool is() const noexcept;
222
242 template<typename T>
243 inline simdjson_result<T> get() const noexcept {
244 // Unless the simdjson library provides an inline implementation, calling this method should
245 // immediately fail.
246 static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library. "
247 "The supported types are Boolean (bool), numbers (double, uint64_t, int64_t), "
248 "strings (std::string_view, const char *), arrays (dom::array) and objects (dom::object). "
249 "We recommend you use get_double(), get_bool(), get_uint64(), get_int64(), "
250 "get_object(), get_array() or get_string() instead of the get template.");
251 }
252
269 template<typename T>
270 simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
271
287 template<typename T>
288 inline void tie(T &value, error_code &error) && noexcept;
289
290#if SIMDJSON_EXCEPTIONS
297 inline operator bool() const noexcept(false);
298
310 inline explicit operator const char*() const noexcept(false);
311
321 inline operator std::string_view() const noexcept(false);
322
330 inline operator uint64_t() const noexcept(false);
338 inline operator int64_t() const noexcept(false);
345 inline operator double() const noexcept(false);
352 inline operator array() const noexcept(false);
359 inline operator object() const noexcept(false);
360
367 inline dom::array::iterator begin() const noexcept(false);
368
375 inline dom::array::iterator end() const noexcept(false);
376#endif // SIMDJSON_EXCEPTIONS
377
391 inline simdjson_result<element> operator[](std::string_view key) const noexcept;
392
406 inline simdjson_result<element> operator[](const char *key) const noexcept;
407 simdjson_result<element> operator[](int) const noexcept = delete;
408
409
432 inline simdjson_result<element> at_pointer(const std::string_view json_pointer) const noexcept;
433
434 inline simdjson_result<std::vector<element>> at_path_with_wildcard(const std::string_view json_path) const noexcept;
435
449 inline simdjson_result<element> at_path(std::string_view json_path) const noexcept;
450
451#ifndef SIMDJSON_DISABLE_DEPRECATED_API
472 [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
473 inline simdjson_result<element> at(const std::string_view json_pointer) const noexcept;
474#endif // SIMDJSON_DISABLE_DEPRECATED_API
475
482 inline simdjson_result<element> at(size_t index) const noexcept;
483
496 inline simdjson_result<element> at_key(std::string_view key) const noexcept;
497
506 inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
507
514 inline bool operator<(const element &other) const noexcept;
515
522 inline bool operator==(const element &other) const noexcept;
523
525 inline bool dump_raw_tape(std::ostream &out) const noexcept;
526
527private:
528 simdjson_inline element(const internal::tape_ref &tape) noexcept;
529 internal::tape_ref tape{};
530 friend class document;
531 friend class object;
532 friend class array;
533 friend struct simdjson_result<element>;
534 template<typename T>
535 friend class simdjson::internal::string_builder;
536
537};
538
539} // namespace dom
540
542template<>
543struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom::element> {
544public:
545 simdjson_inline simdjson_result() noexcept;
546 simdjson_inline simdjson_result(dom::element &&value) noexcept;
547 simdjson_inline simdjson_result(error_code error) noexcept;
548
549 simdjson_inline simdjson_result<dom::element_type> type() const noexcept;
550 template<typename T>
551 simdjson_inline bool is() const noexcept;
552 template<typename T>
553 simdjson_inline simdjson_result<T> get() const noexcept;
554 template<typename T>
555 simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
556
557 simdjson_inline simdjson_result<dom::array> get_array() const noexcept;
558 simdjson_inline simdjson_result<dom::object> get_object() const noexcept;
559 simdjson_inline simdjson_result<const char *> get_c_str() const noexcept;
560 simdjson_inline simdjson_result<size_t> get_string_length() const noexcept;
561 simdjson_inline simdjson_result<std::string_view> get_string() const noexcept;
562 #if SIMDJSON_SUPPORTS_CHAR8_T
563 simdjson_inline simdjson_result<std::u8string_view> get_u8string() const noexcept;
564 #endif
565 simdjson_inline simdjson_result<int64_t> get_int64() const noexcept;
566 simdjson_inline simdjson_result<uint64_t> get_uint64() const noexcept;
567 simdjson_inline simdjson_result<double> get_double() const noexcept;
568 simdjson_inline simdjson_result<bool> get_bool() const noexcept;
569 simdjson_inline simdjson_result<std::string_view> get_bigint() const noexcept;
570
571 simdjson_inline bool is_array() const noexcept;
572 simdjson_inline bool is_object() const noexcept;
573 simdjson_inline bool is_string() const noexcept;
574 simdjson_inline bool is_int64() const noexcept;
575 simdjson_inline bool is_uint64() const noexcept;
576 simdjson_inline bool is_double() const noexcept;
577 simdjson_inline bool is_number() const noexcept;
578 simdjson_inline bool is_bool() const noexcept;
579 simdjson_inline bool is_null() const noexcept;
580 simdjson_inline bool is_bigint() const noexcept;
581
582 simdjson_inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
583 simdjson_inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
584 simdjson_result<dom::element> operator[](int) const noexcept = delete;
585 simdjson_inline simdjson_result<dom::element> at_pointer(const std::string_view json_pointer) const noexcept;
586 simdjson_inline simdjson_result<std::vector<dom::element>> at_path_with_wildcard(const std::string_view json_path) const noexcept;
587 simdjson_inline simdjson_result<dom::element> at_path(const std::string_view json_path) const noexcept;
588 [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
589 simdjson_inline simdjson_result<dom::element> at(const std::string_view json_pointer) const noexcept;
590 simdjson_inline simdjson_result<dom::element> at(size_t index) const noexcept;
591 simdjson_inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
592 simdjson_inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
593
594#if SIMDJSON_EXCEPTIONS
595 simdjson_inline operator bool() const noexcept(false);
596 simdjson_inline explicit operator const char*() const noexcept(false);
597 simdjson_inline operator std::string_view() const noexcept(false);
598 simdjson_inline operator uint64_t() const noexcept(false);
599 simdjson_inline operator int64_t() const noexcept(false);
600 simdjson_inline operator double() const noexcept(false);
601 simdjson_inline operator dom::array() const noexcept(false);
602 simdjson_inline operator dom::object() const noexcept(false);
603
604 simdjson_inline dom::array::iterator begin() const noexcept(false);
605 simdjson_inline dom::array::iterator end() const noexcept(false);
606#endif // SIMDJSON_EXCEPTIONS
607};
608
609} // namespace simdjson
610
611#endif // SIMDJSON_DOM_DOCUMENT_H
JSON array.
Definition array.h:15
A JSON element.
Definition element.h:36
bool is_object() const noexcept
Whether this element is a json object.
bool is_double() const noexcept
Whether this element is a json number that fits in a double.
simdjson_inline element_type type() const noexcept
The type of this element.
bool is_number() const noexcept
Whether this element is a json number.
simdjson_result< T > get() const noexcept
Get the value as the provided type (T).
Definition element.h:243
simdjson_result< const char * > get_c_str() const noexcept
Cast this element to a null-terminated C string.
simdjson_result< element > at_pointer(const std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
simdjson_result< double > get_double() const noexcept
Cast this element to a double floating-point.
simdjson_inline bool is() const noexcept
Tell whether the value can be cast to provided type (T).
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.
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.
bool is_string() const noexcept
Whether this element is a json string.
simdjson_result< element > at_key(std::string_view key) const noexcept
Get the value associated with the given key.
simdjson_result< std::string_view > get_bigint() const noexcept
Read this element as a big integer (raw digit string).
operator std::string_view() const noexcept(false)
Read this element as a null-terminated UTF-8 string.
dom::array::iterator end() const noexcept(false)
Iterate over each element in this array.
simdjson_result< array > get_array() const noexcept
Cast this element to an array.
simdjson_result< size_t > get_string_length() const noexcept
Gives the length in bytes of the string.
simdjson_inline element() noexcept
Create a new, invalid element.
simdjson_result< uint64_t > get_uint64() const noexcept
Cast this element to an unsigned integer.
dom::array::iterator begin() const noexcept(false)
Iterate over each element in this array.
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...
simdjson_result< int64_t > get_int64() const noexcept
Cast this element to a signed integer.
bool is_bigint() const noexcept
Whether this element is a big integer (number exceeding 64-bit range).
simdjson_result< object > get_object() const noexcept
Cast this element to an object.
bool is_uint64() const noexcept
Whether this element is a json number that fits in an unsigned 64-bit integer.
simdjson_result< element > operator[](std::string_view key) const noexcept
Get the value associated with the given key.
bool is_int64() const noexcept
Whether this element is a json number that fits in a signed 64-bit integer.
bool is_null() const noexcept
Whether this element is a json null.
simdjson_result< element > at_path(std::string_view json_path) const noexcept
Get the value associated with the given JSONPath expression.
simdjson_result< std::string_view > get_string() const noexcept
Cast this element to a string.
bool is_array() const noexcept
Whether this element is a json array.
bool is_bool() const noexcept
Whether this element is a json true or false.
JSON object.
Definition object.h:16
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:16
@ STRING
std::string_view
@ UINT64
uint64_t: any integer that fits in uint64_t but not int64_t
@ BIGINT
The BIGINT type is for integers that do not fit in 64 bits. It is only present.
@ 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 simdjson operation that could fail.
Definition error.h:281
simdjson_inline error_code error() const noexcept
The error.
Definition error-inl.h:168
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition error-inl.h:163