userver: userver/formats/bson/serialize.hpp Source File
Loading...
Searching...
No Matches
serialize.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <iosfwd>
7#include <string>
8#include <string_view>
9
10#include <fmt/core.h>
11
19
20USERVER_NAMESPACE_BEGIN
21
22namespace formats::parse {
23
39
46
62
71
72} // namespace formats::parse
73
74namespace formats::bson {
75
77class JsonString;
78
84Document FromJsonString(std::string_view json);
85
91Value ArrayFromJsonString(std::string_view json);
92
97
105
113
117
118namespace impl {
119class JsonStringImpl;
120} // namespace impl
121
123public:
125 explicit JsonString(impl::JsonStringImpl&&);
127 ~JsonString();
128
130 /*implicit*/ operator std::string() const { return ToString(); }
131
133 std::string ToString() const;
134
136 std::string_view GetView() const;
137
138 const char* Data() const;
139 size_t Size() const;
140
141private:
142 static constexpr std::size_t kSize =
144 .For64Bit(16)
145 .For32Bit(8);
146 static constexpr std::size_t kAlignment = alignof(void*);
148};
149
150std::ostream& operator<<(std::ostream&, const JsonString&);
151
153
156
157} // namespace formats::bson
158
159USERVER_NAMESPACE_END
160
161template <>
162struct fmt::formatter<USERVER_NAMESPACE::formats::bson::JsonString> : public fmt::formatter<std::string_view> {
163 template <typename FormatContext>
164 auto format(const USERVER_NAMESPACE::formats::bson::JsonString& json, FormatContext& ctx) USERVER_FMT_CONST {
165 return fmt::formatter<std::string_view>::format(json.GetView(), ctx);
166 }
167};
168
170template <>
171struct fmt::formatter<USERVER_NAMESPACE::formats::bson::Document> : public fmt::formatter<std::string_view> {
172 template <typename FormatContext>
173 auto format(const USERVER_NAMESPACE::formats::bson::Document& bson, FormatContext& ctx) USERVER_FMT_CONST {
174 return fmt::formatter<
175 std::string_view>::format(USERVER_NAMESPACE::formats::bson::ToRelaxedJsonString(bson).GetView(), ctx);
176 }
177};