simdjson 4.6.4
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',
25 BIGINT = 'Z'
26};
27
34class element {
35public:
37 simdjson_inline element() noexcept;
38
40 simdjson_inline element_type type() const noexcept;
41
48 inline simdjson_result<array> get_array() const noexcept;
55 inline simdjson_result<object> get_object() const noexcept;
72 inline simdjson_result<const char *> get_c_str() const noexcept;
82 inline simdjson_result<size_t> get_string_length() const noexcept;
92 inline simdjson_result<std::string_view> get_string() const noexcept;
100 inline simdjson_result<int64_t> get_int64() const noexcept;
108 inline simdjson_result<uint64_t> get_uint64() const noexcept;
115 inline simdjson_result<double> get_double() const noexcept;
122 inline simdjson_result<bool> get_bool() const noexcept;
123
130 inline simdjson_result<std::string_view> get_bigint() const noexcept;
131
137 inline bool is_array() const noexcept;
143 inline bool is_object() const noexcept;
149 inline bool is_string() const noexcept;
155 inline bool is_int64() const noexcept;
161 inline bool is_uint64() const noexcept;
167 inline bool is_double() const noexcept;
168
174 inline bool is_number() const noexcept;
175
181 inline bool is_bool() const noexcept;
185 inline bool is_null() const noexcept;
186
190 inline bool is_bigint() const noexcept;
191
204 template<typename T>
205 simdjson_inline bool is() const noexcept;
206
226 template<typename T>
227 inline simdjson_result<T> get() const noexcept {
228 // Unless the simdjson library provides an inline implementation, calling this method should
229 // immediately fail.
230 static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library. "
231 "The supported types are Boolean (bool), numbers (double, uint64_t, int64_t), "
232 "strings (std::string_view, const char *), arrays (dom::array) and objects (dom::object). "
233 "We recommend you use get_double(), get_bool(), get_uint64(), get_int64(), "
234 "get_object(), get_array() or get_string() instead of the get template.");
235 }
236
253 template<typename T>
254 simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
255
271 template<typename T>
272 inline void tie(T &value, error_code &error) && noexcept;
273
274#if SIMDJSON_EXCEPTIONS
281 inline operator bool() const noexcept(false);
282
294 inline explicit operator const char*() const noexcept(false);
295
305 inline operator std::string_view() const noexcept(false);
306
314 inline operator uint64_t() const noexcept(false);
322 inline operator int64_t() const noexcept(false);
329 inline operator double() const noexcept(false);
336 inline operator array() const noexcept(false);
343 inline operator object() const noexcept(false);
344
351 inline dom::array::iterator begin() const noexcept(false);
352
359 inline dom::array::iterator end() const noexcept(false);
360#endif // SIMDJSON_EXCEPTIONS
361
375 inline simdjson_result<element> operator[](std::string_view key) const noexcept;
376
390 inline simdjson_result<element> operator[](const char *key) const noexcept;
391 simdjson_result<element> operator[](int) const noexcept = delete;
392
393
416 inline simdjson_result<element> at_pointer(const std::string_view json_pointer) const noexcept;
417
418 inline simdjson_result<std::vector<element>> at_path_with_wildcard(const std::string_view json_path) const noexcept;
419
433 inline simdjson_result<element> at_path(std::string_view json_path) const noexcept;
434
435#ifndef SIMDJSON_DISABLE_DEPRECATED_API
456 [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
457 inline simdjson_result<element> at(const std::string_view json_pointer) const noexcept;
458#endif // SIMDJSON_DISABLE_DEPRECATED_API
459
466 inline simdjson_result<element> at(size_t index) const noexcept;
467
480 inline simdjson_result<element> at_key(std::string_view key) const noexcept;
481
490 inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
491
498 inline bool operator<(const element &other) const noexcept;
499
506 inline bool operator==(const element &other) const noexcept;
507
509 inline bool dump_raw_tape(std::ostream &out) const noexcept;
510
511private:
512 simdjson_inline element(const internal::tape_ref &tape) noexcept;
513 internal::tape_ref tape{};
514 friend class document;
515 friend class object;
516 friend class array;
517 friend struct simdjson_result<element>;
518 template<typename T>
519 friend class simdjson::internal::string_builder;
520
521};
522
523} // namespace dom
524
526template<>
527struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom::element> {
528public:
529 simdjson_inline simdjson_result() noexcept;
530 simdjson_inline simdjson_result(dom::element &&value) noexcept;
531 simdjson_inline simdjson_result(error_code error) noexcept;
532
533 simdjson_inline simdjson_result<dom::element_type> type() const noexcept;
534 template<typename T>
535 simdjson_inline bool is() const noexcept;
536 template<typename T>
537 simdjson_inline simdjson_result<T> get() const noexcept;
538 template<typename T>
539 simdjson_warn_unused simdjson_inline error_code get(T &value) const noexcept;
540
541 simdjson_inline simdjson_result<dom::array> get_array() const noexcept;
542 simdjson_inline simdjson_result<dom::object> get_object() const noexcept;
543 simdjson_inline simdjson_result<const char *> get_c_str() const noexcept;
544 simdjson_inline simdjson_result<size_t> get_string_length() const noexcept;
545 simdjson_inline simdjson_result<std::string_view> get_string() const noexcept;
546 simdjson_inline simdjson_result<int64_t> get_int64() const noexcept;
547 simdjson_inline simdjson_result<uint64_t> get_uint64() const noexcept;
548 simdjson_inline simdjson_result<double> get_double() const noexcept;
549 simdjson_inline simdjson_result<bool> get_bool() const noexcept;
550 simdjson_inline simdjson_result<std::string_view> get_bigint() const noexcept;
551
552 simdjson_inline bool is_array() const noexcept;
553 simdjson_inline bool is_object() const noexcept;
554 simdjson_inline bool is_string() const noexcept;
555 simdjson_inline bool is_int64() const noexcept;
556 simdjson_inline bool is_uint64() const noexcept;
557 simdjson_inline bool is_double() const noexcept;
558 simdjson_inline bool is_number() const noexcept;
559 simdjson_inline bool is_bool() const noexcept;
560 simdjson_inline bool is_null() const noexcept;
561 simdjson_inline bool is_bigint() const noexcept;
562
563 simdjson_inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
564 simdjson_inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
565 simdjson_result<dom::element> operator[](int) const noexcept = delete;
566 simdjson_inline simdjson_result<dom::element> at_pointer(const std::string_view json_pointer) const noexcept;
567 simdjson_inline simdjson_result<std::vector<dom::element>> at_path_with_wildcard(const std::string_view json_path) const noexcept;
568 simdjson_inline simdjson_result<dom::element> at_path(const std::string_view json_path) const noexcept;
569 [[deprecated("For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
570 simdjson_inline simdjson_result<dom::element> at(const std::string_view json_pointer) const noexcept;
571 simdjson_inline simdjson_result<dom::element> at(size_t index) const noexcept;
572 simdjson_inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
573 simdjson_inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
574
575#if SIMDJSON_EXCEPTIONS
576 simdjson_inline operator bool() const noexcept(false);
577 simdjson_inline explicit operator const char*() const noexcept(false);
578 simdjson_inline operator std::string_view() const noexcept(false);
579 simdjson_inline operator uint64_t() const noexcept(false);
580 simdjson_inline operator int64_t() const noexcept(false);
581 simdjson_inline operator double() const noexcept(false);
582 simdjson_inline operator dom::array() const noexcept(false);
583 simdjson_inline operator dom::object() const noexcept(false);
584
585 simdjson_inline dom::array::iterator begin() const noexcept(false);
586 simdjson_inline dom::array::iterator end() const noexcept(false);
587#endif // SIMDJSON_EXCEPTIONS
588};
589
590} // namespace simdjson
591
592#endif // SIMDJSON_DOM_DOCUMENT_H
JSON array.
Definition array.h:15
A JSON element.
Definition element.h:34
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:227
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
std::string_view: big integer stored as raw digit string
@ 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:280
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