-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor borzoi converter into nlohmann json function
- Loading branch information
1 parent
66d8049
commit 2042a52
Showing
4 changed files
with
87 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
include/nlohmann_std_unique_ptr_logical_link_control_packet.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2024 Transit Live Mapping Solutions | ||
* All rights reserved. | ||
* | ||
* Authors: | ||
* Marenz Schmidl | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "l2/logical_link_control_packet.hpp" | ||
#include "l3/circuit_mode_control_entity_packet.hpp" | ||
#include "l3/mobile_link_entity_packet.hpp" | ||
#include "l3/mobile_management_packet.hpp" | ||
#include "l3/short_data_service_packet.hpp" | ||
#include <nlohmann/json.hpp> | ||
|
||
static constexpr const int kPacketApiVersion = 0; | ||
|
||
inline static auto get_time() -> std::string { | ||
auto t = std::time(nullptr); | ||
auto tm = *std::localtime(&t); | ||
std::stringstream ss; | ||
ss << std::put_time(&tm, "%FT%T%z"); | ||
return ss.str(); | ||
} | ||
|
||
namespace nlohmann { | ||
template <> struct adl_serializer<std::unique_ptr<LogicalLinkControlPacket>> { | ||
static void to_json(json& j, const std::unique_ptr<LogicalLinkControlPacket>& packet) { | ||
j["protocol_version"] = kPacketApiVersion; | ||
j["time"] = get_time(); | ||
|
||
if (auto* mle = dynamic_cast<MobileLinkEntityPacket*>(packet.get())) { | ||
if (auto* cmce = dynamic_cast<CircuitModeControlEntityPacket*>(mle)) { | ||
if (auto* sds = dynamic_cast<ShortDataServicePacket*>(mle)) { | ||
// Emit ShortDataServicePacket packet to json | ||
j["key"] = "ShortDataServicePacket"; | ||
j["value"] = *sds; | ||
} else { | ||
// Emit CircuitModeControlEntityPacket packet to json | ||
j["key"] = "CircuitModeControlEntityPacket"; | ||
j["value"] = *cmce; | ||
} | ||
} else if (auto* mm = dynamic_cast<MobileManagementPacket*>(mle)) { | ||
// Emit MobileManagementPacket packet to json | ||
j["key"] = "MobileManagementPacket"; | ||
j["value"] = *mm; | ||
} else { | ||
// Emit MobileLinkEntityPacket packet to json | ||
j["key"] = "MobileLinkEntityPacket"; | ||
j["value"] = *mle; | ||
} | ||
} else { | ||
// Emit LogicalLinkControlPacket packet to json | ||
j["key"] = "LogicalLinkControlPacket"; | ||
j["value"] = *packet; | ||
} | ||
} | ||
|
||
static void from_json(const json& j, std::unique_ptr<LogicalLinkControlPacket>& packet) { | ||
auto protocol_version = j["protocol_version"].template get<int>(); | ||
if (protocol_version != kPacketApiVersion) { | ||
throw std::runtime_error("Cannot process packets different API version."); | ||
} | ||
|
||
auto key = j["key"].template get<std::string>(); | ||
|
||
if (key == "LogicalLinkControlPacket") { | ||
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<LogicalLinkControlPacket>()); | ||
} else if (key == "MobileLinkEntityPacket") { | ||
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<MobileLinkEntityPacket>()); | ||
} else if (key == "MobileManagementPacket") { | ||
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<MobileManagementPacket>()); | ||
} else if (key == "CircuitModeControlEntityPacket") { | ||
packet = | ||
std::make_unique<LogicalLinkControlPacket>(j["value"].template get<CircuitModeControlEntityPacket>()); | ||
} else if (key == "ShortDataServicePacket") { | ||
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<ShortDataServicePacket>()); | ||
} else { | ||
throw std::runtime_error("Unknown packet type: " + key); | ||
} | ||
} | ||
}; | ||
} // namespace nlohmann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters