simdjson 4.6.3
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
37 simdjson_inline simdjson_result<object_iterator> begin() noexcept;
38 simdjson_inline simdjson_result<object_iterator> end() noexcept;
77 simdjson_inline simdjson_result<value> find_field(std::string_view key) & noexcept;
79 simdjson_inline simdjson_result<value> find_field(std::string_view key) && noexcept;
80
117 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) & noexcept;
119 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) && noexcept;
121 simdjson_inline simdjson_result<value> operator[](std::string_view key) & noexcept;
123 simdjson_inline simdjson_result<value> operator[](std::string_view key) && noexcept;
124
159 inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
160
172 inline simdjson_result<value> at_path(std::string_view json_path) noexcept;
173
183#if SIMDJSON_SUPPORTS_CONCEPTS
184 template <typename Func>
185 requires std::invocable<Func, value>
186#else
187 template <typename Func>
188#endif
189 inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
190
200 inline simdjson_result<bool> reset() & noexcept;
210 inline simdjson_result<bool> is_empty() & noexcept;
228 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
234 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
235
236#if SIMDJSON_SUPPORTS_CONCEPTS
244 template <typename T>
245 simdjson_warn_unused simdjson_inline error_code get(T &out)
246 noexcept(custom_deserializable<T, object> ? nothrow_custom_deserializable<T, object> : true) {
247 static_assert(custom_deserializable<T, object>);
248 return deserialize(*this, out);
249 }
256 template <typename T>
257 simdjson_inline simdjson_result<T> get()
258 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
259 {
260 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
261 T out{};
262 SIMDJSON_TRY(get<T>(out));
263 return out;
264 }
265
266#if SIMDJSON_STATIC_REFLECTION
291 template<constevalutil::fixed_string... FieldNames, typename T>
292 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
293 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) & noexcept;
294#endif // SIMDJSON_STATIC_REFLECTION
295#endif // SIMDJSON_SUPPORTS_CONCEPTS
296protected:
300 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
301 static simdjson_inline simdjson_result<object> start(value_iterator &iter) noexcept;
302 static simdjson_inline simdjson_result<object> start_root(value_iterator &iter) noexcept;
303 static simdjson_inline simdjson_result<object> started(value_iterator &iter) noexcept;
304 static simdjson_inline object resume(const value_iterator &iter) noexcept;
305 simdjson_inline object(const value_iterator &iter) noexcept;
306
307 simdjson_warn_unused simdjson_inline error_code find_field_raw(const std::string_view key) noexcept;
308
309 value_iterator iter{};
310
311 friend class value;
312 friend class document;
313 friend struct simdjson_result<object>;
314};
315
316} // namespace ondemand
317} // namespace SIMDJSON_IMPLEMENTATION
318} // namespace simdjson
319
320namespace simdjson {
321
322template<>
323struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object> {
324public:
325 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept;
326 simdjson_inline simdjson_result(error_code error) noexcept;
327 simdjson_inline simdjson_result() noexcept = default;
328
331 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) & noexcept;
332 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) && noexcept;
333 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) & noexcept;
334 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) && noexcept;
335 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) & noexcept;
336 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) && noexcept;
337 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
338 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
339#if SIMDJSON_SUPPORTS_CONCEPTS
340 template <typename Func>
341 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
342#else
343 template <typename Func>
344#endif
345 simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
346 inline simdjson_result<bool> reset() noexcept;
347 inline simdjson_result<bool> is_empty() noexcept;
348 inline simdjson_result<size_t> count_fields() & noexcept;
349 inline simdjson_result<std::string_view> raw_json() noexcept;
350 #if SIMDJSON_SUPPORTS_CONCEPTS
351 // TODO: move this code into object-inl.h
352
353 template<typename T>
354 simdjson_inline simdjson_result<T> get() noexcept {
355 if (error()) { return error(); }
356 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
357 return first;
358 }
359 return first.get<T>();
360 }
361 template<typename T>
362 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
363 if (error()) { return error(); }
364 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
365 out = first;
366 } else {
367 SIMDJSON_TRY( first.get<T>(out) );
368 }
369 return SUCCESS;
370 }
371
372#if SIMDJSON_STATIC_REFLECTION
373 // TODO: move this code into object-inl.h
374 template<constevalutil::fixed_string... FieldNames, typename T>
375 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
376 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) noexcept {
377 if (error()) { return error(); }
378 return first.extract_into<FieldNames...>(out);
379 }
380#endif // SIMDJSON_STATIC_REFLECTION
381#endif // SIMDJSON_SUPPORTS_CONCEPTS
382};
383
384} // namespace simdjson
385
386#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.
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 object-inl.h:186
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< bool > is_empty() &noexcept
This method scans the beginning of the object and checks whether the object is empty.
Definition object-inl.h:228
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the object.
Definition object-inl.h:235
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< object_iterator > begin() noexcept
Get an iterator to the start of the object.
Definition object-inl.h:122
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:216
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