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