1 #ifndef SIMDJSON_ELEMENT_INL_H
2 #define SIMDJSON_ELEMENT_INL_H
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/element.h"
6 #include "simdjson/dom/document.h"
7 #include "simdjson/dom/object.h"
8 #include "simdjson/internal/tape_type.h"
10 #include "simdjson/dom/object-inl.h"
11 #include "simdjson/error-inl.h"
12 #include "simdjson/jsonpathutil.h"
22 simdjson_inline simdjson_result<dom::element>::simdjson_result() noexcept
23 : internal::simdjson_result_base<dom::element>() {}
24 simdjson_inline simdjson_result<dom::element>::simdjson_result(dom::element &&value) noexcept
25 : internal::simdjson_result_base<dom::element>(std::forward<dom::element>(value)) {}
26 simdjson_inline simdjson_result<dom::element>::simdjson_result(
error_code error) noexcept
27 : internal::simdjson_result_base<dom::element>(error) {}
28 inline simdjson_result<dom::element_type> simdjson_result<dom::element>::type() const noexcept {
34 simdjson_inline
bool simdjson_result<dom::element>::is() const noexcept {
35 return !
error() && first.is<T>();
40 return first.get<T>();
44 if (error()) {
return error(); }
45 return first.get<T>(value);
48 simdjson_inline simdjson_result<dom::array> simdjson_result<dom::element>::get_array() const noexcept {
50 return first.get_array();
52 simdjson_inline simdjson_result<dom::object> simdjson_result<dom::element>::get_object() const noexcept {
54 return first.get_object();
56 simdjson_inline simdjson_result<const char *> simdjson_result<dom::element>::get_c_str() const noexcept {
58 return first.get_c_str();
60 simdjson_inline simdjson_result<size_t> simdjson_result<dom::element>::get_string_length() const noexcept {
62 return first.get_string_length();
64 simdjson_inline simdjson_result<std::string_view> simdjson_result<dom::element>::get_string() const noexcept {
66 return first.get_string();
68 simdjson_inline simdjson_result<int64_t> simdjson_result<dom::element>::get_int64() const noexcept {
70 return first.get_int64();
72 simdjson_inline simdjson_result<uint64_t> simdjson_result<dom::element>::get_uint64() const noexcept {
74 return first.get_uint64();
76 simdjson_inline simdjson_result<double> simdjson_result<dom::element>::get_double() const noexcept {
78 return first.get_double();
80 simdjson_inline simdjson_result<bool> simdjson_result<dom::element>::get_bool() const noexcept {
82 return first.get_bool();
85 simdjson_inline
bool simdjson_result<dom::element>::is_array() const noexcept {
86 return !
error() && first.is_array();
88 simdjson_inline
bool simdjson_result<dom::element>::is_object() const noexcept {
89 return !
error() && first.is_object();
91 simdjson_inline
bool simdjson_result<dom::element>::is_string() const noexcept {
92 return !
error() && first.is_string();
94 simdjson_inline
bool simdjson_result<dom::element>::is_int64() const noexcept {
95 return !
error() && first.is_int64();
97 simdjson_inline
bool simdjson_result<dom::element>::is_uint64() const noexcept {
98 return !
error() && first.is_uint64();
100 simdjson_inline
bool simdjson_result<dom::element>::is_double() const noexcept {
101 return !
error() && first.is_double();
103 simdjson_inline
bool simdjson_result<dom::element>::is_number() const noexcept {
104 return !
error() && first.is_number();
106 simdjson_inline
bool simdjson_result<dom::element>::is_bool() const noexcept {
107 return !
error() && first.is_bool();
110 simdjson_inline
bool simdjson_result<dom::element>::is_null() const noexcept {
111 return !
error() && first.is_null();
114 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](std::string_view key)
const noexcept {
115 if (error()) {
return error(); }
118 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](
const char *key)
const noexcept {
119 if (error()) {
return error(); }
122 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_pointer(
const std::string_view json_pointer)
const noexcept {
123 if (error()) {
return error(); }
124 return first.at_pointer(json_pointer);
126 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_path(
const std::string_view json_path)
const noexcept {
129 return at_pointer(json_pointer);
131 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
132 [[deprecated(
"For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
133 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at(
const std::string_view json_pointer)
const noexcept {
134 SIMDJSON_PUSH_DISABLE_WARNINGS
135 SIMDJSON_DISABLE_DEPRECATED_WARNING
136 if (error()) {
return error(); }
137 return first.at(json_pointer);
138 SIMDJSON_POP_DISABLE_WARNINGS
141 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at(
size_t index)
const noexcept {
142 if (error()) {
return error(); }
143 return first.at(index);
145 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_key(std::string_view key)
const noexcept {
146 if (error()) {
return error(); }
147 return first.at_key(key);
149 simdjson_inline simdjson_result<dom::element> simdjson_result<dom::element>::at_key_case_insensitive(std::string_view key)
const noexcept {
150 if (error()) {
return error(); }
151 return first.at_key_case_insensitive(key);
154 #if SIMDJSON_EXCEPTIONS
156 simdjson_inline simdjson_result<dom::element>::operator bool() const noexcept(false) {
159 simdjson_inline simdjson_result<dom::element>::operator
const char *()
const noexcept(
false) {
160 return get<const char *>();
162 simdjson_inline simdjson_result<dom::element>::operator std::string_view() const noexcept(false) {
163 return get<std::string_view>();
165 simdjson_inline simdjson_result<dom::element>::operator uint64_t() const noexcept(false) {
166 return get<uint64_t>();
168 simdjson_inline simdjson_result<dom::element>::operator int64_t() const noexcept(false) {
169 return get<int64_t>();
171 simdjson_inline simdjson_result<dom::element>::operator double() const noexcept(false) {
172 return get<double>();
174 simdjson_inline simdjson_result<dom::element>::operator dom::array() const noexcept(false) {
175 return get<dom::array>();
177 simdjson_inline simdjson_result<dom::element>::operator dom::object() const noexcept(false) {
178 return get<dom::object>();
181 simdjson_inline dom::array::iterator simdjson_result<dom::element>::begin() const noexcept(false) {
182 if (
error()) {
throw simdjson_error(
error()); }
183 return first.begin();
185 simdjson_inline dom::array::iterator simdjson_result<dom::element>::end() const noexcept(false) {
186 if (
error()) {
throw simdjson_error(
error()); }
198 simdjson_inline
element::element(
const internal::tape_ref &_tape) noexcept : tape{_tape} { }
201 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
202 auto tape_type = tape.tape_ref_type();
207 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
210 }
else if(tape.is_false()) {
216 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
217 switch (tape.tape_ref_type()) {
218 case internal::tape_type::STRING: {
219 return tape.get_c_str();
226 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
227 switch (tape.tape_ref_type()) {
228 case internal::tape_type::STRING: {
229 return tape.get_string_length();
236 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
237 switch (tape.tape_ref_type()) {
238 case internal::tape_type::STRING:
239 return tape.get_string_view();
245 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
246 if(simdjson_unlikely(!tape.is_uint64())) {
247 if(tape.is_int64()) {
248 int64_t result = tape.next_tape_value<int64_t>();
252 return uint64_t(result);
256 return tape.next_tape_value<int64_t>();
259 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
260 if(simdjson_unlikely(!tape.is_int64())) {
261 if(tape.is_uint64()) {
262 uint64_t result = tape.next_tape_value<uint64_t>();
264 if (result > uint64_t((std::numeric_limits<int64_t>::max)())) {
267 return static_cast<int64_t
>(result);
271 return tape.next_tape_value<int64_t>();
274 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
284 if(simdjson_unlikely(!tape.is_double())) {
285 if(tape.is_uint64()) {
286 return double(tape.next_tape_value<uint64_t>());
287 }
else if(tape.is_int64()) {
288 return double(tape.next_tape_value<int64_t>());
293 return tape.next_tape_value<
double>();
296 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
297 switch (tape.tape_ref_type()) {
298 case internal::tape_type::START_ARRAY:
305 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
306 switch (tape.tape_ref_type()) {
307 case internal::tape_type::START_OBJECT:
316 return get<T>().get(value);
320 simdjson_warn_unused simdjson_inline
error_code element::get<element>(
element &value)
const noexcept {
326 error = get<T>(value);
331 auto result = get<T>();
332 return !result.error();
337 template<>
inline simdjson_result<const char *> element::get<const char *>() const noexcept {
return get_c_str(); }
338 template<>
inline simdjson_result<std::string_view> element::get<std::string_view>() const noexcept {
return get_string(); }
339 template<>
inline simdjson_result<int64_t> element::get<int64_t>() const noexcept {
return get_int64(); }
340 template<>
inline simdjson_result<uint64_t> element::get<uint64_t>() const noexcept {
return get_uint64(); }
341 template<>
inline simdjson_result<double> element::get<double>() const noexcept {
return get_double(); }
342 template<>
inline simdjson_result<bool> element::get<bool>() const noexcept {
return get_bool(); }
354 return tape.is_null_on_tape();
357 #if SIMDJSON_EXCEPTIONS
359 inline element::operator bool() const noexcept(false) {
return get<bool>(); }
360 inline element::operator
const char*()
const noexcept(
false) {
return get<const char *>(); }
361 inline element::operator std::string_view() const noexcept(false) {
return get<std::string_view>(); }
362 inline element::operator uint64_t() const noexcept(false) {
return get<uint64_t>(); }
363 inline element::operator int64_t() const noexcept(false) {
return get<int64_t>(); }
364 inline element::operator double() const noexcept(false) {
return get<double>(); }
365 inline element::operator
array() const noexcept(false) {
return get<array>(); }
366 inline element::operator
object() const noexcept(false) {
return get<object>(); }
369 return get<array>().begin();
372 return get<array>().end();
384 inline bool is_pointer_well_formed(std::string_view json_pointer) noexcept {
385 if (simdjson_unlikely(json_pointer[0] !=
'/')) {
388 size_t escape = json_pointer.find(
'~');
389 if (escape == std::string_view::npos) {
392 if (escape == json_pointer.size() - 1) {
395 if (json_pointer[escape + 1] !=
'0' && json_pointer[escape + 1] !=
'1') {
402 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
403 switch (tape.tape_ref_type()) {
404 case internal::tape_type::START_OBJECT:
406 case internal::tape_type::START_ARRAY:
409 if (!json_pointer.empty()) {
410 if (is_pointer_well_formed(json_pointer)) {
424 return at_pointer(json_pointer);
426 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
427 [[deprecated(
"For standard compliance, use at_pointer instead, and prefix your pointers with a slash '/', see RFC6901 ")]]
430 auto std_pointer = (json_pointer.empty() ?
"" :
"/") + std::string(json_pointer.begin(), json_pointer.end());
431 return at_pointer(std_pointer);
436 return get<array>().at(index);
439 return get<object>().at_key(key);
442 return get<object>().at_key_case_insensitive(key);
445 return tape.json_index < other.tape.json_index;
448 return tape.json_index == other.tape.json_index;
451 inline bool element::dump_raw_tape(std::ostream &out)
const noexcept {
452 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
453 return tape.doc->dump_raw_tape(out);
457 inline std::ostream& operator<<(std::ostream& out,
element_type type) {
460 return out <<
"array";
462 return out <<
"object";
464 return out <<
"int64_t";
466 return out <<
"uint64_t";
468 return out <<
"double";
470 return out <<
"string";
472 return out <<
"bool";
474 return out <<
"null";
476 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.
bool operator<(const element &other) const noexcept
operator< defines a total order for element allowing to use them in ordered C++ STL containers
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< 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 operator==(const element &other) const noexcept
operator== allows to verify if two element values reference the same JSON item
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.
element_type
The actual concrete type of a JSON element This is the type it is most easily cast to with get<>.
@ 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.
error_code
All possible errors returned by simdjson.
@ INCORRECT_TYPE
JSON element has a different type than user expected.
@ NO_SUCH_FIELD
JSON field not found in object.
@ NUMBER_OUT_OF_RANGE
JSON number does not fit in 64 bits.
@ INVALID_JSON_POINTER
Invalid JSON pointer syntax.
std::string json_path_to_pointer_conversion(std::string_view json_path)
Converts JSONPath to JSON Pointer.
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.