userver: userver/formats/json/array.hpp Source File
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
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 formats::json::Value and cannot represent anything else but a JSON array.
21/// 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 {
34 CheckArray();
35 }
36
37 /// @throw TypeMismatchException if @a value is not an array
38 explicit Array(Value&& value)
39 : Value(std::move(value))
40 {
41 CheckArray();
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 formats::json::Value.
55 const Value& GetValue() const& { return *this; }
56
57 /// @brief Returns 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 /// @brief Compares values.
88 bool operator==(const Array& other) const { return GetValue() == other.GetValue(); }
89 bool operator!=(const Array& other) const { return GetValue() != other.GetValue(); }
90
91 /// @see @ref formats::json::Value::As
92 using Value::As;
93
94 /// @see @ref formats::json::Value::ConvertTo
95 using Value::ConvertTo;
96
97 /// @see @ref formats::json::Value::GetPath
98 using Value::GetPath;
99
100 /// @see @ref formats::json::Value::IsRoot
101 using Value::IsRoot;
102
103 /// @brief Returns a deep copy of array (see @ref formats::json::Value::Clone).
104 Array Clone() const { return Array{GetValue().Clone()}; }
105};
106
107inline Array Parse(const Value& value, parse::To<Array>) { return Array{value}; }
108
109inline Value Serialize(const Array& array, formats::serialize::To<Value>) { return Value{array.GetValue()}; }
110
111inline Value Serialize(Array&& array, formats::serialize::To<Value>) { return Value{std::move(array).ExtractValue()}; }
112
113} // namespace formats::json
114
115USERVER_NAMESPACE_END