1 #ifndef SIMDJSON_INLINE_ELEMENT_H
2 #define SIMDJSON_INLINE_ELEMENT_H
4 #include "simdjson/dom/array.h"
5 #include "simdjson/dom/element.h"
6 #include "simdjson/dom/object.h"
15 simdjson_inline simdjson_result<dom::element>::simdjson_result() noexcept
16 : internal::simdjson_result_base<dom::element>() {}
17 simdjson_inline simdjson_result<dom::element>::simdjson_result(dom::element &&value) noexcept
18 : internal::simdjson_result_base<dom::element>(std::forward<dom::element>(value)) {}
19 simdjson_inline simdjson_result<dom::element>::simdjson_result(
error_code error) noexcept
20 : internal::simdjson_result_base<dom::element>(error) {}
21 inline simdjson_result<dom::element_type> simdjson_result<dom::element>::type() const noexcept {
27 simdjson_inline
bool simdjson_result<dom::element>::is() const noexcept {
28 return !
error() && first.is<T>();
33 return first.get<T>();
37 if (error()) {
return error(); }
38 return first.get<T>(value);
41 simdjson_inline simdjson_result<dom::array> simdjson_result<dom::element>::get_array() const noexcept {
43 return first.get_array();
45 simdjson_inline simdjson_result<dom::object> simdjson_result<dom::element>::get_object() const noexcept {
47 return first.get_object();
49 simdjson_inline simdjson_result<const char *> simdjson_result<dom::element>::get_c_str() const noexcept {
51 return first.get_c_str();
53 simdjson_inline simdjson_result<size_t> simdjson_result<dom::element>::get_string_length() const noexcept {
55 return first.get_string_length();
57 simdjson_inline simdjson_result<std::string_view> simdjson_result<dom::element>::get_string() const noexcept {
59 return first.get_string();
61 simdjson_inline simdjson_result<int64_t> simdjson_result<dom::element>::get_int64() const noexcept {
63 return first.get_int64();
65 simdjson_inline simdjson_result<uint64_t> simdjson_result<dom::element>::get_uint64() const noexcept {
67 return first.get_uint64();
69 simdjson_inline simdjson_result<double> simdjson_result<dom::element>::get_double() const noexcept {
71 return first.get_double();
73 simdjson_inline simdjson_result<bool> simdjson_result<dom::element>::get_bool() const noexcept {
75 return first.get_bool();
78 simdjson_inline
bool simdjson_result<dom::element>::is_array() const noexcept {
79 return !
error() && first.is_array();
81 simdjson_inline
bool simdjson_result<dom::element>::is_object() const noexcept {
82 return !
error() && first.is_object();
84 simdjson_inline
bool simdjson_result<dom::element>::is_string() const noexcept {
85 return !
error() && first.is_string();
87 simdjson_inline
bool simdjson_result<dom::element>::is_int64() const noexcept {
88 return !
error() && first.is_int64();
90 simdjson_inline
bool simdjson_result<dom::element>::is_uint64() const noexcept {
91 return !
error() && first.is_uint64();
93 simdjson_inline
bool simdjson_result<dom::element>::is_double() const noexcept {
94 return !
error() && first.is_double();
96 simdjson_inline
bool simdjson_result<dom::element>::is_number() const noexcept {
97 return !
error() && first.is_number();
99 simdjson_inline
bool simdjson_result<dom::element>::is_bool() const noexcept {
100 return !
error() && first.is_bool();
103 simdjson_inline
bool simdjson_result<dom::element>::is_null() const noexcept {
104 return !
error() && first.is_null();
107 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](std::string_view key)
const noexcept {
108 if (error()) {
return error(); }
111 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](
const char *key)
const noexcept {
112 if (error()) {
return error(); }
115 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_pointer(
const std::string_view json_pointer)
const noexcept {
116 if (error()) {
return error(); }
117 return first.at_pointer(json_pointer);
119 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
120 [[deprecated(
"For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
121 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at(
const std::string_view json_pointer)
const noexcept {
122 SIMDJSON_PUSH_DISABLE_WARNINGS
123 SIMDJSON_DISABLE_DEPRECATED_WARNING
124 if (error()) {
return error(); }
125 return first.at(json_pointer);
126 SIMDJSON_POP_DISABLE_WARNINGS
129 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at(
size_t index)
const noexcept {
130 if (error()) {
return error(); }
131 return first.at(index);
133 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_key(std::string_view key)
const noexcept {
134 if (error()) {
return error(); }
135 return first.at_key(key);
137 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_key_case_insensitive(std::string_view key)
const noexcept {
138 if (error()) {
return error(); }
139 return first.at_key_case_insensitive(key);
142 #if SIMDJSON_EXCEPTIONS
144 simdjson_inline simdjson_result<dom::element>::operator bool() const noexcept(false) {
147 simdjson_inline simdjson_result<dom::element>::operator
const char *()
const noexcept(
false) {
148 return get<const char *>();
150 simdjson_inline simdjson_result<dom::element>::operator std::string_view() const noexcept(false) {
151 return get<std::string_view>();
153 simdjson_inline simdjson_result<dom::element>::operator uint64_t() const noexcept(false) {
154 return get<uint64_t>();
156 simdjson_inline simdjson_result<dom::element>::operator int64_t() const noexcept(false) {
157 return get<int64_t>();
159 simdjson_inline simdjson_result<dom::element>::operator double() const noexcept(false) {
160 return get<double>();
162 simdjson_inline simdjson_result<dom::element>::operator dom::array() const noexcept(false) {
163 return get<dom::array>();
165 simdjson_inline simdjson_result<dom::element>::operator dom::object() const noexcept(false) {
166 return get<dom::object>();
169 simdjson_inline dom::array::iterator simdjson_result<dom::element>::begin() const noexcept(false) {
170 if (
error()) {
throw simdjson_error(
error()); }
171 return first.begin();
173 simdjson_inline dom::array::iterator simdjson_result<dom::element>::end() const noexcept(false) {
174 if (
error()) {
throw simdjson_error(
error()); }
186 simdjson_inline
element::element(
const internal::tape_ref &_tape) noexcept : tape{_tape} { }
189 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
190 auto tape_type = tape.tape_ref_type();
191 return tape_type == internal::tape_type::FALSE_VALUE ? element_type::BOOL :
static_cast<element_type
>(tape_type);
195 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
198 }
else if(tape.is_false()) {
204 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
205 switch (tape.tape_ref_type()) {
206 case internal::tape_type::STRING: {
207 return tape.get_c_str();
214 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
215 switch (tape.tape_ref_type()) {
216 case internal::tape_type::STRING: {
217 return tape.get_string_length();
224 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
225 switch (tape.tape_ref_type()) {
226 case internal::tape_type::STRING:
227 return tape.get_string_view();
233 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
234 if(simdjson_unlikely(!tape.is_uint64())) {
235 if(tape.is_int64()) {
236 int64_t result = tape.next_tape_value<int64_t>();
240 return uint64_t(result);
244 return tape.next_tape_value<int64_t>();
247 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
248 if(simdjson_unlikely(!tape.is_int64())) {
249 if(tape.is_uint64()) {
250 uint64_t result = tape.next_tape_value<uint64_t>();
252 if (result > uint64_t((std::numeric_limits<int64_t>::max)())) {
255 return static_cast<int64_t
>(result);
259 return tape.next_tape_value<int64_t>();
262 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
272 if(simdjson_unlikely(!tape.is_double())) {
273 if(tape.is_uint64()) {
274 return double(tape.next_tape_value<uint64_t>());
275 }
else if(tape.is_int64()) {
276 return double(tape.next_tape_value<int64_t>());
281 return tape.next_tape_value<
double>();
284 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
285 switch (tape.tape_ref_type()) {
286 case internal::tape_type::START_ARRAY:
293 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
294 switch (tape.tape_ref_type()) {
295 case internal::tape_type::START_OBJECT:
304 return get<T>().get(value);
308 simdjson_warn_unused simdjson_inline
error_code element::get<element>(
element &value)
const noexcept {
314 error = get<T>(value);
319 auto result = get<T>();
320 return !result.error();
325 template<>
inline simdjson_result<const char *> element::get<const char *>() const noexcept {
return get_c_str(); }
326 template<>
inline simdjson_result<std::string_view> element::get<std::string_view>() const noexcept {
return get_string(); }
327 template<>
inline simdjson_result<int64_t> element::get<int64_t>() const noexcept {
return get_int64(); }
328 template<>
inline simdjson_result<uint64_t> element::get<uint64_t>() const noexcept {
return get_uint64(); }
329 template<>
inline simdjson_result<double> element::get<double>() const noexcept {
return get_double(); }
330 template<>
inline simdjson_result<bool> element::get<bool>() const noexcept {
return get_bool(); }
342 return tape.is_null_on_tape();
345 #if SIMDJSON_EXCEPTIONS
347 inline element::operator bool() const noexcept(false) {
return get<bool>(); }
348 inline element::operator
const char*()
const noexcept(
false) {
return get<const char *>(); }
349 inline element::operator std::string_view() const noexcept(false) {
return get<std::string_view>(); }
350 inline element::operator uint64_t() const noexcept(false) {
return get<uint64_t>(); }
351 inline element::operator int64_t() const noexcept(false) {
return get<int64_t>(); }
352 inline element::operator double() const noexcept(false) {
return get<double>(); }
353 inline element::operator
array() const noexcept(false) {
return get<array>(); }
354 inline element::operator
object() const noexcept(false) {
return get<object>(); }
357 return get<array>().begin();
360 return get<array>().end();
373 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
374 switch (tape.tape_ref_type()) {
375 case internal::tape_type::START_OBJECT:
377 case internal::tape_type::START_ARRAY:
380 if(!json_pointer.empty()) {
389 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
390 [[deprecated(
"For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
393 auto std_pointer = (json_pointer.empty() ?
"" :
"/") + std::string(json_pointer.begin(), json_pointer.end());
394 return at_pointer(std_pointer);
399 return get<array>().at(index);
402 return get<object>().at_key(key);
405 return get<object>().at_key_case_insensitive(key);
408 inline bool element::dump_raw_tape(std::ostream &out)
const noexcept {
409 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
410 return tape.doc->dump_raw_tape(out);
414 inline std::ostream&
operator<<(std::ostream& out, element_type type) {
416 case element_type::ARRAY:
417 return out <<
"array";
418 case element_type::OBJECT:
419 return out <<
"object";
420 case element_type::INT64:
421 return out <<
"int64_t";
422 case element_type::UINT64:
423 return out <<
"uint64_t";
424 case element_type::DOUBLE:
425 return out <<
"double";
426 case element_type::STRING:
427 return out <<
"string";
428 case element_type::BOOL:
429 return out <<
"bool";
430 case element_type::NULL_VALUE:
431 return out <<
"null";
433 return out <<
"unexpected content!!!";
simdjson_result< element > at_pointer(std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
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< 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_result< T > get() const noexcept
Get the value as the provided type (T).
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.
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.
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< 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.
simdjson_result< element > at_pointer(std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
We want to support argument-dependent lookup (ADL).
std::ostream & operator<<(std::ostream &out, error_code error) noexcept
Write the error message to the output stream.
error_code
All possible errors returned by simdjson.
@ INCORRECT_TYPE
JSON element has a different type than user expected.
@ NUMBER_OUT_OF_RANGE
JSON number does not fit in 64 bits.
@ INVALID_JSON_POINTER
Invalid JSON pointer reference.
The result of a simdjson operation that could fail.
simdjson_inline error_code error() const noexcept
The error.
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.