13#if CONTAINER_HAS_RESULT
14kcenon::common::Result<std::vector<uint8_t>>
15json_serializer::serialize(
const value_container& container)
const noexcept
19 auto str = serialize_to_string(container);
20 return kcenon::common::ok(std::vector<uint8_t>(str.begin(), str.end()));
22 catch (
const std::bad_alloc&)
24 return kcenon::common::Result<std::vector<uint8_t>>(
25 kcenon::common::error_info{
27 "Memory allocation failed during JSON serialization",
30 catch (
const std::exception& e)
32 return kcenon::common::Result<std::vector<uint8_t>>(
33 kcenon::common::error_info{
35 std::string(
"JSON serialization failed: ") + e.what(),
40std::string json_serializer::serialize_to_string(
const value_container& container)
const
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();
56 if (message_type !=
"data_container")
59 "\"target_id\":\"{}\",",
60 variant_helpers::json_escape(target_id));
62 "\"target_sub_id\":\"{}\",",
63 variant_helpers::json_escape(target_sub_id));
65 "\"source_id\":\"{}\",",
66 variant_helpers::json_escape(source_id));
68 "\"source_sub_id\":\"{}\",",
69 variant_helpers::json_escape(source_sub_id));
72 "\"message_type\":\"{}\"",
73 variant_helpers::json_escape(message_type));
75 variant_helpers::json_escape(version));
81 for (
const auto& u : values)
87 std::string value_str = variant_helpers::to_string(u.data, u.type);
88 std::string escaped_name = variant_helpers::json_escape(u.name);
91 if (u.type == value_types::string_value || u.type == value_types::bytes_value)
93 std::string escaped_value = variant_helpers::json_escape(value_str);
95 escaped_name, escaped_value);
100 escaped_name, value_str);