userver
C++ Async Framework
Toggle main menu visibility
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
16
USERVER_NAMESPACE_BEGIN
17
18
namespace
logging
{
19
20
class
LogHelper;
21
22
}
// namespace logging
23
24
namespace
formats::
json
{
25
26
constexpr
inline
std::size_t kDepthParseLimit = 128;
27
28
/// Parse JSON from string
29
formats::
json
::
Value
FromString
(std::string_view doc);
30
31
/// Parse JSON from stream
32
formats::
json
::
Value
FromStream
(std::istream& is);
33
34
/// Serialize JSON to stream
35
void
Serialize
(
const
formats::
json
::
Value
& doc, std::ostream& os);
36
37
/// Serialize JSON to string
38
std::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
42
std::string
ToStableString
(
const
formats::
json
::
Value
& doc);
43
44
/// @overload
45
std::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.
50
std::string
ToPythonCompatibleStableString
(
const
formats::
json
::
Value
& doc);
51
52
/// @overload
53
std::string
ToPythonCompatibleStableString
(formats::
json
::
Value
&& doc);
54
55
/// @see formats::json::ToPrettyString
56
struct
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.
62
std::string
ToPrettyString
(
const
formats::
json
::
Value
& doc, PrettyFormat format = {});
63
64
/// Log JSON
65
logging
::LogHelper& operator<<(
logging
::LogHelper&,
const
formats::
json
::
Value
&);
66
67
/// @brief Blocking operations that should not be used on main task processor after startup
68
namespace
blocking
{
69
/// Read JSON from file
70
formats::
json
::
Value
FromFile
(
const
std::string& path);
71
}
// namespace blocking
72
73
namespace
impl {
74
75
class
StringBuffer
final
{
76
public
:
77
explicit
StringBuffer(
const
formats::
json
::
Value
& value);
78
~StringBuffer();
79
80
std::string_view GetStringView()
const
;
81
82
private
:
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
93
USERVER_NAMESPACE_END
94
95
template
<>
96
struct
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
};
userver
formats
json
serialize.hpp
Generated on
for userver by
Doxygen
1.17.0