1#include "simdjson/error.h"
2
3namespace simdjson {
4namespace SIMDJSON_IMPLEMENTATION {
5namespace ondemand {
6
7/**
8 * A JSON field (key/value pair) in an object.
9 *
10 * Returned from object iteration.
11 *
12 * Extends from std::pair<raw_json_string, value> so you can use C++ algorithms that rely on pairs.
13 */
14class field : public std::pair<raw_json_string, value> {
15public:
16 /**
17 * Create a new invalid field.
18 *
19 * Exists so you can declare a variable and later assign to it before use.
20 */
21 simdjson_inline field() noexcept;
22
23 /**
24 * Get the key as a string_view (for higher speed, consider raw_key).
25 * We deliberately use a more cumbersome name (unescaped_key) to force users
26 * to think twice about using it.
27 *
28 * This consumes the key: once you have called unescaped_key(), you cannot
29 * call it again nor can you call key().
30 */
31 simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> unescaped_key(bool allow_replacement) noexcept;
32 /**
33 * Get the key as a raw_json_string. Can be used for direct comparison with
34 * an unescaped C string: e.g., key() == "test".
35 */
36 simdjson_inline raw_json_string key() const noexcept;
37 /**
38 * Get the field value.
39 */
40 simdjson_inline ondemand::value &value() & noexcept;
41 /**
42 * @overload ondemand::value &ondemand::value() & noexcept
43 */
44 simdjson_inline ondemand::value value() && noexcept;
45
46protected:
47 simdjson_inline field(raw_json_string key, ondemand::value &&value) noexcept;
48 static simdjson_inline simdjson_result<field> start(value_iterator &parent_iter) noexcept;
49 static simdjson_inline simdjson_result<field> start(const value_iterator &parent_iter, raw_json_string key) noexcept;
50 friend struct simdjson_result<field>;
51 friend class object_iterator;
52};
53
54} // namespace ondemand
55} // namespace SIMDJSON_IMPLEMENTATION
56} // namespace simdjson
57
58namespace simdjson {
59
60template<>
61struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::field> {
62public:
63 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::field &&value) noexcept; ///< @private
64 simdjson_inline simdjson_result(error_code error) noexcept; ///< @private
65 simdjson_inline simdjson_result() noexcept = default;
66
67 simdjson_inline simdjson_result<std::string_view> unescaped_key(bool allow_replacement = false) noexcept;
68 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> key() noexcept;
69 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> value() noexcept;
70};
71
72} // namespace simdjson
73