simdjson 4.6.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
array.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_ARRAY_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_ARRAY_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/implementation_simdjson_result_base.h"
7#include "simdjson/generic/ondemand/value_iterator.h"
8#include <vector>
9#endif // SIMDJSON_CONDITIONAL_INCLUDE
10
11namespace simdjson {
12namespace SIMDJSON_IMPLEMENTATION {
13namespace ondemand {
14
18class array {
19public:
25 simdjson_inline array() noexcept = default;
26
32 simdjson_inline simdjson_result<array_iterator> begin() noexcept;
38 simdjson_inline simdjson_result<array_iterator> end() noexcept;
54 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
64 simdjson_inline simdjson_result<bool> is_empty() & noexcept;
75 inline simdjson_result<bool> reset() & noexcept;
104 inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
105
119 inline simdjson_result<value> at_path(std::string_view json_path) noexcept;
120
130#if SIMDJSON_SUPPORTS_CONCEPTS
131 template <typename Func>
132 requires std::invocable<Func, value>
133#else
134 template <typename Func>
135#endif
136 inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
137
142 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
143
151 simdjson_inline simdjson_result<value> at(size_t index) noexcept;
152
153#if SIMDJSON_SUPPORTS_CONCEPTS
161 template <typename T>
162 simdjson_warn_unused simdjson_inline error_code get(T &out)
163 noexcept(custom_deserializable<T, array> ? nothrow_custom_deserializable<T, array> : true) {
164 static_assert(custom_deserializable<T, array>);
165 return deserialize(*this, out);
166 }
173 template <typename T>
174 simdjson_inline simdjson_result<T> get()
175 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
176 {
177 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
178 T out{};
179 SIMDJSON_TRY(get<T>(out));
180 return out;
181 }
182#endif // SIMDJSON_SUPPORTS_CONCEPTS
183protected:
187 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
188
196 static simdjson_inline simdjson_result<array> start(value_iterator &iter) noexcept;
205 static simdjson_inline simdjson_result<array> start_root(value_iterator &iter) noexcept;
214 static simdjson_inline simdjson_result<array> started(value_iterator &iter) noexcept;
215
223 simdjson_inline array(const value_iterator &iter) noexcept;
224
230 value_iterator iter{};
231
232 friend class value;
233 friend class document;
234 friend struct simdjson_result<value>;
235 friend struct simdjson_result<array>;
236 friend class array_iterator;
237};
238
239} // namespace ondemand
240} // namespace SIMDJSON_IMPLEMENTATION
241} // namespace simdjson
242
243namespace simdjson {
244
245template<>
246struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array> {
247public:
248 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::array &&value) noexcept;
249 simdjson_inline simdjson_result(error_code error) noexcept;
250 simdjson_inline simdjson_result() noexcept = default;
251
254 inline simdjson_result<size_t> count_elements() & noexcept;
255 inline simdjson_result<bool> is_empty() & noexcept;
256 inline simdjson_result<bool> reset() & noexcept;
257 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
258 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
259 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
260#if SIMDJSON_SUPPORTS_CONCEPTS
261 template <typename Func>
262 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
263#else
264 template <typename Func>
265#endif
266 simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
267 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
268#if SIMDJSON_SUPPORTS_CONCEPTS
269 // TODO: move this code into object-inl.h
270
271 template<typename T>
272 simdjson_inline simdjson_result<T> get() noexcept {
273 if (error()) { return error(); }
274 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
275 return first;
276 }
277 return first.get<T>();
278 }
279 template<typename T>
280 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
281 if (error()) { return error(); }
282 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
283 out = first;
284 } else {
285 SIMDJSON_TRY( first.get<T>(out) );
286 }
287 return SUCCESS;
288 }
289#endif // SIMDJSON_SUPPORTS_CONCEPTS
290};
291
292} // namespace simdjson
293
294#endif // SIMDJSON_GENERIC_ONDEMAND_ARRAY_H
error_code for_each_at_path_with_wildcard(std::string_view json_path, Func &&callback) noexcept
Call the provided callback for each value matching the given JSONPath expression with wildcard suppor...
Definition array-inl.h:179
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index.
Definition array-inl.h:219
value_iterator iter
Iterator marking current position.
Definition array.h:230
simdjson_inline simdjson_result< bool > is_empty() &noexcept
This method scans the beginning of the array and checks whether the array is empty.
Definition array-inl.h:120
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition array-inl.h:131
static simdjson_inline simdjson_result< array > start(value_iterator &iter) noexcept
Begin array iteration.
Definition array-inl.h:61
simdjson_inline simdjson_result< array_iterator > begin() noexcept
Begin array iteration.
Definition array-inl.h:79
simdjson_inline array() noexcept=default
Create a new invalid array.
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition array-inl.h:107
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
Definition array-inl.h:167
static simdjson_inline simdjson_result< array > start_root(value_iterator &iter) noexcept
Begin array iteration from the root.
Definition array-inl.h:68
static simdjson_inline simdjson_result< array > started(value_iterator &iter) noexcept
Begin array iteration.
Definition array-inl.h:73
simdjson_warn_unused simdjson_inline error_code consume() noexcept
Go to the end of the array, no matter where you are right now.
Definition array-inl.h:88
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Consumes the array and returns a string_view instance corresponding to the array as represented in JS...
Definition array-inl.h:94
simdjson_inline simdjson_result< array_iterator > end() noexcept
Sentinel representing the end of the array.
Definition array-inl.h:85
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the array.
Definition array-inl.h:127
An ephemeral JSON value returned during iteration.
Definition value.h:22
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
error_code
All possible errors returned by simdjson.
Definition error.h:19
@ SUCCESS
No error.
Definition error.h:20
The result of a simdjson operation that could fail.
Definition error.h:280
simdjson_inline error_code error() const noexcept
The error.
Definition error-inl.h:168
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition error-inl.h:163