simdjson 4.1.0
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#endif // SIMDJSON_CONDITIONAL_INCLUDE
9
10namespace simdjson {
11namespace SIMDJSON_IMPLEMENTATION {
12namespace ondemand {
13
17class array {
18public:
24 simdjson_inline array() noexcept = default;
25
31 simdjson_inline simdjson_result<array_iterator> begin() noexcept;
37 simdjson_inline simdjson_result<array_iterator> end() noexcept;
53 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
63 simdjson_inline simdjson_result<bool> is_empty() & noexcept;
74 inline simdjson_result<bool> reset() & noexcept;
103 inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
104
118 inline simdjson_result<value> at_path(std::string_view json_path) noexcept;
119
124 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
125
133 simdjson_inline simdjson_result<value> at(size_t index) noexcept;
134
135#if SIMDJSON_SUPPORTS_CONCEPTS
143 template <typename T>
144 simdjson_warn_unused simdjson_inline error_code get(T &out)
145 noexcept(custom_deserializable<T, array> ? nothrow_custom_deserializable<T, array> : true) {
146 static_assert(custom_deserializable<T, array>);
147 return deserialize(*this, out);
148 }
155 template <typename T>
156 simdjson_inline simdjson_result<T> get()
157 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
158 {
159 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
160 T out{};
161 SIMDJSON_TRY(get<T>(out));
162 return out;
163 }
164#endif // SIMDJSON_SUPPORTS_CONCEPTS
165protected:
169 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
170
178 static simdjson_inline simdjson_result<array> start(value_iterator &iter) noexcept;
187 static simdjson_inline simdjson_result<array> start_root(value_iterator &iter) noexcept;
196 static simdjson_inline simdjson_result<array> started(value_iterator &iter) noexcept;
197
205 simdjson_inline array(const value_iterator &iter) noexcept;
206
212 value_iterator iter{};
213
214 friend class value;
215 friend class document;
216 friend struct simdjson_result<value>;
217 friend struct simdjson_result<array>;
218 friend class array_iterator;
219};
220
221} // namespace ondemand
222} // namespace SIMDJSON_IMPLEMENTATION
223} // namespace simdjson
224
225namespace simdjson {
226
227template<>
228struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array> {
229public:
230 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::array &&value) noexcept;
231 simdjson_inline simdjson_result(error_code error) noexcept;
232 simdjson_inline simdjson_result() noexcept = default;
233
236 inline simdjson_result<size_t> count_elements() & noexcept;
237 inline simdjson_result<bool> is_empty() & noexcept;
238 inline simdjson_result<bool> reset() & noexcept;
239 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
240 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
241 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
242 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
243#if SIMDJSON_SUPPORTS_CONCEPTS
244 // TODO: move this code into object-inl.h
245
246 template<typename T>
247 simdjson_inline simdjson_result<T> get() noexcept {
248 if (error()) { return error(); }
249 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
250 return first;
251 }
252 return first.get<T>();
253 }
254 template<typename T>
255 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
256 if (error()) { return error(); }
257 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::array>) {
258 out = first;
259 } else {
260 SIMDJSON_TRY( first.get<T>(out) );
261 }
262 return SUCCESS;
263 }
264#endif // SIMDJSON_SUPPORTS_CONCEPTS
265};
266
267} // namespace simdjson
268
269#endif // SIMDJSON_GENERIC_ONDEMAND_ARRAY_H
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index.
Definition array-inl.h:173
value_iterator iter
Iterator marking current position.
Definition array.h:212
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:21
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:278
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