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
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
20USERVER_NAMESPACE_BEGIN
21
22namespace formats::parse {
23
24formats::json::Value Convert(const formats::bson::Value& bson,
25 formats::parse::To<formats::json::Value>);
26formats::bson::Value Convert(const formats::json::Value& json,
27 formats::parse::To<formats::bson::Value>);
28
29} // namespace formats::parse
30
31namespace formats::bson {
32
33/// Wraps To*JsonString results to avoid unneeded copying
34class JsonString;
35
36/// Applies heuristics to convert JSON string to BSON document.
37/// As JSON have rather lax typing, some heuristics are applied to guess correct
38/// BSON types for values. It is strongly recommended to write your own
39/// conversion routines matching your schemas.
40/// @warning Stability of heuristics is not guaranteed, this is provided as-is.
42
43/// Applies heuristics to convert JSON string to BSON array.
44/// As JSON have rather lax typing, some heuristics are applied to guess correct
45/// BSON types for values. It is strongly recommended to write your own
46/// conversion routines matching your schemas.
47/// @warning Stability of heuristics is not guaranteed, this is provided as-is.
49
50/// Converts BSON to a canonical MongoDB Extended JSON format.
51/// @see
52/// https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
54
55/// Converts BSON to a relaxed MongoDB Extended JSON format.
56/// Notable differences from canonical format are:
57/// * numbers are not being wrapped in `$number*` objects;
58/// * dates have string representation.
59/// @see
60/// https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
62
63/// Converts BSON to a legacy libbson's JSON format.
64/// Notable differences from other formats:
65/// * all numbers are not wrapped;
66/// * non-standard tokens for special floating point values;
67/// * dates are milliseconds since epoch;
68/// * different format for binaries.
70
71/// Converts BSON array to a legacy libbson's JSON format, except the outermost
72/// element is encoded as a JSON array, rather than a JSON document.
74
75namespace impl {
76class JsonStringImpl;
77} // namespace impl
78
80 public:
81 /// @cond
82 explicit JsonString(impl::JsonStringImpl&&);
83 /// @endcond
84 ~JsonString();
85
86 /// Implicitly convertible to string
87 /*implicit*/ operator std::string() const { return ToString(); }
88
89 /// Returns a copy of the string
90 std::string ToString() const;
91
92 /// Returns a view of the string
94
95 const char* Data() const;
96 size_t Size() const;
97
98 private:
99 static constexpr std::size_t kSize = compiler::SelectSize() //
100 .For64Bit(16)
101 .For32Bit(8);
102 static constexpr std::size_t kAlignment = alignof(void*);
103 utils::FastPimpl<impl::JsonStringImpl, kSize, kAlignment, utils::kStrictMatch>
104 impl_;
105};
106
107std::ostream& operator<<(std::ostream&, const JsonString&);
108
109logging::LogHelper& operator<<(logging::LogHelper&, const JsonString&);
110
111/// Uses formats::bson::ToRelaxedJsonString representation by default.
112logging::LogHelper& operator<<(logging::LogHelper&, const Document&);
113
114} // namespace formats::bson
115
116USERVER_NAMESPACE_END
117
118template <>
119struct fmt::formatter<USERVER_NAMESPACE::formats::bson::JsonString>
120 : public fmt::formatter<std::string_view> {
121 template <typename FormatContext>
122 auto format(const USERVER_NAMESPACE::formats::bson::JsonString& json,
123 FormatContext& ctx) USERVER_FMT_CONST {
124 return fmt::formatter<std::string_view>::format(json.GetView(), ctx);
125 }
126};
127
128/// Uses formats::bson::ToRelaxedJsonString representation by default.
129template <>
130struct fmt::formatter<USERVER_NAMESPACE::formats::bson::Document>
131 : public fmt::formatter<std::string_view> {
132 template <typename FormatContext>
133 auto format(const USERVER_NAMESPACE::formats::bson::Document& bson,
134 FormatContext& ctx) USERVER_FMT_CONST {
135 return fmt::formatter<std::string_view>::format(
136 USERVER_NAMESPACE::formats::bson::ToRelaxedJsonString(bson).GetView(),
137 ctx);
138 }
139};