simdjson  3.7.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 
172  inline simdjson_result<element> at_pointer(std::string_view json_pointer) const noexcept;
173 
188  inline simdjson_result<element> at_key(std::string_view key) const noexcept;
189 
201  inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
202 
203 private:
204  simdjson_inline object(const internal::tape_ref &tape) noexcept;
205 
206  internal::tape_ref tape;
207 
208  friend class element;
209  friend struct simdjson_result<element>;
210  template<typename T>
211  friend class simdjson::internal::string_builder;
212 };
213 
218 public:
220  std::string_view key;
223 
224 private:
225  simdjson_inline key_value_pair(std::string_view _key, element _value) noexcept;
226  friend class object;
227 };
228 
229 } // namespace dom
230 
232 template<>
233 struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom::object> {
234 public:
235  simdjson_inline simdjson_result() noexcept;
236  simdjson_inline simdjson_result(dom::object value) noexcept;
237  simdjson_inline simdjson_result(error_code error) noexcept;
238 
239  inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
240  inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
241  inline simdjson_result<dom::element> at_pointer(std::string_view json_pointer) const noexcept;
242  inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
243  inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
244 
245 #if SIMDJSON_EXCEPTIONS
246  inline dom::object::iterator begin() const noexcept(false);
247  inline dom::object::iterator end() const noexcept(false);
248  inline size_t size() const noexcept(false);
249 #endif // SIMDJSON_EXCEPTIONS
250 };
251 
252 } // namespace simdjson
253 
254 #if defined(__cpp_lib_ranges)
255 #include <ranges>
256 
257 namespace std {
258 namespace ranges {
259 template<>
260 inline constexpr bool enable_view<simdjson::dom::object> = true;
261 #if SIMDJSON_EXCEPTIONS
262 template<>
263 inline constexpr bool enable_view<simdjson::simdjson_result<simdjson::dom::object>> = true;
264 #endif // SIMDJSON_EXCEPTIONS
265 } // namespace ranges
266 } // namespace std
267 #endif // defined(__cpp_lib_ranges)
268 
269 #endif // SIMDJSON_DOM_OBJECT_H
A JSON element.
Definition: element.h:31
Key/value pair in an object.
Definition: object.h:217
std::string_view key
key in the key-value pair
Definition: object.h:220
element value
value in the key-value pair
Definition: object.h:222
bool operator!=(const iterator &other) const noexcept
Check if these values come from the same place in the JSON.
Definition: object-inl.h:163
element value() const noexcept
Get the value of this key/value pair.
Definition: object-inl.h:200
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:217
const char * key_c_str() const noexcept
Get the key of this key/value pair.
Definition: object-inl.h:197
uint32_t key_length() const noexcept
Get the length (in bytes) of the key in this key/value pair.
Definition: object-inl.h:194
reference operator*() const noexcept
Get the actual key/value pair.
Definition: object-inl.h:160
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:228
std::string_view key() const noexcept
Get the key of this key/value pair.
Definition: object-inl.h:191
iterator & operator++() noexcept
Get the next key/value pair.
Definition: object-inl.h:181
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:134
iterator end() const noexcept
One past the last key/value pair.
Definition: object-inl.h:74
size_t size() const noexcept
Get the size of the object (number of keys).
Definition: object-inl.h:78
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:146
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:89
simdjson_inline object() noexcept
Create a new, invalid object.
Definition: object-inl.h:68
iterator begin() const noexcept
Return the first key/value pair.
Definition: object-inl.h:70
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:492
The result of a simdjson operation that could fail.
Definition: error.h:215