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, formats::json::Value,
26 USERVER_NAMESPACE::utils::StrongTypedefOps::kCompareTransparent>;
27
28namespace io {
29namespace detail {
30
31inline constexpr char kJsonbVersion = 1;
32
33struct JsonParser : BufferParserBase<formats::json::Value> {
34 using BaseType = BufferParserBase<formats::json::Value>;
35 using BaseType::BaseType;
36
37 void operator()(const FieldBuffer& buffer);
38};
39
40void JsonValueToBuffer(const formats::json::Value& value,
41 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(
57 static_cast<const formats::json::Value&>(this->value), buffer);
58 }
59};
60
61} // namespace detail
62
63namespace traits {
64
65template <>
67 using type = io::detail::JsonParser;
68};
69
70template <>
71struct ParserBufferCategory<io::detail::JsonParser>
73
74template <>
77};
78
79template <>
82};
83
84} // namespace traits
85
86template <>
87struct CppToSystemPg<formats::json::Value>
88 : PredefinedOid<PredefinedOids::kJsonb> {};
89
90template <>
92
93} // namespace io
94} // namespace storages::postgres
95
96USERVER_NAMESPACE_END