simdjson  3.11.0
Ridiculously Fast JSON
object.h
1 #ifndef SIMDJSON_DOM_OBJECT_H
2 #define SIMDJSON_DOM_OBJECT_H
3 
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/element.h"
6 #include "simdjson/internal/tape_ref.h"
7 
8 namespace simdjson {
9 namespace dom {
10 
14 class object {
15 public:
17  simdjson_inline object() noexcept;
18 
19  class iterator {
20  public:
21  using value_type = const key_value_pair;
22  using difference_type = std::ptrdiff_t;
23  using pointer = void;
24  using reference = value_type;
25  using iterator_category = std::forward_iterator_tag;
26 
30  inline reference operator*() const noexcept;
37  inline iterator& operator++() noexcept;
44  inline iterator operator++(int) noexcept;
50  inline bool operator!=(const iterator& other) const noexcept;
51  inline bool operator==(const iterator& other) const noexcept;
52 
53  inline bool operator<(const iterator& other) const noexcept;
54  inline bool operator<=(const iterator& other) const noexcept;
55  inline bool operator>=(const iterator& other) const noexcept;
56  inline bool operator>(const iterator& other) const noexcept;
60  inline std::string_view key() const noexcept;
65  inline uint32_t key_length() const noexcept;
70  inline bool key_equals(std::string_view o) const noexcept;
76  inline bool key_equals_case_insensitive(std::string_view o) const noexcept;
80  inline const char *key_c_str() const noexcept;
84  inline element value() const noexcept;
85 
86  iterator() noexcept = default;
87  iterator(const iterator&) noexcept = default;
88  iterator& operator=(const iterator&) noexcept = default;
89  private:
90  simdjson_inline iterator(const internal::tape_ref &tape) noexcept;
91 
92  internal::tape_ref tape;
93 
94  friend class object;
95  };
96 
102  inline iterator begin() const noexcept;
108  inline iterator end() const noexcept;
114  inline size_t size() const noexcept;
130  inline simdjson_result<element> operator[](std::string_view key) const noexcept;
131 
147  inline simdjson_result<element> operator[](const char *key) const noexcept;
148  simdjson_result<element> operator[](int) const noexcept = delete;
149 
173  inline simdjson_result<element> at_pointer(std::string_view json_pointer) const noexcept;
174 
188  inline simdjson_result<element> at_path(std::string_view json_path) const noexcept;
189 
204  inline simdjson_result<element> at_key(std::string_view key) const noexcept;
205 
217  inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
218 
222  inline operator element() const noexcept;
223 
224 private:
225  simdjson_inline object(const internal::tape_ref &tape) noexcept;
226 
227  internal::tape_ref tape;
228 
229  friend class element;
230  friend struct simdjson_result<element>;
231  template<typename T>
232  friend class simdjson::internal::string_builder;
233 };
234 
239 public:
241  std::string_view key;
244 
245 private:
246  simdjson_inline key_value_pair(std::string_view _key, element _value) noexcept;
247  friend class object;
248 };
249 
250 } // namespace dom
251 
253 template<>
254 struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom::object> {
255 public:
256  simdjson_inline simdjson_result() noexcept;
257  simdjson_inline simdjson_result(dom::object value) noexcept;
258  simdjson_inline simdjson_result(error_code error) noexcept;
259 
260  inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
261  inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
262  simdjson_result<dom::element> operator[](int) const noexcept = delete;
263  inline simdjson_result<dom::element> at_pointer(std::string_view json_pointer) const noexcept;
264  inline simdjson_result<dom::element> at_path(std::string_view json_path) const noexcept;
265  inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
266  inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
267 
268 #if SIMDJSON_EXCEPTIONS
269  inline dom::object::iterator begin() const noexcept(false);
270  inline dom::object::iterator end() const noexcept(false);
271  inline size_t size() const noexcept(false);
272 #endif // SIMDJSON_EXCEPTIONS
273 };
274 
275 } // namespace simdjson
276 
277 #if defined(__cpp_lib_ranges)
278 #include <ranges>
279 
280 namespace std {
281 namespace ranges {
282 template<>
283 inline constexpr bool enable_view<simdjson::dom::object> = true;
284 #if SIMDJSON_EXCEPTIONS
285 template<>
286 inline constexpr bool enable_view<simdjson::simdjson_result<simdjson::dom::object>> = true;
287 #endif // SIMDJSON_EXCEPTIONS
288 } // namespace ranges
289 } // namespace std
290 #endif // defined(__cpp_lib_ranges)
291 
292 #endif // SIMDJSON_DOM_OBJECT_H
A JSON element.
Definition: element.h:31
Key/value pair in an object.
Definition: object.h:238
std::string_view key
key in the key-value pair
Definition: object.h:241
element value
value in the key-value pair
Definition: object.h:243
bool operator!=(const iterator &other) const noexcept
Check if these values come from the same place in the JSON.
Definition: object-inl.h:179
element value() const noexcept
Get the value of this key/value pair.
Definition: object-inl.h:216
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:233
const char * key_c_str() const noexcept
Get the key of this key/value pair.
Definition: object-inl.h:213
uint32_t key_length() const noexcept
Get the length (in bytes) of the key in this key/value pair.
Definition: object-inl.h:210
reference operator*() const noexcept
Get the actual key/value pair.
Definition: object-inl.h:176
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:244
std::string_view key() const noexcept
Get the key of this key/value pair.
Definition: object-inl.h:207
iterator & operator++() noexcept
Get the next key/value pair.
Definition: object-inl.h:197
JSON object.
Definition: object.h:14
simdjson_result< element > at_key(std::string_view key) const noexcept
Get the value associated with the given key.
Definition: object-inl.h:146
iterator end() const noexcept
One past the last key/value pair.
Definition: object-inl.h:80
size_t size() const noexcept
Get the size of the object (number of keys).
Definition: object-inl.h:84
simdjson_result< element > at_path(std::string_view json_path) const noexcept
Get the value associated with the given JSONPath expression.
Definition: object-inl.h:140
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:158
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:95
simdjson_inline object() noexcept
Create a new, invalid object.
Definition: object-inl.h:74
iterator begin() const noexcept
Return the first key/value pair.
Definition: object-inl.h:76
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
The result of a JSON navigation that may fail.
Definition: element.h:509
The result of a simdjson operation that could fail.
Definition: error.h:215