From bbc5fb308bd7e862afa176acc97323d0282b0b5e Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Tue, 6 Aug 2024 17:48:02 +0200 Subject: [PATCH] use enum for json serialization in LogicalLinkControlPacket --- ...unique_ptr_logical_link_control_packet.hpp | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/include/nlohmann/std_unique_ptr_logical_link_control_packet.hpp b/include/nlohmann/std_unique_ptr_logical_link_control_packet.hpp index 3ba3e60..bf5fe40 100644 --- a/include/nlohmann/std_unique_ptr_logical_link_control_packet.hpp +++ b/include/nlohmann/std_unique_ptr_logical_link_control_packet.hpp @@ -17,6 +17,14 @@ static constexpr const int kPacketApiVersion = 0; +enum class PacketType { + kLogicalLinkControlPacket, + kMobileLinkEntityPacket, + kCircuitModeControlEntityPacket, + kMobileManagementPacket, + kShortDataServicePacket, +}; + namespace nlohmann { template <> struct adl_serializer> { static void to_json(json& j, const std::unique_ptr& packet) { @@ -26,25 +34,25 @@ template <> struct adl_serializer> { if (auto* cmce = dynamic_cast(mle)) { if (auto* sds = dynamic_cast(mle)) { // Emit ShortDataServicePacket packet to json - j["key"] = "ShortDataServicePacket"; + j["key"] = PacketType::kShortDataServicePacket; j["value"] = *sds; } else { // Emit CircuitModeControlEntityPacket packet to json - j["key"] = "CircuitModeControlEntityPacket"; + j["key"] = PacketType::kCircuitModeControlEntityPacket; j["value"] = *cmce; } } else if (auto* mm = dynamic_cast(mle)) { // Emit MobileManagementPacket packet to json - j["key"] = "MobileManagementPacket"; + j["key"] = PacketType::kMobileManagementPacket; j["value"] = *mm; } else { // Emit MobileLinkEntityPacket packet to json - j["key"] = "MobileLinkEntityPacket"; + j["key"] = PacketType::kMobileLinkEntityPacket; j["value"] = *mle; } } else { // Emit LogicalLinkControlPacket packet to json - j["key"] = "LogicalLinkControlPacket"; + j["key"] = PacketType::kLogicalLinkControlPacket; j["value"] = *packet; } } @@ -55,21 +63,20 @@ template <> struct adl_serializer> { throw std::runtime_error("Cannot process packets different API version."); } - auto key = j["key"].template get(); + auto key = j["key"].template get(); - if (key == "LogicalLinkControlPacket") { + switch (key) { + case PacketType::kLogicalLinkControlPacket: packet = std::make_unique(j["value"].template get()); - } else if (key == "MobileLinkEntityPacket") { + case PacketType::kMobileLinkEntityPacket: packet = std::make_unique(j["value"].template get()); - } else if (key == "MobileManagementPacket") { - packet = std::make_unique(j["value"].template get()); - } else if (key == "CircuitModeControlEntityPacket") { + case PacketType::kCircuitModeControlEntityPacket: packet = std::make_unique( j["value"].template get()); - } else if (key == "ShortDataServicePacket") { + case PacketType::kMobileManagementPacket: + packet = std::make_unique(j["value"].template get()); + case PacketType::kShortDataServicePacket: packet = std::make_unique(j["value"].template get()); - } else { - throw std::runtime_error("Unknown packet type: " + key); } } };