1 #ifndef SIMDJSON_GENERIC_ONDEMAND_OBJECT_INL_H
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_OBJECT_INL_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/ondemand/field.h"
7 #include "simdjson/generic/ondemand/object.h"
8 #include "simdjson/generic/ondemand/object_iterator.h"
9 #include "simdjson/generic/ondemand/raw_json_string.h"
10 #include "simdjson/generic/ondemand/json_iterator.h"
11 #include "simdjson/generic/ondemand/value-inl.h"
15 namespace SIMDJSON_IMPLEMENTATION {
20 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(has_value) );
22 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
25 return value(iter.child());
29 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(has_value) );
31 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
34 return value(iter.child());
37 return find_field_unordered(key);
39 simdjson_inline simdjson_result<value> object::operator[](
const std::string_view key) && noexcept {
40 return std::forward<object>(*this).find_field_unordered(key);
44 SIMDJSON_TRY( iter.find_field_raw(key).get(has_value) );
46 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
49 return value(iter.child());
53 SIMDJSON_TRY( iter.find_field_raw(key).get(has_value) );
55 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
58 return value(iter.child());
62 SIMDJSON_TRY( iter.start_object().error() );
65 simdjson_inline simdjson_result<object> object::start_root(value_iterator &iter) noexcept {
66 SIMDJSON_TRY( iter.start_root_object().error() );
70 if(iter.is_at_key()) {
85 auto error = iter.field_key().get(actual_key);
86 if (error) { iter.abandon();
return error; };
88 if ((error = iter.field_value())) { iter.abandon();
return error; }
90 auto error_skip = iter.json_iter().skip_child(iter.depth()-1);
91 if(error_skip) { iter.abandon(); }
96 const uint8_t * starting_point{iter.peek_start()};
98 if(error) {
return error; }
99 const uint8_t * final_point{iter._json_iter->peek()};
100 return std::string_view(
reinterpret_cast<const char*
>(starting_point),
size_t(final_point - starting_point));
104 SIMDJSON_TRY( iter.started_object().error() );
108 simdjson_inline
object object::resume(
const value_iterator &iter) noexcept {
112 simdjson_inline
object::object(
const value_iterator &_iter) noexcept
117 simdjson_inline simdjson_result<object_iterator> object::begin() noexcept {
118 #if SIMDJSON_DEVELOPMENT_CHECKS
121 return object_iterator(iter);
123 simdjson_inline simdjson_result<object_iterator> object::end() noexcept {
124 return object_iterator(iter);
129 json_pointer = json_pointer.substr(1);
130 size_t slash = json_pointer.find(
'/');
131 std::string_view key = json_pointer.substr(0, slash);
136 size_t escape = key.find(
'~');
137 if (escape != std::string_view::npos) {
139 std::string unescaped(key);
141 switch (unescaped[escape+1]) {
143 unescaped.replace(escape, 2,
"~");
146 unescaped.replace(escape, 2,
"/");
151 escape = unescaped.find(
'~', escape+1);
152 }
while (escape != std::string::npos);
153 child = find_field(unescaped);
155 child = find_field(key);
161 if (slash != std::string_view::npos) {
162 child = child.at_pointer(json_pointer.substr(slash));
169 if (json_pointer ==
"-1") {
172 return at_pointer(json_pointer);
178 for(simdjson_unused
auto v : *
this) { count++; }
180 if(iter.error()) {
return iter.error(); }
189 auto error = iter.reset_object().get(is_not_empty);
190 if(error) {
return error; }
191 return !is_not_empty;
195 return iter.reset_object();
204 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept
205 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(value)) {}
206 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(
error_code error) noexcept
207 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(error) {}
209 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::begin() noexcept {
211 return first.begin();
213 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::end() noexcept {
217 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) & noexcept {
218 if (error()) {
return error(); }
219 return first.find_field_unordered(key);
221 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) && noexcept {
222 if (error()) {
return error(); }
223 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field_unordered(key);
225 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) & noexcept {
226 if (error()) {
return error(); }
229 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) && noexcept {
230 if (error()) {
return error(); }
231 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first)[key];
233 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) & noexcept {
234 if (error()) {
return error(); }
235 return first.find_field(key);
237 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) && noexcept {
238 if (error()) {
return error(); }
239 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field(key);
242 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_pointer(std::string_view json_pointer) noexcept {
243 if (error()) {
return error(); }
244 return first.at_pointer(json_pointer);
247 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_path(
248 std::string_view json_path) noexcept {
252 return first.at_path(json_path);
255 inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::reset() noexcept {
257 return first.reset();
260 inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::is_empty() noexcept {
262 return first.is_empty();
265 simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::count_fields() & noexcept {
266 if (error()) {
return error(); }
267 return first.count_fields();
270 simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::raw_json() noexcept {
272 return first.raw_json();
A forward-only JSON object field iterator.
simdjson_inline object() noexcept=default
Create a new invalid object.
simdjson_inline simdjson_result< value > find_field_unordered(std::string_view key) &noexcept
Look up a field by name on an object, without regard to key order.
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Consumes the object and returns a string_view instance corresponding to the object as represented in ...
simdjson_inline simdjson_result< value > find_field(std::string_view key) &noexcept
Look up a field by name on an object (order-sensitive).
simdjson_result< bool > is_empty() &noexcept
This method scans the beginning of the object and checks whether the object is empty.
simdjson_inline error_code consume() noexcept
Go to the end of the object, no matter where you are right now.
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the object.
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ OUT_OF_ORDER_ITERATION
tried to iterate an array or object out of order (checked when SIMDJSON_DEVELOPMENT_CHECKS=1)
@ 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.