Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
json_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 "json_serializer.h"
6#include "../container.h"
9
10namespace kcenon::container
11{
12
13#if CONTAINER_HAS_RESULT
14kcenon::common::Result<std::vector<uint8_t>>
15json_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 JSON serialization",
28 "json_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("JSON serialization failed: ") + e.what(),
36 "json_serializer"});
37 }
38}
39
40std::string json_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), "{{");
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\":\"{}\",",
60 variant_helpers::json_escape(target_id));
61 utility_module::formatter::format_to(std::back_inserter(result),
62 "\"target_sub_id\":\"{}\",",
63 variant_helpers::json_escape(target_sub_id));
64 utility_module::formatter::format_to(std::back_inserter(result),
65 "\"source_id\":\"{}\",",
66 variant_helpers::json_escape(source_id));
67 utility_module::formatter::format_to(std::back_inserter(result),
68 "\"source_sub_id\":\"{}\",",
69 variant_helpers::json_escape(source_sub_id));
70 }
71 utility_module::formatter::format_to(std::back_inserter(result),
72 "\"message_type\":\"{}\"",
73 variant_helpers::json_escape(message_type));
74 utility_module::formatter::format_to(std::back_inserter(result), ",\"version\":\"{}\"",
75 variant_helpers::json_escape(version));
76 utility_module::formatter::format_to(std::back_inserter(result), "}},"); // end header
77
78 // Values section
79 utility_module::formatter::format_to(std::back_inserter(result), "\"values\":{{");
80 bool first = true;
81 for (const auto& u : values)
82 {
83 if (!first)
84 {
85 utility_module::formatter::format_to(std::back_inserter(result), ",");
86 }
87 std::string value_str = variant_helpers::to_string(u.data, u.type);
88 std::string escaped_name = variant_helpers::json_escape(u.name);
89
90 // String and bytes values need quotes
91 if (u.type == value_types::string_value || u.type == value_types::bytes_value)
92 {
93 std::string escaped_value = variant_helpers::json_escape(value_str);
94 utility_module::formatter::format_to(std::back_inserter(result), "\"{}\":\"{}\"",
95 escaped_name, escaped_value);
96 }
97 else
98 {
99 utility_module::formatter::format_to(std::back_inserter(result), "\"{}\":{}",
100 escaped_name, value_str);
101 }
102 first = false;
103 }
104 utility_module::formatter::format_to(std::back_inserter(result), "}}"); // end values
105 utility_module::formatter::format_to(std::back_inserter(result), "}}");
106
107 return result;
108}
109#endif
110
111} // namespace kcenon::container
static void format_to(OutputIt out, const std::string &format_str, Args &&... args)
Definition formatter.h:62