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, formats::parse::To<formats::json::Value>);
25formats::bson::Value Convert(const formats::json::Value& json, formats::parse::To<formats::bson::Value>);
26
27} // namespace formats::parse
28
29namespace formats::bson {
30
31/// Wraps To*JsonString results to avoid unneeded copying
32class 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.
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.
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
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
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.
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.
72
73namespace impl {
74class JsonStringImpl;
75} // namespace impl
76
78public:
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
92
93 const char* Data() const;
94 size_t Size() const;
95
96private:
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
104std::ostream& operator<<(std::ostream&, const JsonString&);
105
106logging::LogHelper& operator<<(logging::LogHelper&, const JsonString&);
107
108/// Uses formats::bson::ToRelaxedJsonString representation by default.
109logging::LogHelper& operator<<(logging::LogHelper&, const Document&);
110
111} // namespace formats::bson
112
113USERVER_NAMESPACE_END
114
115template <>
116struct 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.
124template <>
125struct 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};