Class EventSerializer

java.lang.Object
cloud.opencode.base.event.serialization.EventSerializer

public final class EventSerializer extends Object
Event Serializer with Optional Serialization Module Delegation 支持可选序列化模块委托的事件序列化器

Serializes and deserializes events for transmission or storage. If the Serialization module (opencode-base-serialization) is available, it delegates to OpenSerializer for better performance and format support. Otherwise, falls back to Java serialization.

序列化和反序列化事件以进行传输或存储。 如果序列化模块可用,则委托给 OpenSerializer 以获得更好的性能和格式支持。 否则降级到 Java 序列化。

Supported Formats (with Serialization module) | 支持的格式(使用序列化模块时):

  • JSON - for human-readable format | 人类可读格式
  • MessagePack - for compact binary format | 紧凑二进制格式
  • Java Serialization - fallback | 回退方案

Usage Examples | 使用示例:

// Check if serialization module is available
if (EventSerializer.isSerializationModuleAvailable()) {
    // Serialize event
    byte[] data = EventSerializer.serialize(event);

    // Deserialize event
    MyEvent restored = EventSerializer.deserialize(data, MyEvent.class);
}

Features | 主要功能:

  • Event serialization and deserialization - 事件序列化和反序列化
  • Secure deserialization with class filtering - 带类过滤的安全反序列化

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)

Performance | 性能特性:

  • Time complexity: O(n) where n is the serialized event size - 时间复杂度: O(n),n 为序列化事件大小
  • Space complexity: O(n) for serialization buffers - 空间复杂度: O(n) 序列化缓冲区
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isSerializationModuleAvailable

      public static boolean isSerializationModuleAvailable()
      Checks if the Serialization module is available. 检查序列化模块是否可用
      Returns:
      true if Serialization module is available | 如果序列化模块可用返回 true
    • serialize

      public static <T extends Event> byte[] serialize(T event)
      Serializes an event to bytes. 将事件序列化为字节数组
      Type Parameters:
      T - the event type | 事件类型
      Parameters:
      event - the event to serialize | 要序列化的事件
      Returns:
      the serialized bytes | 序列化的字节数组
      Throws:
      EventSerializer.EventSerializationException - if serialization fails | 如果序列化失败
    • deserialize

      public static <T extends Event> T deserialize(byte[] data, Class<T> eventType)
      Deserializes bytes to an event. 将字节数组反序列化为事件
      Type Parameters:
      T - the event type | 事件类型
      Parameters:
      data - the serialized data | 序列化的数据
      eventType - the event class | 事件类
      Returns:
      the deserialized event | 反序列化的事件
      Throws:
      EventSerializer.EventSerializationException - if deserialization fails | 如果反序列化失败
    • serializeToString

      public static <T extends Event> String serializeToString(T event)
      Serializes an event to a Base64 string. 将事件序列化为 Base64 字符串
      Type Parameters:
      T - the event type | 事件类型
      Parameters:
      event - the event to serialize | 要序列化的事件
      Returns:
      the Base64 encoded string | Base64 编码的字符串
      Throws:
      EventSerializer.EventSerializationException - if serialization fails | 如果序列化失败
    • deserializeFromString

      public static <T extends Event> T deserializeFromString(String base64, Class<T> eventType)
      Deserializes a Base64 string to an event. 将 Base64 字符串反序列化为事件
      Type Parameters:
      T - the event type | 事件类型
      Parameters:
      base64 - the Base64 encoded string | Base64 编码的字符串
      eventType - the event class | 事件类
      Returns:
      the deserialized event | 反序列化的事件
      Throws:
      EventSerializer.EventSerializationException - if deserialization fails | 如果反序列化失败