userver
C++ Async Framework
Loading...
Searching...
No Matches
serialize.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/formats/bson/serialize.hpp
4
/// @brief Textual serialization helpers
5
6
#
include
<
iosfwd
>
7
#
include
<
string
>
8
#
include
<
string_view
>
9
10
#
include
<
fmt
/
core
.
h
>
11
12
#
include
<
userver
/
compiler
/
select
.
hpp
>
13
#
include
<
userver
/
formats
/
bson
/
document
.
hpp
>
14
#
include
<
userver
/
formats
/
bson
/
value
.
hpp
>
15
#
include
<
userver
/
formats
/
json_fwd
.
hpp
>
16
#
include
<
userver
/
logging
/
log_helper_fwd
.
hpp
>
17
#
include
<
userver
/
utils
/
fast_pimpl
.
hpp
>
18
#
include
<
userver
/
utils
/
fmt_compat
.
hpp
>
19
20
USERVER_NAMESPACE_BEGIN
21
22
namespace
formats
::
parse
{
23
24
formats
::
json
::Value Convert(
const
formats
::
bson
::
Value
& bson,
formats
::
parse
::
To
<
formats
::
json
::Value>);
25
formats
::
bson
::
Value
Convert(
const
formats
::
json
::Value& json,
formats
::
parse
::
To
<
formats
::
bson
::
Value
>);
26
27
}
// namespace formats::parse
28
29
namespace
formats
::
bson
{
30
31
/// Wraps To*JsonString results to avoid unneeded copying
32
class
JsonString
;
33
34
/// Applies heuristics to convert JSON string to BSON document.
35
/// As JSON have rather lax typing, some heuristics are applied to guess correct
36
/// BSON types for values. It is strongly recommended to write your own
37
/// conversion routines matching your schemas.
38
/// @warning Stability of heuristics is not guaranteed, this is provided as-is.
39
Document
FromJsonString
(std::string_view json);
40
41
/// Applies heuristics to convert JSON string to BSON array.
42
/// As JSON have rather lax typing, some heuristics are applied to guess correct
43
/// BSON types for values. It is strongly recommended to write your own
44
/// conversion routines matching your schemas.
45
/// @warning Stability of heuristics is not guaranteed, this is provided as-is.
46
Value
ArrayFromJsonString
(std::string_view json);
47
48
/// Converts BSON to a canonical MongoDB Extended JSON format.
49
/// @see
50
/// https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
51
JsonString
ToCanonicalJsonString
(
const
formats
::
bson
::
Document
&);
52
53
/// Converts BSON to a relaxed MongoDB Extended JSON format.
54
/// Notable differences from canonical format are:
55
/// * numbers are not being wrapped in `$number*` objects;
56
/// * dates have string representation.
57
/// @see
58
/// https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
59
JsonString
ToRelaxedJsonString
(
const
formats
::
bson
::
Document
&);
60
61
/// Converts BSON to a legacy libbson's JSON format.
62
/// Notable differences from other formats:
63
/// * all numbers are not wrapped;
64
/// * non-standard tokens for special floating point values;
65
/// * dates are milliseconds since epoch;
66
/// * different format for binaries.
67
JsonString
ToLegacyJsonString
(
const
formats
::
bson
::
Document
&);
68
69
/// Converts BSON array to a legacy libbson's JSON format, except the outermost
70
/// element is encoded as a JSON array, rather than a JSON document.
71
JsonString
ToArrayJsonString
(
const
formats
::
bson
::
Value
&);
72
73
namespace
impl {
74
class
JsonStringImpl;
75
}
// namespace impl
76
77
class
JsonString
{
78
public
:
79
/// @cond
80
explicit
JsonString(impl::JsonStringImpl&&);
81
/// @endcond
82
~JsonString();
83
84
/// Implicitly convertible to string
85
/*implicit*/
operator
std::string()
const
{
return
ToString
(
)
; }
86
87
/// Returns a copy of the string
88
std::string
ToString
()
const
;
89
90
/// Returns a view of the string
91
std::string_view
GetView
()
const
;
92
93
const
char
* Data()
const
;
94
size_t Size()
const
;
95
96
private
:
97
static
constexpr
std::size_t kSize =
compiler
::SelectSize()
//
98
.For64Bit(16)
99
.For32Bit(8);
100
static
constexpr
std::size_t kAlignment =
alignof
(
void
*);
101
utils
::FastPimpl<impl::JsonStringImpl, kSize, kAlignment,
utils
::
kStrictMatch
> impl_;
102
};
103
104
std::ostream& operator<<(std::ostream&,
const
JsonString
&);
105
106
logging
::LogHelper& operator<<(
logging
::LogHelper&,
const
JsonString
&);
107
108
/// Uses formats::bson::ToRelaxedJsonString representation by default.
109
logging
::LogHelper& operator<<(
logging
::LogHelper&,
const
Document
&);
110
111
}
// namespace formats::bson
112
113
USERVER_NAMESPACE_END
114
115
template
<>
116
struct
fmt::formatter<USERVER_NAMESPACE::
formats
::
bson
::
JsonString
> :
public
fmt::formatter<std::string_view> {
117
template
<
typename
FormatContext>
118
auto
format(
const
USERVER_NAMESPACE::
formats
::
bson
::
JsonString
& json, FormatContext& ctx)
USERVER_FMT_CONST
{
119
return
fmt::formatter<std::string_view>::format(json
.
GetView
(
)
, ctx);
120
}
121
};
122
123
/// Uses formats::bson::ToRelaxedJsonString representation by default.
124
template
<>
125
struct
fmt::formatter<USERVER_NAMESPACE::
formats
::
bson
::
Document
> :
public
fmt::formatter<std::string_view> {
126
template
<
typename
FormatContext>
127
auto
format(
const
USERVER_NAMESPACE::
formats
::
bson
::
Document
& bson, FormatContext& ctx)
USERVER_FMT_CONST
{
128
return
fmt::formatter<std::string_view>::format(
129
USERVER_NAMESPACE
::
formats
::
bson
::
ToRelaxedJsonString
(
bson
)
.
GetView
(
)
, ctx
130
);
131
}
132
};
userver
formats
bson
serialize.hpp
Generated on Fri Apr 11 2025 14:25:06 for userver by
Doxygen
1.13.2