simdjson 4.2.4
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;
70 simdjson_inline simdjson_result<value> find_field(std::string_view key) & noexcept;
72 simdjson_inline simdjson_result<value> find_field(std::string_view key) && noexcept;
73
110 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) & noexcept;
112 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) && noexcept;
114 simdjson_inline simdjson_result<value> operator[](std::string_view key) & noexcept;
116 simdjson_inline simdjson_result<value> operator[](std::string_view key) && noexcept;
117
152 inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
153
165 inline simdjson_result<value> at_path(std::string_view json_path) noexcept;
166
174 inline simdjson_result<std::vector<value>> at_path_with_wildcard(std::string_view json_path) noexcept;
175
185 inline simdjson_result<bool> reset() & noexcept;
195 inline simdjson_result<bool> is_empty() & noexcept;
213 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
219 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
220
221#if SIMDJSON_SUPPORTS_CONCEPTS
229 template <typename T>
230 simdjson_warn_unused simdjson_inline error_code get(T &out)
231 noexcept(custom_deserializable<T, object> ? nothrow_custom_deserializable<T, object> : true) {
232 static_assert(custom_deserializable<T, object>);
233 return deserialize(*this, out);
234 }
241 template <typename T>
242 simdjson_inline simdjson_result<T> get()
243 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
244 {
245 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
246 T out{};
247 SIMDJSON_TRY(get<T>(out));
248 return out;
249 }
250
251#if SIMDJSON_STATIC_REFLECTION
276 template<constevalutil::fixed_string... FieldNames, typename T>
277 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
278 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) & noexcept;
279#endif // SIMDJSON_STATIC_REFLECTION
280#endif // SIMDJSON_SUPPORTS_CONCEPTS
281protected:
285 simdjson_warn_unused simdjson_inline error_code consume() noexcept;
286 static simdjson_inline simdjson_result<object> start(value_iterator &iter) noexcept;
287 static simdjson_inline simdjson_result<object> start_root(value_iterator &iter) noexcept;
288 static simdjson_inline simdjson_result<object> started(value_iterator &iter) noexcept;
289 static simdjson_inline object resume(const value_iterator &iter) noexcept;
290 simdjson_inline object(const value_iterator &iter) noexcept;
291
292 simdjson_warn_unused simdjson_inline error_code find_field_raw(const std::string_view key) noexcept;
293
294 value_iterator iter{};
295
296 friend class value;
297 friend class document;
298 friend struct simdjson_result<object>;
299};
300
301} // namespace ondemand
302} // namespace SIMDJSON_IMPLEMENTATION
303} // namespace simdjson
304
305namespace simdjson {
306
307template<>
308struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object> {
309public:
310 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept;
311 simdjson_inline simdjson_result(error_code error) noexcept;
312 simdjson_inline simdjson_result() noexcept = default;
313
316 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) & noexcept;
317 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) && noexcept;
318 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) & noexcept;
319 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) && noexcept;
320 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) & noexcept;
321 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) && noexcept;
322 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
323 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
324 simdjson_inline simdjson_result<std::vector<SIMDJSON_IMPLEMENTATION::ondemand::value>> at_path_with_wildcard(std::string_view json_path) noexcept;
325 inline simdjson_result<bool> reset() noexcept;
326 inline simdjson_result<bool> is_empty() noexcept;
327 inline simdjson_result<size_t> count_fields() & noexcept;
328 inline simdjson_result<std::string_view> raw_json() noexcept;
329 #if SIMDJSON_SUPPORTS_CONCEPTS
330 // TODO: move this code into object-inl.h
331
332 template<typename T>
333 simdjson_inline simdjson_result<T> get() noexcept {
334 if (error()) { return error(); }
335 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
336 return first;
337 }
338 return first.get<T>();
339 }
340 template<typename T>
341 simdjson_warn_unused simdjson_inline error_code get(T& out) noexcept {
342 if (error()) { return error(); }
343 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
344 out = first;
345 } else {
346 SIMDJSON_TRY( first.get<T>(out) );
347 }
348 return SUCCESS;
349 }
350
351#if SIMDJSON_STATIC_REFLECTION
352 // TODO: move this code into object-inl.h
353 template<constevalutil::fixed_string... FieldNames, typename T>
354 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
355 simdjson_warn_unused simdjson_inline error_code extract_into(T& out) noexcept {
356 if (error()) { return error(); }
357 return first.extract_into<FieldNames...>(out);
358 }
359#endif // SIMDJSON_STATIC_REFLECTION
360#endif // SIMDJSON_SUPPORTS_CONCEPTS
361};
362
363} // namespace simdjson
364
365#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