Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
xml_serializer.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#include "xml_serializer.h"
6#include "../container.h"
9
10namespace kcenon::container
11{
12
13#if CONTAINER_HAS_RESULT
14kcenon::common::Result<std::vector<uint8_t>>
15xml_serializer::serialize(const value_container& container) const noexcept
16{
17 try
18 {
19 auto str = serialize_to_string(container);
20 return kcenon::common::ok(std::vector<uint8_t>(str.begin(), str.end()));
21 }
22 catch (const std::bad_alloc&)
23 {
24 return kcenon::common::Result<std::vector<uint8_t>>(
25 kcenon::common::error_info{
26 -2,
27 "Memory allocation failed during XML serialization",
28 "xml_serializer"});
29 }
30 catch (const std::exception& e)
31 {
32 return kcenon::common::Result<std::vector<uint8_t>>(
33 kcenon::common::error_info{
34 -1,
35 std::string("XML serialization failed: ") + e.what(),
36 "xml_serializer"});
37 }
38}
39
40std::string xml_serializer::serialize_to_string(const value_container& container) const
41{
42 // Get container data through public accessors
43 auto message_type = container.message_type();
44 auto target_id = container.target_id();
45 auto target_sub_id = container.target_sub_id();
46 auto source_id = container.source_id();
47 auto source_sub_id = container.source_sub_id();
48 auto version = container.version();
49 auto values = container.get_variant_values();
50
51 std::string result;
52 utility_module::formatter::format_to(std::back_inserter(result), "<container>");
53
54 // Header section
55 utility_module::formatter::format_to(std::back_inserter(result), "<header>");
56 if (message_type != "data_container")
57 {
58 utility_module::formatter::format_to(std::back_inserter(result),
59 "<target_id>{}</target_id>",
60 variant_helpers::xml_encode(target_id));
61 utility_module::formatter::format_to(std::back_inserter(result),
62 "<target_sub_id>{}</target_sub_id>",
63 variant_helpers::xml_encode(target_sub_id));
64 utility_module::formatter::format_to(std::back_inserter(result),
65 "<source_id>{}</source_id>",
66 variant_helpers::xml_encode(source_id));
67 utility_module::formatter::format_to(std::back_inserter(result),
68 "<source_sub_id>{}</source_sub_id>",
69 variant_helpers::xml_encode(source_sub_id));
70 }
71 utility_module::formatter::format_to(std::back_inserter(result),
72 "<message_type>{}</message_type>",
73 variant_helpers::xml_encode(message_type));
74 utility_module::formatter::format_to(std::back_inserter(result),
75 "<version>{}</version>",
76 variant_helpers::xml_encode(version));
77 utility_module::formatter::format_to(std::back_inserter(result), "</header>");
78
79 // Values section
80 utility_module::formatter::format_to(std::back_inserter(result), "<values>");
81 for (const auto& u : values)
82 {
83 std::string value_str = variant_helpers::to_string(u.data, u.type);
84 utility_module::formatter::format_to(std::back_inserter(result), "<{}>{}</{}>",
85 u.name, variant_helpers::xml_encode(value_str),
86 u.name);
87 }
88 utility_module::formatter::format_to(std::back_inserter(result), "</values>");
89 utility_module::formatter::format_to(std::back_inserter(result), "</container>");
90
91 return result;
92}
93#endif
94
95} // namespace kcenon::container
static void format_to(OutputIt out, const std::string &format_str, Args &&... args)
Definition formatter.h:62