Couchbase C++ SDK 1.0.4 (rev. 5355b0f)
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
23
24#include <tao/json/value.hpp>
25
26namespace couchbase
27{
28#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
29namespace core::utils::json
30{
31auto
32generate_binary(const tao::json::value& object) -> std::vector<std::byte>;
33
34auto
35parse_binary(const std::vector<std::byte>& input) -> tao::json::value;
36} // namespace core::utils::json
37#endif
38
39namespace codec
40{
42{
43public:
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(
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(
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(
78 std::string("json_transcoder cannot parse document: ").append(e.what()));
79 }
80 }
81};
82
83#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
84template<>
85struct is_serializer<tao_json_serializer> : public std::true_type {
86};
87#endif
88} // namespace codec
89} // namespace couchbase
Definition tao_json_serializer.hxx:42
tao::json::value document_type
Definition tao_json_serializer.hxx:44
static auto serialize(Document document) -> binary
Definition tao_json_serializer.hxx:47
static auto deserialize(const binary &data) -> Document
Definition tao_json_serializer.hxx:63
std::vector< std::byte > binary
Definition encoded_value.hxx:25
@ encoding_failure
Returned when encoding of a user object failed while trying to write it to the cluster.
@ decoding_failure
Returned when decoding of the data into the user object failed.
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28