simdjson 4.4.0
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
181 inline simdjson_result<std::vector<value>> at_path_with_wildcard(std::string_view json_path) noexcept;
182
192 inline simdjson_result<bool> reset() & noexcept;
202 inline simdjson_result<bool> is_empty() & noexcept;
220 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
226 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
227
228#if SIMDJSON_SUPPORTS_CONCEPTS
236 template <typename T>
237 simdjson_warn_unused simdjson_inline error_code get(T &out)
238 noexcept(custom_deserializable<T, object> ? nothrow_custom_deserializable<T, object> : true) {
239 static_assert(custom_deserializable<T, object>);
240 return deserialize(*this, out);
241 }
248 template <typename T>
249 simdjson_inline simdjson_result<T> get()
250 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
251 {
252 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
253 T out{};
254 SIMDJSON_TRY(get<T>(out));
255 return out;
256 }
257
258#if SIMDJSON_STATIC_REFLECTION
283 template<constevalutil::fixed_string... FieldNames, typename T>
284 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
285 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) & noexcept;
286#endif // SIMDJSON_STATIC_REFLECTION
287#endif // SIMDJSON_SUPPORTS_CONCEPTS
288protected:
292 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
293 static simdjson_inline simdjson_result<object> start(value_iterator &iter) noexcept;
294 static simdjson_inline simdjson_result<object> start_root(value_iterator &iter) noexcept;
295 static simdjson_inline simdjson_result<object> started(value_iterator &iter) noexcept;
296 static simdjson_inline object resume(const value_iterator &iter) noexcept;
297 simdjson_inline object(const value_iterator &iter) noexcept;
298
299 simdjson_warn_unused simdjson_inline error_code find_field_raw(const std::string_view key) noexcept;
300
301 value_iterator iter{};
302
303 friend class value;
304 friend class document;
305 friend struct simdjson_result<object>;
306};
307
308} // namespace ondemand
309} // namespace SIMDJSON_IMPLEMENTATION
310} // namespace simdjson
311
312namespace simdjson {
313
314template<>
315struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object> {
316public:
317 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept;
318 simdjson_inline simdjson_result(error_code error) noexcept;
319 simdjson_inline simdjson_result() noexcept = default;
320
323 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) & noexcept;
324 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) && noexcept;
325 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) & noexcept;
326 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) && noexcept;
327 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) & noexcept;
328 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) && noexcept;
329 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
330 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
331 simdjson_inline simdjson_result<std::vector<SIMDJSON_IMPLEMENTATION::ondemand::value>> at_path_with_wildcard(std::string_view json_path) noexcept;
332 inline simdjson_result<bool> reset() noexcept;
333 inline simdjson_result<bool> is_empty() noexcept;
334 inline simdjson_result<size_t> count_fields() & noexcept;
335 inline simdjson_result<std::string_view> raw_json() noexcept;
336 #if SIMDJSON_SUPPORTS_CONCEPTS
337 // TODO: move this code into object-inl.h
338
339 template<typename T>
340 simdjson_inline simdjson_result<T> get() noexcept {
341 if (error()) { return error(); }
342 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
343 return first;
344 }
345 return first.get<T>();
346 }
347 template<typename T>
348 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
349 if (error()) { return error(); }
350 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
351 out = first;
352 } else {
353 SIMDJSON_TRY( first.get<T>(out) );
354 }
355 return SUCCESS;
356 }
357
358#if SIMDJSON_STATIC_REFLECTION
359 // TODO: move this code into object-inl.h
360 template<constevalutil::fixed_string... FieldNames, typename T>
361 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
362 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) noexcept {
363 if (error()) { return error(); }
364 return first.extract_into<FieldNames...>(out);
365 }
366#endif // SIMDJSON_STATIC_REFLECTION
367#endif // SIMDJSON_SUPPORTS_CONCEPTS
368};
369
370} // namespace simdjson
371
372#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< 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: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