libe3 0.2.5
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
json_encoder.hpp
Go to the documentation of this file.
1/**
2 * @file json_encoder.hpp
3 * @brief JSON Encoder for E3AP PDUs
4 *
5 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef LIBE3_JSON_ENCODER_HPP
10#define LIBE3_JSON_ENCODER_HPP
11
12#include "libe3/e3_encoder.hpp"
13#include <nlohmann/json.hpp>
14
15namespace libe3 {
16
17/**
18 * @brief JSON encoder for E3AP PDUs
19 *
20 * Provides JSON encoding/decoding for development and debugging.
21 *
22 * JSON wire format uses camelCase keys (e.g. "dAppName", "ranFunctionIdentifier")
23 * and camelCase PDU type strings (e.g. "setupRequest", "indicationMessage").
24 * PascalCase PDU types are rejected by the decoder.
25 *
26 * Envelope format is flat: payload fields sit alongside "type", "id", and
27 * "timestamp" at root level. Messages with a "data" wrapper are rejected.
28 */
29class JsonE3Encoder : public E3Encoder {
30public:
31 JsonE3Encoder() = default;
32 ~JsonE3Encoder() override = default;
33
34 EncodeResult<EncodedMessage> encode(const Pdu& pdu) override;
35 EncodeResult<Pdu> decode(const EncodedMessage& encoded) override;
36 EncodeResult<Pdu> decode(const uint8_t* data, size_t size) override;
37 EncodingFormat format() const noexcept override { return EncodingFormat::JSON; }
38
39private:
40 // Helper methods for encoding PDU types to JSON
41 nlohmann::json encode_setup_request(const SetupRequest& req) const;
42 nlohmann::json encode_setup_response(const SetupResponse& resp) const;
43 nlohmann::json encode_subscription_request(const SubscriptionRequest& req) const;
44 nlohmann::json encode_subscription_delete(const SubscriptionDelete& del) const;
45 nlohmann::json encode_subscription_response(const SubscriptionResponse& resp) const;
46 std::optional<nlohmann::json> encode_indication_message(const IndicationMessage& msg) const;
47 nlohmann::json encode_dapp_control_action(const DAppControlAction& action) const;
48 nlohmann::json encode_dapp_report(const DAppReport& report) const;
49 nlohmann::json encode_xapp_control_action(const XAppControlAction& action) const;
50 nlohmann::json encode_release_message(const ReleaseMessage& msg) const;
51 nlohmann::json encode_message_ack(const MessageAck& ack) const;
52
53 // Helper methods for decoding JSON to PDU types
54 SetupRequest decode_setup_request(const nlohmann::json& j) const;
55 SetupResponse decode_setup_response(const nlohmann::json& j) const;
56 SubscriptionRequest decode_subscription_request(const nlohmann::json& j) const;
57 SubscriptionDelete decode_subscription_delete(const nlohmann::json& j) const;
58 SubscriptionResponse decode_subscription_response(const nlohmann::json& j) const;
59 IndicationMessage decode_indication_message(const nlohmann::json& j, const std::string& raw_json) const;
60 DAppControlAction decode_dapp_control_action(const nlohmann::json& j) const;
61 DAppReport decode_dapp_report(const nlohmann::json& j) const;
62 XAppControlAction decode_xapp_control_action(const nlohmann::json& j) const;
63 ReleaseMessage decode_release_message(const nlohmann::json& j) const;
64 MessageAck decode_message_ack(const nlohmann::json& j) const;
65
66 // Binary data encoding helpers (hex string)
67 static std::string binary_to_hex(const std::vector<uint8_t>& data);
68 static std::vector<uint8_t> hex_to_binary(const std::string& hex);
69
70 // Helper methods for type conversions
71 std::optional<PduType> string_to_pdu_type(const std::string& s) const;
72 ErrorCode string_to_error_code(const std::string& s) const;
73};
74
75} // namespace libe3
76
77#endif // LIBE3_JSON_ENCODER_HPP
E3Encoder(const E3Encoder &)=delete
~JsonE3Encoder() override=default
EncodeResult< Pdu > decode(const EncodedMessage &encoded) override
Decode bytes to a PDU.
EncodingFormat format() const noexcept override
Get the encoding format.
EncodeResult< EncodedMessage > encode(const Pdu &pdu) override
Encode a PDU to bytes.
Abstract E3 Encoder interface for PDU encoding/decoding.
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:141
EncodingFormat
E3AP encoding formats supported by the library.
Definition types.hpp:51
@ JSON
JSON encoding (for development/debugging).
Definition types.hpp:53
tl::expected< T, ErrorCode > EncodeResult
Result type for encoding/decoding operations.
E3AP dApp Control Action structure.
Definition types.hpp:272
E3AP dApp Report structure.
Definition types.hpp:293
Encoded message wrapper.
Definition types.hpp:175
E3AP Indication Message structure.
Definition types.hpp:263
E3AP Message Acknowledgment structure.
Definition types.hpp:285
Generic E3AP PDU structure.
Definition types.hpp:345
E3AP Release Message structure.
Definition types.hpp:321
E3AP Setup Request structure.
Definition types.hpp:201
E3AP Setup Response structure.
Definition types.hpp:221
E3AP Subscription Delete structure.
Definition types.hpp:245
E3AP Subscription Request structure.
Definition types.hpp:233
E3AP Subscription Response structure.
Definition types.hpp:253
E3AP xApp Control Action structure.
Definition types.hpp:306