userver: userver/formats/json/array.hpp Source File
Loading...
Searching...
No Matches
array.hpp
1#pragma once
2
3/// @file userver/formats/json/array.hpp
4/// @brief @copybrief formats::json::Array
5
6#include <userver/formats/json/value.hpp>
7#include <userver/formats/parse/to.hpp>
8#include <userver/formats/serialize/to.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12/// @brief JSON document model (`formats::json::Value`) and helpers.
13namespace formats::json {
14
15class ValueBuilder;
16
17/// @ingroup userver_universal userver_containers userver_formats
18///
19/// @brief Non-mutable JSON array representation.
20///
21/// This class is implemented in terms of @ref formats::json::Value and cannot represent anything else but a JSON
22/// array. Use it when you need to explicitly state that only JSON array is expected.
23class Array final : private Value {
24public:
25 using Value::const_iterator;
26 using Value::const_reverse_iterator;
27
28 /// @brief Creates empty array.
30
31 /// @throw TypeMismatchException if @a value is not an array
32 explicit Array(const Value& value)
33 : Value(value)
34 {
36 }
37
38 /// @throw TypeMismatchException if @a value is not an array
39 explicit Array(Value&& value)
40 : Value(std::move(value))
41 {
43 }
44
45 /// @brief Creates array extracting value from the @a builder.
46 /// @throw TypeMismatchException if extracted value is not an array.
47 explicit Array(ValueBuilder&& builder);
48
49 Array(const Array&) = default;
50 Array(Array&&) noexcept = default;
51
52 Array& operator=(const Array&) & = default;
53 Array& operator=(Array&&) noexcept = default;
54
55 /// @brief Returns @ref formats::json::Value.
56 const Value& GetValue() const& { return *this; }
57
58 /// @brief Returns @ref formats::json::Value.
59 Value&& ExtractValue() && { return std::move(*this); }
60
61 /// @see @ref formats::json::Value::operator[]
62 using Value::operator[];
63
64 // hide overload intended for JSON objects
65 Value operator[](std::string_view key) const = delete;
66
67 /// @see @ref formats::json::Value::begin
68 using Value::begin;
69
70 /// @see @ref formats::json::Value::end
71 using Value::end;
72
73 /// @see @ref formats::json::Value::rbegin
74 using Value::rbegin;
75
76 /// @see @ref formats::json::Value::rend
77 using Value::rend;
78
79 /// @see @ref formats::json::Value::IsEmpty
80 using Value::IsEmpty;
81
82 /// @see @ref formats::json::Value::GetSize
83 using Value::GetSize;
84
85 /// @see @ref formats::json::Value::CheckInBounds
86 using Value::CheckInBounds;
87
88 /// @see @ref formats::json::Value::As
89 using Value::As;
90
91 /// @see @ref formats::json::Value::ConvertTo
92 using Value::ConvertTo;
93
94 /// @see @ref formats::json::Value::GetPath
95 using Value::GetPath;
96
97 /// @see @ref formats::json::Value::IsRoot
98 using Value::IsRoot;
99
100 /// @brief Returns a deep copy of array (see @ref formats::json::Value::Clone).
101 Array Clone() const { return Array{GetValue().Clone()}; }
102};
103
104/// @brief Compares values.
105inline bool operator==(const Array& lhs, const Array& rhs) { return lhs.GetValue() == rhs.GetValue(); }
106inline bool operator==(const Array& lhs, const Value& rhs) { return lhs.GetValue() == rhs; }
107inline bool operator==(const Value& lhs, const Array& rhs) { return lhs == rhs.GetValue(); }
108inline bool operator!=(const Array& lhs, const Array& rhs) { return lhs.GetValue() != rhs.GetValue(); }
109inline bool operator!=(const Array& lhs, const Value& rhs) { return lhs.GetValue() != rhs; }
110inline bool operator!=(const Value& lhs, const Array& rhs) { return lhs != rhs.GetValue(); }
111
112inline Array Parse(const Value& value, parse::To<Array>) { return Array{value}; }
113
114inline Value Serialize(const Array& array, formats::serialize::To<Value>) { return Value{array.GetValue()}; }
115
116inline Value Serialize(Array&& array, formats::serialize::To<Value>) { return Value{std::move(array).ExtractValue()}; }
117
118} // namespace formats::json
119
120USERVER_NAMESPACE_END