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
3/// @file userver/formats/json/serialize.hpp
4/// @brief Parsers and serializers to/from string and stream
5
6#include <iosfwd>
7#include <string>
8#include <string_view>
9
10#include <fmt/format.h>
11
12#include <userver/formats/json/value.hpp>
13#include <userver/utils/fast_pimpl.hpp>
14#include <userver/utils/fmt_compat.hpp>
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
28/// Parse JSON from string
29formats::json::Value FromString(std::string_view doc);
30
31/// Parse JSON from stream
32formats::json::Value FromStream(std::istream& is);
33
34/// Serialize JSON to stream
35void Serialize(const formats::json::Value& doc, std::ostream& os);
36
37/// Serialize JSON to string
38std::string ToString(const formats::json::Value& doc);
39
40/// Stably serialize JSON to string. In result there is no whitespace, keys
41/// are sorted and character escaping is stabilized
42std::string ToStableString(const formats::json::Value& doc);
43
44/// @overload
45std::string ToStableString(formats::json::Value&& doc);
46
47/// Stably serialize JSON to string using the same representation as Python's
48/// `json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False)`.
49/// The input must contain valid UTF-8 strings and finite numbers.
50std::string ToPythonCompatibleStableString(const formats::json::Value& doc);
51
52/// @overload
53std::string ToPythonCompatibleStableString(formats::json::Value&& doc);
54
55/// @see formats::json::ToPrettyString
56struct PrettyFormat final {
57 char indent_char{' '};
58 std::size_t indent_char_count{2};
59};
60
61/// Serialize JSON to a string, using `\n` and indents for objects and arrays.
62std::string ToPrettyString(const formats::json::Value& doc, PrettyFormat format = {});
63
64/// Log JSON
65logging::LogHelper& operator<<(logging::LogHelper&, const formats::json::Value&);
66
67/// @brief Blocking operations that should not be used on main task processor after startup
68namespace blocking {
69/// Read JSON from file
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;
86 utils::FastPimpl<Impl, kSize, kAlignment> pimpl_;
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};