simdjson 5.0.2
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
36 simdjson_inline simdjson_result<array_iterator> begin() & noexcept;
43 simdjson_inline simdjson_result<array_iterator> begin() && noexcept;
49 simdjson_inline simdjson_result<array_iterator> end() noexcept;
65 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
75 simdjson_inline simdjson_result<bool> is_empty() & noexcept;
86 inline simdjson_result<bool> reset() & noexcept;
115 inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
116
130 inline simdjson_result<value> at_path(std::string_view json_path) noexcept;
131
141#if SIMDJSON_SUPPORTS_CONCEPTS
142 template <typename Func>
143 requires std::invocable<Func, value>
144#else
145 template <typename Func>
146#endif
147 inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
148
153 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
154
162 simdjson_inline simdjson_result<value> at(size_t index) noexcept;
163
164#if SIMDJSON_SUPPORTS_CONCEPTS
172 template <typename T>
173 simdjson_warn_unused simdjson_inline error_code get(T &out)
174 noexcept(nothrow_gettable<T, array>) {
175 static_assert(custom_deserializable<T, array>);
176 return deserialize(*this, out);
177 }
184 template <typename T>
185 simdjson_inline simdjson_result<T> get()
186 noexcept(nothrow_gettable<T, array>)
187 {
188 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
189 T out{};
190 SIMDJSON_TRY(get<T>(out));
191 return out;
192 }
193#endif // SIMDJSON_SUPPORTS_CONCEPTS
194protected:
198 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
199
207 static simdjson_inline simdjson_result<array> start(value_iterator &iter) noexcept;
216 static simdjson_inline simdjson_result<array> start_root(value_iterator &iter) noexcept;
225 static simdjson_inline simdjson_result<array> started(value_iterator &iter) noexcept;
226
234 simdjson_inline array(const value_iterator &iter) noexcept;
235
241 value_iterator iter{};
242#if SIMDJSON_DEVELOPMENT_CHECKS
243 bool locked{false};
244 simdjson_inline void set_locked(bool _locked) noexcept;
245#endif
246
247 friend class value;
248 friend class document;
249 friend struct simdjson_result<value>;
250 friend struct simdjson_result<array>;
251 friend class array_iterator;
252};
253
254} // namespace ondemand
255} // namespace SIMDJSON_IMPLEMENTATION
256} // namespace simdjson
257
258namespace simdjson {
259
260template<>
261struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array> {
262public:
263 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::array &&value) noexcept;
264 simdjson_inline simdjson_result(error_code error) noexcept;
265 simdjson_inline simdjson_result() noexcept = default;
266
270 inline simdjson_result<size_t> count_elements() & noexcept;
271 inline simdjson_result<bool> is_empty() & noexcept;
272 inline simdjson_result<bool> reset() & noexcept;
273 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
274 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
275 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
276#if SIMDJSON_SUPPORTS_CONCEPTS
277 template <typename Func>
278 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
279#else
280 template <typename Func>
281#endif
282 simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
283 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
284#if SIMDJSON_SUPPORTS_CONCEPTS
285 // TODO: move this code into object-inl.h
286
287 template<typename T>
288 simdjson_inline simdjson_result<T> get() noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
289 if (error()) { return error(); }
290 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
291 return first;
292 }
293 return first.get<T>();
294 }
295 template<typename T>
296 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
297 if (error()) { return error(); }
298 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
299 out = first;
300 } else {
301 SIMDJSON_TRY( first.get<T>(out) );
302 }
303 return SUCCESS;
304 }
305#endif // SIMDJSON_SUPPORTS_CONCEPTS
306};
307
308} // namespace simdjson
309
310#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:190
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index.
Definition array-inl.h:236
value_iterator iter
Iterator marking current position.
Definition array.h:241
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:131
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
Definition array-inl.h:79
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition array-inl.h:154
static simdjson_inline simdjson_result< array > start(value_iterator &iter) noexcept
Begin array iteration.
Definition array-inl.h:61
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:115
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
Definition array-inl.h:178
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:96
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:102
simdjson_inline simdjson_result< array_iterator > end() noexcept
Sentinel representing the end of the array.
Definition array-inl.h:93
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the array.
Definition array-inl.h:141
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:281
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