userver: userver/formats/json/object.hpp Source File
Loading...
Searching...
No Matches
object.hpp
Go to the documentation of this file.
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 formats::json::Value and cannot represent anything else but a JSON object.
19/// Use it when you need to explicitly state that only JSON object is expected.
20class Object final : private Value {
21public:
22 /// @brief Creates empty object.
24
25 /// @throw TypeMismatchException if @a value is not an object
26 explicit Object(const Value& value)
27 : Value(value)
28 {
29 CheckObject();
30 }
31
32 /// @throw TypeMismatchException if @a value is not an object
33 explicit Object(Value&& value)
34 : Value(std::move(value))
35 {
36 CheckObject();
37 }
38
39 /// @brief Creates object extracting value from the @a builder.
40 /// @throw TypeMismatchException if extracted value is not an object.
41 explicit Object(ValueBuilder&& builder);
42
43 Object(const Object&) = default;
44 Object(Object&&) noexcept = default;
45
46 Object& operator=(const Object&) & = default;
47 Object& operator=(Object&&) noexcept = default;
48
49 /// @brief Returns formats::json::Value.
50 const Value& GetValue() const& { return *this; }
51
52 /// @brief Returns formats::json::Value.
53 Value&& ExtractValue() && { return std::move(*this); }
54
55 /// @see @ref formats::json::Value::operator[]
56 using Value::operator[];
57
58 // hide overload intended for JSON arrays
59 Value operator[](std::size_t index) const = delete;
60
61 /// @see @ref formats::json::Value::IsEmpty
62 using Value::IsEmpty;
63
64 /// @see @ref formats::json::Value::GetSize
65 using Value::GetSize;
66
67 /// @brief Compares values.
68 bool operator==(const Object& other) const { return GetValue() == other.GetValue(); }
69 bool operator!=(const Object& other) const { return GetValue() != other.GetValue(); }
70
71 /// @see @ref formats::json::Value::As
72 using Value::As;
73
74 /// @see @ref formats::json::Value::ConvertTo
75 using Value::ConvertTo;
76
77 /// @see @ref formats::json::Value::HasMember
78 using Value::HasMember;
79
80 /// @see @ref formats::json::Value::GetPath
81 using Value::GetPath;
82
83 /// @see @ref formats::json::Value::IsRoot
84 using Value::IsRoot;
85
86 /// @brief Returns a deep copy of object (see @ref formats::json::Value::Clone).
87 Object Clone() const { return Object{GetValue().Clone()}; }
88};
89
90inline Object Parse(const Value& value, parse::To<Object>) { return Object{value}; }
91
92inline Value Serialize(const Object& object, formats::serialize::To<Value>) { return Value{object.GetValue()}; }
93
94inline Value Serialize(Object&& object, formats::serialize::To<Value>) {
95 return Value{std::move(object).ExtractValue()};
96}
97
98} // namespace formats::json
99
100USERVER_NAMESPACE_END