1#ifndef SIMDJSON_ARRAY_INL_H
2#define SIMDJSON_ARRAY_INL_H
6#include "simdjson/dom/base.h"
7#include "simdjson/dom/array.h"
8#include "simdjson/dom/element.h"
9#include "simdjson/error-inl.h"
10#include "simdjson/jsonpathutil.h"
11#include "simdjson/internal/tape_ref-inl.h"
20simdjson_inline simdjson_result<dom::array>::simdjson_result() noexcept
21 : internal::simdjson_result_base<dom::array>() {}
22simdjson_inline simdjson_result<dom::array>::simdjson_result(dom::array value) noexcept
23 : internal::simdjson_result_base<dom::array>(std::forward<dom::array>(value)) {}
24simdjson_inline simdjson_result<dom::array>::simdjson_result(
error_code error) noexcept
25 : internal::simdjson_result_base<dom::array>(error) {}
27#if SIMDJSON_EXCEPTIONS
29inline dom::array::iterator simdjson_result<dom::array>::begin() const noexcept(false) {
30 if (error()) {
throw simdjson_error(error()); }
33inline dom::array::iterator simdjson_result<dom::array>::end() const noexcept(false) {
34 if (error()) {
throw simdjson_error(error()); }
37inline size_t simdjson_result<dom::array>::size() const noexcept(false) {
38 if (error()) {
throw simdjson_error(error()); }
44inline simdjson_result<dom::element> simdjson_result<dom::array>::at_pointer(std::string_view json_pointer)
const noexcept {
45 if (error()) {
return error(); }
46 return first.at_pointer(json_pointer);
49 inline simdjson_result<dom::element> simdjson_result<dom::array>::at_path(std::string_view json_path)
const noexcept {
52 return at_pointer(json_pointer);
55inline simdjson_result<std::vector<dom::element>> simdjson_result<dom::array>::at_path_with_wildcard(std::string_view json_path)
const noexcept {
59 return first.at_path_with_wildcard(json_path);
62inline simdjson_result<dom::element> simdjson_result<dom::array>::at(
size_t index)
const noexcept {
63 if (error()) {
return error(); }
64 return first.at(index);
67inline std::vector<dom::element>& simdjson_result<dom::array>::get_values(std::vector<dom::element>& out)
const noexcept {
68 return first.get_values(out);
77simdjson_inline
array::array(
const internal::tape_ref &_tape) noexcept : tape{_tape} {}
79 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
80 return internal::tape_ref(tape.doc, tape.json_index + 1);
83 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
84 return internal::tape_ref(tape.doc, tape.after_element() - 1);
87 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
88 return tape.scope_count();
91 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
92 return tape.matching_brace_index() - tape.json_index;
95 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
96 if(json_pointer.empty()) {
98 }
else if(json_pointer[0] !=
'/') {
101 json_pointer = json_pointer.substr(1);
107 size_t array_index = 0;
109 for (i = 0; i < json_pointer.length() && json_pointer[i] !=
'/'; i++) {
110 uint8_t digit = uint8_t(json_pointer[i] -
'0');
113 array_index = array_index*10 + digit;
123 auto child =
array(tape).
at(array_index);
129 if (i < json_pointer.length()) {
130 child = child.at_pointer(json_pointer.substr(i));
138 return at_pointer(json_pointer);
142 if (current == end) {
149 for (
auto it = current; it != end; ++it) {
150 std::vector<element> child_result;
151 auto error = it->at_path_with_wildcard(path_suffix).get(child_result);
155 accumulator.reserve(accumulator.size() + child_result.size());
156 accumulator.insert(accumulator.end(),
157 std::make_move_iterator(child_result.begin()),
158 std::make_move_iterator(child_result.end()));
163 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
167 if (!json_path.empty() && json_path.front() ==
'$') {
171 if (i >= json_path.size() || (json_path[i] !=
'.' && json_path[i] !=
'[')) {
175 if (json_path.find(
"*") != std::string::npos) {
176 std::vector<element> child_values;
179 (json_path.compare(i, 3,
"[*]") == 0 && json_path.size() == i + 3) ||
180 (json_path.compare(i, 2,
".*") == 0 && json_path.size() == i + 2)
182 get_values(child_values);
186 std::pair<std::string_view, std::string_view> key_and_json_path = get_next_key_and_json_path(json_path);
188 std::string_view key = key_and_json_path.first;
189 json_path = key_and_json_path.second;
191 if (key.size() > 0) {
193 get_values(child_values);
196 std::string json_pointer = std::string(
"/") + std::string(key);
197 auto error = at_pointer(json_pointer).get(pointer_result);
200 child_values.emplace_back(pointer_result);
204 std::vector<element> result = {};
206 if (child_values.size() > 0) {
207 std::vector<element>::iterator child_values_begin = child_values.begin();
208 std::vector<element>::iterator child_values_end = child_values.end();
210 process_json_path_of_child_elements(child_values_begin, child_values_end, json_path, result);
219 auto error = at_path(json_path).get(result);
224 return std::vector<element>{std::move(result)};
229 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
232 if (i == index) {
return element; }
239 out.reserve(this->size());
247inline array::operator
element() const noexcept {
254simdjson_inline array::iterator::iterator(
const internal::tape_ref &_tape) noexcept : tape{_tape} { }
259 tape.json_index = tape.after_element();
268 return tape.json_index != other.tape.json_index;
270inline bool array::iterator::operator==(
const array::iterator& other)
const noexcept {
271 return tape.json_index == other.tape.json_index;
273inline bool array::iterator::operator<(
const array::iterator& other)
const noexcept {
274 return tape.json_index < other.tape.json_index;
276inline bool array::iterator::operator<=(
const array::iterator& other)
const noexcept {
277 return tape.json_index <= other.tape.json_index;
279inline bool array::iterator::operator>=(
const array::iterator& other)
const noexcept {
280 return tape.json_index >= other.tape.json_index;
282inline bool array::iterator::operator>(
const array::iterator& other)
const noexcept {
283 return tape.json_index > other.tape.json_index;
291#include "simdjson/dom/element-inl.h"
293#if SIMDJSON_SUPPORTS_RANGES
294static_assert(std::ranges::view<simdjson::dom::array>);
295static_assert(std::ranges::sized_range<simdjson::dom::array>);
296#if SIMDJSON_EXCEPTIONS
297static_assert(std::ranges::view<simdjson::simdjson_result<simdjson::dom::array>>);
298static_assert(std::ranges::sized_range<simdjson::simdjson_result<simdjson::dom::array>>);
bool operator!=(const iterator &other) const noexcept
Check if these values come from the same place in the JSON.
iterator & operator++() noexcept
Get the next value.
reference operator*() const noexcept
Get the actual value.
std::vector< element > & get_values(std::vector< element > &out) const noexcept
Gets the values of items in an array element This function has linear-time complexity: the values are...
simdjson_result< std::vector< element > > at_path_with_wildcard(std::string_view json_path) const noexcept
Adds support for JSONPath expression with wildcards '*'.
size_t number_of_slots() const noexcept
Get the total number of slots used by this array on the tape.
iterator end() const noexcept
One past the last array element.
size_t size() const noexcept
Get the size of the array (number of immediate children).
simdjson_result< element > at(size_t index) const noexcept
Get the value at the given index.
simdjson_result< element > at_pointer(std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
simdjson_inline array() noexcept
Create a new, invalid array.
simdjson_result< element > at_path(std::string_view json_path) const noexcept
Get the value associated with the given JSONPath expression.
iterator begin() const noexcept
Return the first array element.
void process_json_path_of_child_elements(std::vector< element >::iterator ¤t, std::vector< element >::iterator &end, const std::string_view &path_suffix, std::vector< element > &accumulator) const noexcept
Recursive function which processes the JSON path of each child element.
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.
@ INDEX_OUT_OF_BOUNDS
JSON array index too large.
@ 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.