Couchbase C++ SDK
1.4.0 (rev. 59aa900)
Toggle main menu visibility
Loading...
Searching...
No Matches
raw_json_transcoder.hxx
Go to the documentation of this file.
1
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/*
3
* Copyright 2024. Couchbase, Inc.
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*/
17
18
#pragma once
19
20
#include <
couchbase/codec/codec_flags.hxx
>
21
#include <
couchbase/codec/encoded_value.hxx
>
22
#include <
couchbase/codec/transcoder_traits.hxx
>
23
#include <
couchbase/error_codes.hxx
>
24
25
#include <cstddef>
26
#include <string>
27
#include <string_view>
28
#include <type_traits>
29
#include <vector>
30
31
namespace
couchbase
32
{
33
#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
34
namespace
core::utils
35
{
36
auto
37
to_binary
(std::string_view value)
noexcept
-> std::vector<std::byte>;
38
}
// namespace core::utils
39
#endif
40
41
namespace
codec
42
{
43
class
raw_json_transcoder
44
{
45
public
:
46
template
<
47
typename
Document,
48
std::enable_if_t<std::is_same_v<Document, std::string> || std::is_same_v<Document, binary>,
49
bool
> =
true
>
50
static
auto
encode
(
const
Document& document) ->
encoded_value
51
{
52
if
constexpr
(std::is_same_v<Document, std::string>) {
53
return
{ core::utils::to_binary(document),
codec_flags::json_common_flags
};
54
}
else
{
55
return
{ std::move(document),
codec_flags::json_common_flags
};
56
}
57
}
58
59
template
<
60
typename
Document,
61
std::enable_if_t<std::is_same_v<Document, std::string> || std::is_same_v<Document, binary>,
62
bool
> =
true
>
63
static
auto
decode
(
const
encoded_value
& encoded) -> Document
64
{
65
if
(!
codec_flags::has_common_flags
(encoded.flags,
codec_flags::json_common_flags
)) {
66
throw
std::system_error(
67
errc::common::decoding_failure
,
68
"raw_json_transcoder expects document to have JSON common flags, flags="
+
69
std::to_string(encoded.flags));
70
}
71
if
constexpr
(std::is_same_v<Document, std::string>) {
72
return
std::string{
reinterpret_cast<
const
char
*
>
(encoded.data.data()), encoded.data.size() };
73
}
else
{
74
return
encoded.data;
75
}
76
}
77
};
78
79
#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
80
template
<>
81
struct
is_transcoder<raw_json_transcoder> :
public
std::true_type {
82
};
83
#endif
84
}
// namespace codec
85
}
// namespace couchbase
couchbase::codec::raw_json_transcoder
Definition
raw_json_transcoder.hxx:44
couchbase::codec::raw_json_transcoder::decode
static auto decode(const encoded_value &encoded) -> Document
Definition
raw_json_transcoder.hxx:63
couchbase::codec::raw_json_transcoder::encode
static auto encode(const Document &document) -> encoded_value
Definition
raw_json_transcoder.hxx:50
codec_flags.hxx
encoded_value.hxx
error_codes.hxx
couchbase::codec::codec_flags::has_common_flags
constexpr auto has_common_flags(std::uint32_t flags) -> bool
Checks whether the upper 8 bits are set, indicating common flags presence.
Definition
codec_flags.hxx:95
couchbase::codec::codec_flags::json_common_flags
constexpr std::uint32_t json_common_flags
Definition
codec_flags.hxx:145
couchbase::codec
Definition
analytics_options.hxx:36
couchbase::errc::common::decoding_failure
@ decoding_failure
Returned when decoding of the data into the user object failed.
Definition
error_codes.hxx:275
couchbase::subdoc::to_binary
auto to_binary(mutate_in_macro value) -> std::vector< std::byte >
Converts macro into binary array suitable for sending to the server.
couchbase
Represents a single item from the result of scan().
Definition
allow_querying_search_index_options.hxx:28
couchbase::codec::encoded_value
Definition
encoded_value.hxx:27
transcoder_traits.hxx
couchbase
codec
raw_json_transcoder.hxx
Generated by
1.17.0