Cbeam
Loading...
Searching...
No Matches
nested_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
29#include <cbeam/json/traits.hpp> // for cbeam::json::traits, cbeam::json::serialized_object
30
31namespace cbeam::container
32{
33 class buffer;
34}
35
36namespace cbeam::json
37{
47 template <typename Map>
49 {
56 static void serialize(const Map& map, container::buffer& stream)
57 {
58 // We assume Map = cbeam::container::nested_map<Key, Value>.
59 using Key = typename Map::key_type;
60 using Value = typename Map::mapped_type;
61
62 // Start JSON object
63 stream.append("{", 1);
64
65 bool first = true;
66
67 // 1) Serialize all entries in map.data
68 for (const auto& [k, v] : map.data)
69 {
70 if (!first)
71 {
72 stream.append(",", 1);
73 }
74 else
75 {
76 first = false;
77 }
78
79 // Key
80 traits<Key>::serialize(k, stream);
81 stream.append(":", 1);
82
83 // Value
84 traits<Value>::serialize(v, stream);
85 }
86
87 // 2) Serialize all entries in map.sub_tables
88 for (const auto& [subkey, submap] : map.sub_tables)
89 {
90 if (!first)
91 {
92 stream.append(",", 1);
93 }
94 else
95 {
96 first = false;
97 }
98
99 // Key
100 traits<Key>::serialize(subkey, stream);
101 stream.append(":", 1);
102
103 // Recursively serialize
104 nested_map_serializer<decltype(submap)>::serialize(submap, stream);
105 }
106
107 // End JSON object
108 stream.append("}", 1);
109 }
110 };
111
118 template <typename Key, typename Value>
127
128 template <typename Key, typename Value>
137}
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
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
Provides JSON-style and nested-map serialization features. It offers methods to convert a wide range ...
Definition map.hpp:37
The root namespace for the Cbeam library. This namespace unifies cross-platform utilities for concurr...
Definition message_manager.hpp:47
A map structure that can store nested maps of keys and values. By including serialization/nested_map....
Definition nested_map.hpp:44
Generic serializer for nested_map<Key, Value> types.
Definition nested_map.hpp:49
static void serialize(const Map &map, container::buffer &stream)
Serializes a nested map to JSON-like text, appending it to a buffer.
Definition nested_map.hpp:56
static void serialize(const cbeam::container::nested_map< Key, Value > &map, container::buffer &stream)
Definition nested_map.hpp:121
static void serialize(const cbeam::container::nested_map< Key, Value > &map, container::buffer &stream)
Definition nested_map.hpp:131
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