Cbeam
Loading...
Searching...
No Matches
traits.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/container/buffer.hpp> // for cbeam::container::buffer
30
31#include <cstdint> // for uint8_t
32#include <cstring> // for memcpy, std::memcpy
33
34#include <type_traits> // for std::is_standard_layout, std::is_trivial
35
36namespace cbeam::json
37{
38 static inline const char escape_character = '\\';
39 static inline const std::string characters_to_escape = "\\\"\r\n\t\f\b";
40
46 using serialized_object = void*;
47
54 template <typename T>
55 struct traits
56 {
57 static_assert(std::is_standard_layout<T>::value && std::is_trivial<T>::value,
58 "A specialization of cbeam::json::traits must be implemented for types that are not both standard-layout and trivial.");
59
67 static inline void serialize(const T& val, container::buffer& stream)
68 {
69 // implementation for types that are both standard-layout and trivial
70 stream.append("\"", 1);
71 const std::string str = convert::escape_string(convert::to_string(val), escape_character, characters_to_escape);
72 stream.append(str.c_str(), str.length());
73 stream.append("\"", 1);
74 }
75 };
76}
Manages memory a byte buffer, offering dynamic appending. This class is designed for scenarios where ...
Definition buffer.hpp:50
virtual void append(const void *buffer_to_append, const std::size_t length_of_buffer)
append the given buffer to the end of the current buffer. If there is no current buffer yet,...
Definition buffer.hpp:96
std::string to_string(const container::buffer &b)
Creates a std::string from the contents of a container::buffer.
Definition buffer.hpp:42
std::string escape_string(const std::string &input, const char escape_character, const std::string &characters_to_escape)
Definition string.hpp:97
Provides JSON-style and nested-map serialization features. It offers methods to convert a wide range ...
Definition map.hpp:37
void * serialized_object
Represents a serialized value in memory.
Definition traits.hpp:46
Defines the traits required for serializing and deserializing objects of type T.
Definition traits.hpp:56
static void serialize(const T &val, container::buffer &stream)
Required to serialize an object of type T into a shared_buffer stream.
Definition traits.hpp:67