Couchbase C++ SDK
1.4.0 (rev. 59aa900)
Toggle main menu visibility
Loading...
Searching...
No Matches
tao_json_serializer.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 2020-Present 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/encoded_value.hxx
>
21
#include <
couchbase/codec/serializer_traits.hxx
>
22
#include <
couchbase/error_codes.hxx
>
23
24
#include <tao/json/value.hpp>
25
26
namespace
couchbase
27
{
28
#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
29
namespace
core::utils::json
30
{
31
auto
32
generate_binary(
const
tao::json::value&
object
) -> std::vector<std::byte>;
33
34
auto
35
parse_binary(
const
std::vector<std::byte>& input) -> tao::json::value;
36
}
// namespace core::utils::json
37
#endif
38
39
namespace
codec
40
{
41
class
tao_json_serializer
42
{
43
public
:
44
using
document_type
= tao::json::value;
45
46
template
<
typename
Document>
47
static
auto
serialize
([[maybe_unused]] Document document) ->
binary
48
{
49
try
{
50
if
constexpr
(std::is_null_pointer_v<std::remove_reference_t<Document>>) {
51
return
core::utils::json::generate_binary(tao::json::null);
52
}
else
{
53
return
core::utils::json::generate_binary(tao::json::value(document));
54
}
55
}
catch
(
const
tao::pegtl::parse_error& e) {
56
throw
std::system_error(
57
errc::common::encoding_failure
,
58
std::string(
"json_transcoder cannot generate document as JSON: "
).append(e.message()));
59
}
60
}
61
62
template
<
typename
Document>
63
static
auto
deserialize
(
const
binary
& data) -> Document
64
{
65
try
{
66
if
constexpr
(std::is_same_v<Document, tao::json::value>) {
67
return
core::utils::json::parse_binary(data);
68
}
else
{
69
return
core::utils::json::parse_binary(data).as<Document>();
70
}
71
}
catch
(
const
tao::pegtl::parse_error& e) {
72
throw
std::system_error(
73
errc::common::decoding_failure
,
74
std::string(
"json_transcoder cannot parse document as JSON: "
).append(e.message()));
75
}
catch
(
const
std::out_of_range& e) {
76
throw
std::system_error(
77
errc::common::decoding_failure
,
78
std::string(
"json_transcoder cannot parse document: "
).append(e.what()));
79
}
80
}
81
};
82
83
#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
84
template
<>
85
struct
is_serializer<tao_json_serializer> :
public
std::true_type {
86
};
87
#endif
88
}
// namespace codec
89
}
// namespace couchbase
couchbase::codec::tao_json_serializer
Definition
tao_json_serializer.hxx:42
couchbase::codec::tao_json_serializer::document_type
tao::json::value document_type
Definition
tao_json_serializer.hxx:44
couchbase::codec::tao_json_serializer::serialize
static auto serialize(Document document) -> binary
Definition
tao_json_serializer.hxx:47
couchbase::codec::tao_json_serializer::deserialize
static auto deserialize(const binary &data) -> Document
Definition
tao_json_serializer.hxx:63
encoded_value.hxx
error_codes.hxx
couchbase::codec
Definition
analytics_options.hxx:36
couchbase::codec::binary
std::vector< std::byte > binary
Definition
encoded_value.hxx:25
couchbase::errc::common::encoding_failure
@ encoding_failure
Returned when encoding of a user object failed while trying to write it to the cluster.
Definition
error_codes.hxx:267
couchbase::errc::common::decoding_failure
@ decoding_failure
Returned when decoding of the data into the user object failed.
Definition
error_codes.hxx:275
couchbase
Represents a single item from the result of scan().
Definition
allow_querying_search_index_options.hxx:28
serializer_traits.hxx
couchbase
codec
tao_json_serializer.hxx
Generated by
1.17.0