userver: userver/storages/postgres/io/json_types.hpp Source File
Loading...
Searching...
No Matches
json_types.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/json_types.hpp
4/// @brief JSON I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <type_traits>
8
9#include <userver/storages/postgres/exceptions.hpp>
10#include <userver/storages/postgres/io/buffer_io_base.hpp>
11#include <userver/storages/postgres/io/field_buffer.hpp>
12#include <userver/storages/postgres/io/traits.hpp>
13#include <userver/storages/postgres/io/type_mapping.hpp>
14#include <userver/storages/postgres/io/type_traits.hpp>
15#include <userver/storages/postgres/io/user_types.hpp>
16
17#include <userver/formats/json.hpp>
18#include <userver/utils/strong_typedef.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace storages::postgres {
23
24using PlainJson = USERVER_NAMESPACE::utils::StrongTypedef<
25 struct PlainJsonTag,
27 USERVER_NAMESPACE::utils::StrongTypedefOps::kCompareTransparent>;
28
29namespace io {
30namespace detail {
31
32inline constexpr char kJsonbVersion = 1;
33
34struct JsonParser : BufferParserBase<formats::json::Value> {
35 using BaseType = BufferParserBase<formats::json::Value>;
36 using BaseType::BaseType;
37
38 void operator()(const FieldBuffer& buffer);
39};
40
41void JsonValueToBuffer(const formats::json::Value& value, std::vector<char>& buffer);
42
43void JsonValueToBuffer(const formats::json::Value& value, std::string& buffer);
44
45template <typename JsonValue>
46struct JsonFormatter : BufferFormatterBase<JsonValue> {
47 using BaseType = BufferFormatterBase<JsonValue>;
48 using BaseType::BaseType;
49
50 template <typename Buffer>
51 void operator()(const UserTypes&, Buffer& buffer) const {
52 if constexpr (!std::is_same_v<PlainJson, JsonValue>) {
53 buffer.push_back(kJsonbVersion);
54 }
55
56 detail::JsonValueToBuffer(static_cast<const formats::json::Value&>(this->value), buffer);
57 }
58};
59
60} // namespace detail
61
62namespace traits {
63
64template <>
66 using type = io::detail::JsonParser;
67};
68
69template <>
70struct ParserBufferCategory<io::detail::JsonParser>
72
73template <>
76};
77
78template <>
81};
82
83} // namespace traits
84
85template <>
86struct CppToSystemPg<formats::json::Value> : PredefinedOid<PredefinedOids::kJsonb> {};
87
88template <>
89struct CppToSystemPg<PlainJson> : PredefinedOid<PredefinedOids::kJson> {};
90
91} // namespace io
92} // namespace storages::postgres
93
94USERVER_NAMESPACE_END