simdjson 4.2.2
Ridiculously Fast JSON
Loading...
Searching...
No Matches
object.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_OBJECT_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_OBJECT_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#if SIMDJSON_STATIC_REFLECTION && SIMDJSON_SUPPORTS_CONCEPTS
10#include "simdjson/generic/ondemand/json_string_builder.h" // for constevalutil::fixed_string
11#endif
12#endif // SIMDJSON_CONDITIONAL_INCLUDE
13
14namespace simdjson {
15namespace SIMDJSON_IMPLEMENTATION {
16namespace ondemand {
17
21class object {
22public:
28 simdjson_inline object() noexcept = default;
29
30 simdjson_inline simdjson_result<object_iterator> begin() noexcept;
31 simdjson_inline simdjson_result<object_iterator> end() noexcept;
68 simdjson_inline simdjson_result<value> find_field(std::string_view key) & noexcept;
70 simdjson_inline simdjson_result<value> find_field(std::string_view key) && noexcept;
71
107 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) & noexcept;
109 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) && noexcept;
111 simdjson_inline simdjson_result<value> operator[](std::string_view key) & noexcept;
113 simdjson_inline simdjson_result<value> operator[](std::string_view key) && noexcept;
114
149 inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
150
162 inline simdjson_result<value> at_path(std::string_view json_path) noexcept;
163
171 inline simdjson_result<std::vector<value>> at_path_with_wildcard(std::string_view json_path) noexcept;
172
182 inline simdjson_result<bool> reset() & noexcept;
192 inline simdjson_result<bool> is_empty() & noexcept;
210 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
216 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
217
218#if SIMDJSON_SUPPORTS_CONCEPTS
226 template <typename T>
227 simdjson_warn_unused simdjson_inline error_code get(T &out)
228 noexcept(custom_deserializable<T, object> ? nothrow_custom_deserializable<T, object> : true) {
229 static_assert(custom_deserializable<T, object>);
230 return deserialize(*this, out);
231 }
238 template <typename T>
239 simdjson_inline simdjson_result<T> get()
240 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
241 {
242 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
243 T out{};
244 SIMDJSON_TRY(get<T>(out));
245 return out;
246 }
247
248#if SIMDJSON_STATIC_REFLECTION
273 template<constevalutil::fixed_string... FieldNames, typename T>
274 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
275 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) & noexcept;
276#endif // SIMDJSON_STATIC_REFLECTION
277#endif // SIMDJSON_SUPPORTS_CONCEPTS
278protected:
282 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
283 static simdjson_inline simdjson_result<object> start(value_iterator &iter) noexcept;
284 static simdjson_inline simdjson_result<object> start_root(value_iterator &iter) noexcept;
285 static simdjson_inline simdjson_result<object> started(value_iterator &iter) noexcept;
286 static simdjson_inline object resume(const value_iterator &iter) noexcept;
287 simdjson_inline object(const value_iterator &iter) noexcept;
288
289 simdjson_warn_unused simdjson_inline error_code find_field_raw(const std::string_view key) noexcept;
290
291 value_iterator iter{};
292
293 friend class value;
294 friend class document;
295 friend struct simdjson_result<object>;
296};
297
298} // namespace ondemand
299} // namespace SIMDJSON_IMPLEMENTATION
300} // namespace simdjson
301
302namespace simdjson {
303
304template<>
305struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object> {
306public:
307 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept;
308 simdjson_inline simdjson_result(error_code error) noexcept;
309 simdjson_inline simdjson_result() noexcept = default;
310
313 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) & noexcept;
314 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) && noexcept;
315 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) & noexcept;
316 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) && noexcept;
317 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) & noexcept;
318 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) && noexcept;
319 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
320 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
321 simdjson_inline simdjson_result<std::vector<SIMDJSON_IMPLEMENTATION::ondemand::value>> at_path_with_wildcard(std::string_view json_path) noexcept;
322 inline simdjson_result<bool> reset() noexcept;
323 inline simdjson_result<bool> is_empty() noexcept;
324 inline simdjson_result<size_t> count_fields() & noexcept;
325 inline simdjson_result<std::string_view> raw_json() noexcept;
326 #if SIMDJSON_SUPPORTS_CONCEPTS
327 // TODO: move this code into object-inl.h
328
329 template<typename T>
330 simdjson_inline simdjson_result<T> get() noexcept {
331 if (error()) { return error(); }
332 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
333 return first;
334 }
335 return first.get<T>();
336 }
337 template<typename T>
338 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
339 if (error()) { return error(); }
340 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
341 out = first;
342 } else {
343 SIMDJSON_TRY( first.get<T>(out) );
344 }
345 return SUCCESS;
346 }
347
348#if SIMDJSON_STATIC_REFLECTION
349 // TODO: move this code into object-inl.h
350 template<constevalutil::fixed_string... FieldNames, typename T>
351 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
352 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) noexcept {
353 if (error()) { return error(); }
354 return first.extract_into<FieldNames...>(out);
355 }
356#endif // SIMDJSON_STATIC_REFLECTION
357#endif // SIMDJSON_SUPPORTS_CONCEPTS
358};
359
360} // namespace simdjson
361
362#endif // SIMDJSON_GENERIC_ONDEMAND_OBJECT_H
A forward-only JSON object field iterator.
Definition object.h:21
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.
Definition object-inl.h:23
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 ...
Definition object-inl.h:100
simdjson_warn_unused simdjson_inline error_code consume() noexcept
Go to the end of the object, no matter where you are right now.
Definition object-inl.h:74
simdjson_inline simdjson_result< value > find_field(std::string_view key) &noexcept
Look up a field by name on an object (order-sensitive).
Definition object-inl.h:47
simdjson_result< std::vector< value > > at_path_with_wildcard(std::string_view json_path) noexcept
Get all values matching the given JSONPath expression with wildcard support.
Definition object-inl.h:180
simdjson_result< bool > is_empty() &noexcept
This method scans the beginning of the object and checks whether the object is empty.
Definition object-inl.h:236
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the object.
Definition object-inl.h:243
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
Definition object-inl.h:172
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition object-inl.h:132
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
Definition object-inl.h:224
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