Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
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
24
25#include <cstddef>
26#include <string>
27#include <string_view>
28#include <type_traits>
29#include <vector>
30
31namespace couchbase
32{
33#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
34namespace core::utils
35{
36auto
37to_binary(std::string_view value) noexcept -> std::vector<std::byte>;
38} // namespace core::utils
39#endif
40
41namespace codec
42{
44{
45public:
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 {
66 throw std::system_error(
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
80template<>
81struct is_transcoder<raw_json_transcoder> : public std::true_type {
82};
83#endif
84} // namespace codec
85} // namespace couchbase
Definition raw_json_transcoder.hxx:44
static auto decode(const encoded_value &encoded) -> Document
Definition raw_json_transcoder.hxx:63
static auto encode(const Document &document) -> encoded_value
Definition raw_json_transcoder.hxx:50
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:96
constexpr std::uint32_t json_common_flags
Definition codec_flags.hxx:146
@ decoding_failure
Returned when decoding of the data into the user object failed.
auto to_binary(mutate_in_macro value) -> std::vector< std::byte >
Converts macro into binary array suitable for sending to the server.
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
Definition encoded_value.hxx:27