| 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 | |
| 19 | /** |
| 20 | * JSON object. |
| 21 | */ |
| 22 | class object { |
| 23 | public: |
| 24 | /** Create a new, invalid object */ |
| 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 | |
| 32 | /** |
| 33 | * Get the actual key/value pair |
| 34 | */ |
| 35 | inline const value_type operator*() const noexcept; |
| 36 | /** |
| 37 | * Get the next key/value pair. |
| 38 | * |
| 39 | * Part of the std::iterator interface. |
| 40 | * |
| 41 | */ |
| 42 | inline iterator& operator++() noexcept; |
| 43 | /** |
| 44 | * Get the next key/value pair. |
| 45 | * |
| 46 | * Part of the std::iterator interface. |
| 47 | * |
| 48 | */ |
| 49 | inline iterator operator++(int) noexcept; |
| 50 | /** |
| 51 | * Check if these values come from the same place in the JSON. |
| 52 | * |
| 53 | * Part of the std::iterator interface. |
| 54 | */ |
| 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; |
| 62 | /** |
| 63 | * Get the key of this key/value pair. |
| 64 | */ |
| 65 | inline std::string_view key() const noexcept; |
| 66 | /** |
| 67 | * Get the length (in bytes) of the key in this key/value pair. |
| 68 | * You should expect this function to be faster than key().size(). |
| 69 | */ |
| 70 | inline uint32_t key_length() const noexcept; |
| 71 | /** |
| 72 | * Returns true if the key in this key/value pair is equal |
| 73 | * to the provided string_view. |
| 74 | */ |
| 75 | inline bool key_equals(std::string_view o) const noexcept; |
| 76 | /** |
| 77 | * Returns true if the key in this key/value pair is equal |
| 78 | * to the provided string_view in a case-insensitive manner. |
| 79 | * Case comparisons may only be handled correctly for ASCII strings. |
| 80 | */ |
| 81 | inline bool key_equals_case_insensitive(std::string_view o) const noexcept; |
| 82 | /** |
| 83 | * Get the key of this key/value pair. |
| 84 | */ |
| 85 | inline const char *key_c_str() const noexcept; |
| 86 | /** |
| 87 | * Get the value of this key/value pair. |
| 88 | */ |
| 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 | |
| 102 | /** |
| 103 | * Return the first key/value pair. |
| 104 | * |
| 105 | * Part of the std::iterable interface. |
| 106 | */ |
| 107 | inline iterator begin() const noexcept; |
| 108 | /** |
| 109 | * One past the last key/value pair. |
| 110 | * |
| 111 | * Part of the std::iterable interface. |
| 112 | */ |
| 113 | inline iterator end() const noexcept; |
| 114 | /** |
| 115 | * Get the size of the object (number of keys). |
| 116 | * It is a saturated value with a maximum of 0xFFFFFF: if the value |
| 117 | * is 0xFFFFFF then the size is 0xFFFFFF or greater. |
| 118 | */ |
| 119 | inline size_t size() const noexcept; |
| 120 | /** |
| 121 | * Get the value associated with the given key. |
| 122 | * |
| 123 | * The key will be matched against **unescaped** JSON: |
| 124 | * |
| 125 | * dom::parser parser; |
| 126 | * int64_t(parser.parse(R"({ "a\n": 1 })"_padded)["a\n"]) == 1 |
| 127 | * parser.parse(R"({ "a\n": 1 })"_padded)["a\\n"].get_uint64().error() == NO_SUCH_FIELD |
| 128 | * |
| 129 | * This function has linear-time complexity: the keys are checked one by one. |
| 130 | * |
| 131 | * @return The value associated with this field, or: |
| 132 | * - NO_SUCH_FIELD if the field does not exist in the object |
| 133 | * - INCORRECT_TYPE if this is not an object |
| 134 | */ |
| 135 | inline simdjson_result<element> operator[](std::string_view key) const noexcept; |
| 136 | |
| 137 | /** |
| 138 | * Get the value associated with the given key. |
| 139 | * |
| 140 | * The key will be matched against **unescaped** JSON: |
| 141 | * |
| 142 | * dom::parser parser; |
| 143 | * int64_t(parser.parse(R"({ "a\n": 1 })"_padded)["a\n"]) == 1 |
| 144 | * parser.parse(R"({ "a\n": 1 })"_padded)["a\\n"].get_uint64().error() == NO_SUCH_FIELD |
| 145 | * |
| 146 | * This function has linear-time complexity: the keys are checked one by one. |
| 147 | * |
| 148 | * @return The value associated with this field, or: |
| 149 | * - NO_SUCH_FIELD if the field does not exist in the object |
| 150 | * - INCORRECT_TYPE if this is not an object |
| 151 | */ |
| 152 | inline simdjson_result<element> operator[](const char *key) const noexcept; |
| 153 | |
| 154 | /** |
| 155 | * Get the value associated with the given JSON pointer. We use the RFC 6901 |
| 156 | * https://tools.ietf.org/html/rfc6901 standard, interpreting the current node |
| 157 | * as the root of its own JSON document. |
| 158 | * |
| 159 | * dom::parser parser; |
| 160 | * object obj = parser.parse(R"({ "foo": { "a": [ 10, 20, 30 ] }})"_padded); |
| 161 | * obj.at_pointer("/foo/a/1") == 20 |
| 162 | * obj.at_pointer("/foo")["a"].at(1) == 20 |
| 163 | * |
| 164 | * It is allowed for a key to be the empty string: |
| 165 | * |
| 166 | * dom::parser parser; |
| 167 | * object obj = parser.parse(R"({ "": { "a": [ 10, 20, 30 ] }})"_padded); |
| 168 | * obj.at_pointer("//a/1") == 20 |
| 169 | * obj.at_pointer("/")["a"].at(1) == 20 |
| 170 | * |
| 171 | * @return The value associated with the given JSON pointer, or: |
| 172 | * - NO_SUCH_FIELD if a field does not exist in an object |
| 173 | * - INDEX_OUT_OF_BOUNDS if an array index is larger than an array length |
| 174 | * - INCORRECT_TYPE if a non-integer is used to access an array |
| 175 | * - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed |
| 176 | */ |
| 177 | inline simdjson_result<element> at_pointer(std::string_view json_pointer) const noexcept; |
| 178 | |
| 179 | /** |
| 180 | * Get the value associated with the given key. |
| 181 | * |
| 182 | * The key will be matched against **unescaped** JSON: |
| 183 | * |
| 184 | * dom::parser parser; |
| 185 | * int64_t(parser.parse(R"({ "a\n": 1 })"_padded)["a\n"]) == 1 |
| 186 | * parser.parse(R"({ "a\n": 1 })"_padded)["a\\n"].get_uint64().error() == NO_SUCH_FIELD |
| 187 | * |
| 188 | * This function has linear-time complexity: the keys are checked one by one. |
| 189 | * |
| 190 | * @return The value associated with this field, or: |
| 191 | * - NO_SUCH_FIELD if the field does not exist in the object |
| 192 | */ |
| 193 | inline simdjson_result<element> at_key(std::string_view key) const noexcept; |
| 194 | |
| 195 | /** |
| 196 | * Get the value associated with the given key in a case-insensitive manner. |
| 197 | * It is only guaranteed to work over ASCII inputs. |
| 198 | * |
| 199 | * Note: The key will be matched against **unescaped** JSON. |
| 200 | * |
| 201 | * This function has linear-time complexity: the keys are checked one by one. |
| 202 | * |
| 203 | * @return The value associated with this field, or: |
| 204 | * - NO_SUCH_FIELD if the field does not exist in the object |
| 205 | */ |
| 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 | |
| 219 | /** |
| 220 | * Key/value pair in an object. |
| 221 | */ |
| 222 | class key_value_pair { |
| 223 | public: |
| 224 | /** key in the key-value pair **/ |
| 225 | std::string_view key; |
| 226 | /** value in the key-value pair **/ |
| 227 | element value; |
| 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 | |
| 236 | /** The result of a JSON conversion that may fail. */ |
| 237 | template<> |
| 238 | struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom::object> { |
| 239 | public: |
| 240 | simdjson_inline simdjson_result() noexcept; ///< @private |
| 241 | simdjson_inline simdjson_result(dom::object value) noexcept; ///< @private |
| 242 | simdjson_inline simdjson_result(error_code error) noexcept; ///< @private |
| 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 |
| 275 | |