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