simdjson  3.4.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 = key_value_pair;
22  using difference_type = std::ptrdiff_t;
23 
27  inline const value_type operator*() const noexcept;
34  inline iterator& operator++() noexcept;
41  inline iterator operator++(int) noexcept;
47  inline bool operator!=(const iterator& other) const noexcept;
48  inline bool operator==(const iterator& other) const noexcept;
49 
50  inline bool operator<(const iterator& other) const noexcept;
51  inline bool operator<=(const iterator& other) const noexcept;
52  inline bool operator>=(const iterator& other) const noexcept;
53  inline bool operator>(const iterator& other) const noexcept;
57  inline std::string_view key() const noexcept;
62  inline uint32_t key_length() const noexcept;
67  inline bool key_equals(std::string_view o) const noexcept;
73  inline bool key_equals_case_insensitive(std::string_view o) const noexcept;
77  inline const char *key_c_str() const noexcept;
81  inline element value() const noexcept;
82 
83  iterator() noexcept = default;
84  iterator(const iterator&) noexcept = default;
85  iterator& operator=(const iterator&) noexcept = default;
86  private:
87  simdjson_inline iterator(const internal::tape_ref &tape) noexcept;
88 
89  internal::tape_ref tape;
90 
91  friend class object;
92  };
93 
99  inline iterator begin() const noexcept;
105  inline iterator end() const noexcept;
111  inline size_t size() const noexcept;
127  inline simdjson_result<element> operator[](std::string_view key) const noexcept;
128 
144  inline simdjson_result<element> operator[](const char *key) const noexcept;
145 
169  inline simdjson_result<element> at_pointer(std::string_view json_pointer) const noexcept;
170 
185  inline simdjson_result<element> at_key(std::string_view key) const noexcept;
186 
198  inline simdjson_result<element> at_key_case_insensitive(std::string_view key) const noexcept;
199 
200 private:
201  simdjson_inline object(const internal::tape_ref &tape) noexcept;
202 
203  internal::tape_ref tape;
204 
205  friend class element;
206  friend struct simdjson_result<element>;
207  template<typename T>
208  friend class simdjson::internal::string_builder;
209 };
210 
215 public:
217  std::string_view key;
220 
221 private:
222  simdjson_inline key_value_pair(std::string_view _key, element _value) noexcept;
223  friend class object;
224 };
225 
226 } // namespace dom
227 
229 template<>
230 struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom::object> {
231 public:
232  simdjson_inline simdjson_result() noexcept;
233  simdjson_inline simdjson_result(dom::object value) noexcept;
234  simdjson_inline simdjson_result(error_code error) noexcept;
235 
236  inline simdjson_result<dom::element> operator[](std::string_view key) const noexcept;
237  inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
238  inline simdjson_result<dom::element> at_pointer(std::string_view json_pointer) const noexcept;
239  inline simdjson_result<dom::element> at_key(std::string_view key) const noexcept;
240  inline simdjson_result<dom::element> at_key_case_insensitive(std::string_view key) const noexcept;
241 
242 #if SIMDJSON_EXCEPTIONS
243  inline dom::object::iterator begin() const noexcept(false);
244  inline dom::object::iterator end() const noexcept(false);
245  inline size_t size() const noexcept(false);
246 #endif // SIMDJSON_EXCEPTIONS
247 };
248 
249 } // namespace simdjson
250 
251 #if defined(__cpp_lib_ranges)
252 #include <ranges>
253 
254 namespace std {
255 namespace ranges {
256 template<>
257 inline constexpr bool enable_view<simdjson::dom::object> = true;
258 #if SIMDJSON_EXCEPTIONS
259 template<>
260 inline constexpr bool enable_view<simdjson::simdjson_result<simdjson::dom::object>> = true;
261 #endif // SIMDJSON_EXCEPTIONS
262 } // namespace ranges
263 } // namespace std
264 #endif // defined(__cpp_lib_ranges)
265 
266 #endif // SIMDJSON_DOM_OBJECT_H
A JSON element.
Definition: element.h:31
Key/value pair in an object.
Definition: object.h:214
std::string_view key
key in the key-value pair
Definition: object.h:217
element value
value in the key-value pair
Definition: object.h:219
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
const value_type 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:493
The result of a simdjson operation that could fail.
Definition: error.h:207