simdjson  3.1.6
Ridiculously Fast JSON
object.h
1 #ifndef SIMDJSON_DOM_OBJECT_H
2 #define SIMDJSON_DOM_OBJECT_H
3 
4 #include "simdjson/common_defs.h"
5 #include "simdjson/error.h"
6 #include "simdjson/internal/tape_ref.h"
7 
8 namespace simdjson {
9 namespace internal {
10 template<typename T>
11 class string_builder;
12 }
13 namespace dom {
14 
15 class document;
16 class element;
17 class key_value_pair;
18 
22 class object {
23 public:
25  simdjson_inline object() noexcept;
26 
27  class iterator {
28  public:
29  using value_type = key_value_pair;
30  using difference_type = std::ptrdiff_t;
31 
35  inline const value_type operator*() const noexcept;
42  inline iterator& operator++() noexcept;
49  inline iterator operator++(int) noexcept;
55  inline bool operator!=(const iterator& other) const noexcept;
56  inline bool operator==(const iterator& other) const noexcept;
57 
58  inline bool operator<(const iterator& other) const noexcept;
59  inline bool operator<=(const iterator& other) const noexcept;
60  inline bool operator>=(const iterator& other) const noexcept;
61  inline bool operator>(const iterator& other) const noexcept;
65  inline std::string_view key() const noexcept;
70  inline uint32_t key_length() const noexcept;
75  inline bool key_equals(std::string_view o) const noexcept;
81  inline bool key_equals_case_insensitive(std::string_view o) const noexcept;
85  inline const char *key_c_str() const noexcept;
89  inline element value() const noexcept;
90 
91  iterator() noexcept = default;
92  iterator(const iterator&) noexcept = default;
93  iterator& operator=(const iterator&) noexcept = default;
94  private:
95  simdjson_inline iterator(const internal::tape_ref &tape) noexcept;
96 
97  internal::tape_ref tape;
98 
99  friend class object;
100  };
101 
107  inline iterator begin() const noexcept;
113  inline iterator end() const noexcept;
119  inline size_t size() const noexcept;
135  inline simdjson_result<element> operator[](std::string_view key) const noexcept;
136 
152  inline simdjson_result<element> operator[](const char *key) const noexcept;
153 
177  inline simdjson_result<element> at_pointer(std::string_view json_pointer) const noexcept;
178 
193  inline simdjson_result<element> at_key(std::string_view key) const noexcept;
194 
206  inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
207 
208 private:
209  simdjson_inline object(const internal::tape_ref &tape) noexcept;
210 
211  internal::tape_ref tape;
212 
213  friend class element;
214  friend struct simdjson_result<element>;
215  template<typename T>
216  friend class simdjson::internal::string_builder;
217 };
218 
223 public:
225  std::string_view key;
228 
229 private:
230  simdjson_inline key_value_pair(std::string_view _key, element _value) noexcept;
231  friend class object;
232 };
233 
234 } // namespace dom
235 
237 template<>
238 struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom::object> {
239 public:
240  simdjson_inline simdjson_result() noexcept;
241  simdjson_inline simdjson_result(dom::object value) noexcept;
242  simdjson_inline simdjson_result(error_code error) noexcept;
243 
244  inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
245  inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
246  inline simdjson_result<dom::element> at_pointer(std::string_view json_pointer) const noexcept;
247  inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
248  inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
249 
250 #if SIMDJSON_EXCEPTIONS
251  inline dom::object::iterator begin() const noexcept(false);
252  inline dom::object::iterator end() const noexcept(false);
253  inline size_t size() const noexcept(false);
254 #endif // SIMDJSON_EXCEPTIONS
255 };
256 
257 } // namespace simdjson
258 
259 #if defined(__cpp_lib_ranges)
260 #include <ranges>
261 
262 namespace std {
263 namespace ranges {
264 template<>
265 inline constexpr bool enable_view<simdjson::dom::object> = true;
266 #if SIMDJSON_EXCEPTIONS
267 template<>
268 inline constexpr bool enable_view<simdjson::simdjson_result<simdjson::dom::object>> = true;
269 #endif // SIMDJSON_EXCEPTIONS
270 } // namespace ranges
271 } // namespace std
272 #endif // defined(__cpp_lib_ranges)
273 
274 #endif // SIMDJSON_DOM_OBJECT_H
A JSON element.
Definition: element.h:40
Key/value pair in an object.
Definition: object.h:222
std::string_view key
key in the key-value pair
Definition: object.h:225
element value
value in the key-value pair
Definition: object.h:227
bool operator!=(const iterator &other) const noexcept
Check if these values come from the same place in the JSON.
Definition: object-inl.h:160
element value() const noexcept
Get the value of this key/value pair.
Definition: object-inl.h:197
bool key_equals(std::string_view o) const noexcept
Returns true if the key in this key/value pair is equal to the provided string_view.
Definition: object-inl.h:214
const char * key_c_str() const noexcept
Get the key of this key/value pair.
Definition: object-inl.h:194
uint32_t key_length() const noexcept
Get the length (in bytes) of the key in this key/value pair.
Definition: object-inl.h:191
const value_type operator*() const noexcept
Get the actual key/value pair.
Definition: object-inl.h:157
bool key_equals_case_insensitive(std::string_view o) const noexcept
Returns true if the key in this key/value pair is equal to the provided string_view in a case-insensi...
Definition: object-inl.h:225
std::string_view key() const noexcept
Get the key of this key/value pair.
Definition: object-inl.h:188
iterator & operator++() noexcept
Get the next key/value pair.
Definition: object-inl.h:178
JSON object.
Definition: object.h:22
simdjson_result< element > at_key(std::string_view key) const noexcept
Get the value associated with the given key.
Definition: object-inl.h:131
iterator end() const noexcept
One past the last key/value pair.
Definition: object-inl.h:71
size_t size() const noexcept
Get the size of the object (number of keys).
Definition: object-inl.h:75
simdjson_result< element > at_key_case_insensitive(std::string_view key) const noexcept
Get the value associated with the given key in a case-insensitive manner.
Definition: object-inl.h:143
simdjson_result< element > at_pointer(std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
Definition: object-inl.h:86
simdjson_inline object() noexcept
Create a new, invalid object.
Definition: object-inl.h:65
iterator begin() const noexcept
Return the first key/value pair.
Definition: object-inl.h:67
We want to support argument-dependent lookup (ADL).
error_code
All possible errors returned by simdjson.
Definition: error.h:17
The result of a JSON navigation that may fail.
Definition: element.h:482
The result of a simdjson operation that could fail.
Definition: error.h:205