Cbeam
Loading...
Searching...
No Matches
map.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 buffer (ptr only), cbeam::container::buffer
29#include <cbeam/json/traits.hpp> // for cbeam::serialization::traits, cbeam::serialization::serialized_object
30
31#include <cstdint> // for uint8_t
32#include <cstring> // for memcpy, size_t, std::memcpy
33
34#include <map> // for std::map
35
36namespace cbeam::json
37{
46 template <typename Map>
48 {
57 static void serialize(const Map& map, container::buffer& stream)
58 {
59 bool first = true;
60
61 for (const auto& pair : map)
62 {
63 if (first)
64 {
65 first = false;
66 }
67 else
68 {
69 stream.append(",", 1);
70 }
71
73 stream.append(":", 1);
75 }
76 }
77 };
78
85 template <typename Key, typename Value>
86 struct traits<std::map<Key, Value>>
87 {
88 static inline void serialize(const std::map<Key, Value>& map, container::buffer& stream)
89 {
90 stream.append("{", 1);
92 stream.append("}", 1);
93 }
94 };
95
96 template <typename Key, typename Value>
97 struct traits<const std::map<Key, Value>>
98 {
99 static inline void serialize(const std::map<Key, Value>& map, container::buffer& stream)
100 {
101 stream.append("{", 1);
103 stream.append("}", 1);
104 }
105 };
106}
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
Provides JSON-style and nested-map serialization features. It offers methods to convert a wide range ...
Definition map.hpp:37
Provides JSON-style serialization logic for any map type Map.
Definition map.hpp:48
static void serialize(const Map &map, container::buffer &stream)
Serializes a map to a JSON-like representation, appending the result to a buffer.
Definition map.hpp:57
static void serialize(const std::map< Key, Value > &map, container::buffer &stream)
Definition map.hpp:99
static void serialize(const std::map< Key, Value > &map, container::buffer &stream)
Definition map.hpp:88
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