userver: userver/formats/json/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/format.h>
11
15
16USERVER_NAMESPACE_BEGIN
17
18namespace logging {
19
20class LogHelper;
21
22} // namespace logging
23
24namespace formats::json {
25
26constexpr inline std::size_t kDepthParseLimit = 128;
27
29formats::json::Value FromString(std::string_view doc);
30
33
35void Serialize(const formats::json::Value& doc, std::ostream& os);
36
38std::string ToString(const formats::json::Value& doc);
39
42std::string ToStableString(const formats::json::Value& doc);
43
46
51
54
56struct PrettyFormat final {
57 char indent_char{' '};
58 std::size_t indent_char_count{2};
59};
60
62std::string ToPrettyString(const formats::json::Value& doc, PrettyFormat format = {});
63
66
68namespace blocking {
70formats::json::Value FromFile(const std::string& path);
71} // namespace blocking
72
73namespace impl {
74
75class StringBuffer final {
76public:
77 explicit StringBuffer(const formats::json::Value& value);
78 ~StringBuffer();
79
80 std::string_view GetStringView() const;
81
82private:
83 struct Impl;
84 static constexpr std::size_t kSize = 48;
85 static constexpr std::size_t kAlignment = 8;
87};
88
89} // namespace impl
90
91} // namespace formats::json
92
93USERVER_NAMESPACE_END
94
95template <>
96struct fmt::formatter<USERVER_NAMESPACE::formats::json::Value> : fmt::formatter<std::string_view> {
97 constexpr static auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
98
99 template <typename FormatContext>
100 auto format(const USERVER_NAMESPACE::formats::json::Value& value, FormatContext& ctx)
101 USERVER_FMT_CONST -> decltype(ctx.out()) {
102 const USERVER_NAMESPACE::formats::json::impl::StringBuffer buffer(value);
103 return fmt::format_to(ctx.out(), "{}", buffer.GetStringView());
104 }
105};