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) : Value(value) { CheckArray(); }
32
33 /// @throw TypeMismatchException if @a value is not an array
34 explicit Array(Value&& value) : Value(std::move(value)) { CheckArray(); }
35
36 /// @brief Creates array extracting value from the @a builder.
37 /// @throw TypeMismatchException if extracted value is not an array.
38 explicit Array(ValueBuilder&& builder);
39
40 Array(const Array&) = default;
41 Array(Array&&) noexcept = default;
42
43 Array& operator=(const Array&) & = default;
44 Array& operator=(Array&&) noexcept = default;
45
46 /// @brief Returns formats::json::Value.
47 const Value& GetValue() const& { return *this; }
48
49 /// @brief Returns formats::json::Value.
50 Value&& ExtractValue() && { return std::move(*this); }
51
52 /// @see @ref formats::json::Value::operator[]
53 using Value::operator[];
54
55 // hide overload intended for JSON objects
56 Value operator[](std::string_view key) const = delete;
57
58 /// @see @ref formats::json::Value::begin
59 using Value::begin;
60
61 /// @see @ref formats::json::Value::end
62 using Value::end;
63
64 /// @see @ref formats::json::Value::rbegin
65 using Value::rbegin;
66
67 /// @see @ref formats::json::Value::rend
68 using Value::rend;
69
70 /// @see @ref formats::json::Value::IsEmpty
71 using Value::IsEmpty;
72
73 /// @see @ref formats::json::Value::GetSize
74 using Value::GetSize;
75
76 /// @see @ref formats::json::Value::CheckInBounds
77 using Value::CheckInBounds;
78
79 /// @brief Compares values.
80 bool operator==(const Array& other) const { return GetValue() == other.GetValue(); }
81 bool operator!=(const Array& other) const { return GetValue() != other.GetValue(); }
82
83 /// @see @ref formats::json::Value::As
84 using Value::As;
85
86 /// @see @ref formats::json::Value::ConvertTo
87 using Value::ConvertTo;
88
89 /// @see @ref formats::json::Value::GetPath
90 using Value::GetPath;
91
92 /// @see @ref formats::json::Value::IsRoot
93 using Value::IsRoot;
94
95 /// @brief Returns a deep copy of array (see @ref formats::json::Value::Clone).
96 Array Clone() const { return Array{GetValue().Clone()}; }
97};
98
99inline Array Parse(const Value& value, parse::To<Array>) { return Array{value}; }
100
101inline Value Serialize(const Array& array, formats::serialize::To<Value>) { return Value{array.GetValue()}; }
102
103inline Value Serialize(Array&& array, formats::serialize::To<Value>) { return Value{std::move(array).ExtractValue()}; }
104
105} // namespace formats::json
106
107USERVER_NAMESPACE_END