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) : Value(value) { CheckObject(); }
27
28 /// @throw TypeMismatchException if @a value is not an object
29 explicit Object(Value&& value) : Value(std::move(value)) { CheckObject(); }
30
31 /// @brief Creates object extracting value from the @a builder.
32 /// @throw TypeMismatchException if extracted value is not an object.
33 explicit Object(ValueBuilder&& builder);
34
35 Object(const Object&) = default;
36 Object(Object&&) noexcept = default;
37
38 Object& operator=(const Object&) & = default;
39 Object& operator=(Object&&) noexcept = default;
40
41 /// @brief Returns formats::json::Value.
42 const Value& GetValue() const& { return *this; }
43
44 /// @brief Returns formats::json::Value.
45 Value&& ExtractValue() && { return std::move(*this); }
46
47 /// @see @ref formats::json::Value::operator[]
48 using Value::operator[];
49
50 // hide overload intended for JSON arrays
51 Value operator[](std::size_t index) const = delete;
52
53 /// @see @ref formats::json::Value::IsEmpty
54 using Value::IsEmpty;
55
56 /// @see @ref formats::json::Value::GetSize
57 using Value::GetSize;
58
59 /// @brief Compares values.
60 bool operator==(const Object& other) const { return GetValue() == other.GetValue(); }
61 bool operator!=(const Object& other) const { return GetValue() != other.GetValue(); }
62
63 /// @see @ref formats::json::Value::As
64 using Value::As;
65
66 /// @see @ref formats::json::Value::ConvertTo
67 using Value::ConvertTo;
68
69 /// @see @ref formats::json::Value::HasMember
70 using Value::HasMember;
71
72 /// @see @ref formats::json::Value::GetPath
73 using Value::GetPath;
74
75 /// @see @ref formats::json::Value::IsRoot
76 using Value::IsRoot;
77
78 /// @brief Returns a deep copy of object (see @ref formats::json::Value::Clone).
79 Object Clone() const { return Object{GetValue().Clone()}; }
80};
81
82inline Object Parse(const Value& value, parse::To<Object>) { return Object{value}; }
83
84inline Value Serialize(const Object& object, formats::serialize::To<Value>) { return Value{object.GetValue()}; }
85
86inline Value Serialize(Object&& object, formats::serialize::To<Value>) {
87 return Value{std::move(object).ExtractValue()};
88}
89
90} // namespace formats::json
91
92USERVER_NAMESPACE_END