1 #ifndef SIMDJSON_OBJECT_INL_H
2 #define SIMDJSON_OBJECT_INL_H
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/object.h"
6 #include "simdjson/dom/document.h"
8 #include "simdjson/dom/element-inl.h"
9 #include "simdjson/error-inl.h"
10 #include "simdjson/jsonpathutil.h"
19 simdjson_inline simdjson_result<dom::object>::simdjson_result() noexcept
20 : internal::simdjson_result_base<dom::
object>() {}
21 simdjson_inline simdjson_result<dom::object>::simdjson_result(dom::object value) noexcept
22 : internal::simdjson_result_base<dom::object>(std::forward<dom::object>(value)) {}
23 simdjson_inline simdjson_result<dom::object>::simdjson_result(
error_code error) noexcept
24 : internal::simdjson_result_base<dom::object>(error) {}
26 inline simdjson_result<dom::element> simdjson_result<dom::object>::operator[](std::string_view key)
const noexcept {
27 if (error()) {
return error(); }
30 inline simdjson_result<dom::element> simdjson_result<dom::object>::operator[](
const char *key)
const noexcept {
31 if (error()) {
return error(); }
34 inline simdjson_result<dom::element> simdjson_result<dom::object>::at_pointer(std::string_view json_pointer)
const noexcept {
35 if (error()) {
return error(); }
36 return first.at_pointer(json_pointer);
38 inline simdjson_result<dom::element> simdjson_result<dom::object>::at_path(std::string_view json_path)
const noexcept {
41 return at_pointer(json_pointer);
43 inline simdjson_result<dom::element> simdjson_result<dom::object>::at_key(std::string_view key)
const noexcept {
44 if (error()) {
return error(); }
45 return first.at_key(key);
47 inline simdjson_result<dom::element> simdjson_result<dom::object>::at_key_case_insensitive(std::string_view key)
const noexcept {
48 if (error()) {
return error(); }
49 return first.at_key_case_insensitive(key);
52 #if SIMDJSON_EXCEPTIONS
54 inline dom::object::iterator simdjson_result<dom::object>::begin() const noexcept(false) {
55 if (
error()) {
throw simdjson_error(
error()); }
58 inline dom::object::iterator simdjson_result<dom::object>::end() const noexcept(false) {
59 if (
error()) {
throw simdjson_error(
error()); }
62 inline size_t simdjson_result<dom::object>::size() const noexcept(false) {
63 if (
error()) {
throw simdjson_error(
error()); }
75 simdjson_inline
object::object(
const internal::tape_ref &_tape) noexcept : tape{_tape} { }
77 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
78 return internal::tape_ref(tape.doc, tape.json_index + 1);
81 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
82 return internal::tape_ref(tape.doc, tape.after_element() - 1);
85 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
86 return tape.scope_count();
96 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
97 if(json_pointer.empty()) {
99 }
else if(json_pointer[0] !=
'/') {
102 json_pointer = json_pointer.substr(1);
103 size_t slash = json_pointer.find(
'/');
104 std::string_view key = json_pointer.substr(0, slash);
109 size_t escape = key.find(
'~');
110 if (escape != std::string_view::npos) {
112 std::string unescaped(key);
114 switch (unescaped[escape+1]) {
116 unescaped.replace(escape, 2,
"~");
119 unescaped.replace(escape, 2,
"/");
124 escape = unescaped.find(
'~', escape+1);
125 }
while (escape != std::string::npos);
126 child = at_key(unescaped);
134 if (slash != std::string_view::npos) {
135 child = child.at_pointer(json_pointer.substr(slash));
143 return at_pointer(json_pointer);
148 for (
iterator field = begin(); field != end_field; ++field) {
149 if (field.key_equals(key)) {
150 return field.value();
160 for (
iterator field = begin(); field != end_field; ++field) {
161 if (field.key_equals_case_insensitive(key)) {
162 return field.value();
168 inline object::operator
element() const noexcept {
175 simdjson_inline object::iterator::iterator(
const internal::tape_ref &_tape) noexcept : tape{_tape} { }
180 return tape.json_index != other.tape.json_index;
182 inline bool object::iterator::operator==(
const object::iterator& other)
const noexcept {
183 return tape.json_index == other.tape.json_index;
185 inline bool object::iterator::operator<(
const object::iterator& other)
const noexcept {
186 return tape.json_index < other.tape.json_index;
188 inline bool object::iterator::operator<=(
const object::iterator& other)
const noexcept {
189 return tape.json_index <= other.tape.json_index;
191 inline bool object::iterator::operator>=(
const object::iterator& other)
const noexcept {
192 return tape.json_index >= other.tape.json_index;
194 inline bool object::iterator::operator>(
const object::iterator& other)
const noexcept {
195 return tape.json_index > other.tape.json_index;
199 tape.json_index = tape.after_element();
208 return tape.get_string_view();
211 return tape.get_string_length();
214 return reinterpret_cast<const char *
>(&tape.doc->string_buf[size_t(tape.tape_value()) +
sizeof(uint32_t)]);
217 return element(internal::tape_ref(tape.doc, tape.json_index + 1));
236 const uint32_t len = key_length();
237 if(o.size() == len) {
239 return (memcmp(o.data(), key_c_str(), len) == 0);
247 const uint32_t len = key_length();
248 if(o.size() == len) {
252 return (simdjson_strncasecmp(o.data(), key_c_str(), len) == 0);
259 inline key_value_pair::key_value_pair(std::string_view _key,
element _value) noexcept :
260 key(_key), value(_value) {}
266 #if defined(__cpp_lib_ranges)
267 static_assert(std::ranges::view<simdjson::dom::object>);
268 static_assert(std::ranges::sized_range<simdjson::dom::object>);
269 #if SIMDJSON_EXCEPTIONS
Key/value pair in an object.
bool operator!=(const iterator &other) const noexcept
Check if these values come from the same place in the JSON.
element value() const noexcept
Get the value of this key/value pair.
bool key_equals(std::string_view o) const noexcept
Returns true if the key in this key/value pair is equal to the provided string_view.
const char * key_c_str() const noexcept
Get the key of this key/value pair.
uint32_t key_length() const noexcept
Get the length (in bytes) of the key in this key/value pair.
reference operator*() const noexcept
Get the actual key/value pair.
bool key_equals_case_insensitive(std::string_view o) const noexcept
Returns true if the key in this key/value pair is equal to the provided string_view in a case-insensi...
std::string_view key() const noexcept
Get the key of this key/value pair.
iterator & operator++() noexcept
Get the next key/value pair.
simdjson_result< element > at_key(std::string_view key) const noexcept
Get the value associated with the given key.
iterator end() const noexcept
One past the last key/value pair.
size_t size() const noexcept
Get the size of the object (number of keys).
simdjson_result< element > at_path(std::string_view json_path) const noexcept
Get the value associated with the given JSONPath expression.
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.
simdjson_result< element > operator[](std::string_view key) const noexcept
Get the value associated with the given key.
simdjson_result< element > at_pointer(std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
simdjson_inline object() noexcept
Create a new, invalid object.
iterator begin() const noexcept
Return the first key/value pair.
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ NO_SUCH_FIELD
JSON field not found in object.
@ 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.