Class XmlSerializer

java.lang.Object
cloud.opencode.base.serialization.xml.XmlSerializer
All Implemented Interfaces:
Serializer

public class XmlSerializer extends Object implements Serializer
XmlSerializer - XML Serialization using JAXB XML 序列化器(使用 JAXB)

Provides XML serialization using JAXB (Jakarta XML Binding). Objects should be annotated with JAXB annotations for best results.

使用 JAXB (Jakarta XML Binding) 提供 XML 序列化。 对象应使用 JAXB 注解以获得最佳效果。

Features | 主要功能:

  • Human-readable output - 人类可读的输出
  • Standard XML format - 标准 XML 格式
  • JAXB annotation support - JAXB 注解支持
  • Schema validation capable - 可进行 Schema 验证

Limitations | 限制:

  • Limited generic type support - 有限的泛型类型支持
  • Requires JAXB annotations for complex types - 复杂类型需要 JAXB 注解

Usage Examples | 使用示例:

XmlSerializer serializer = new XmlSerializer();

// Serialize
byte[] data = serializer.serialize(user);

// Deserialize
User restored = serializer.deserialize(data, User.class);

Security | 安全性:

  • Thread-safe: Yes (JAXB context cached) - 线程安全: 是(JAXB 上下文已缓存)

Performance | 性能特性:

  • Time complexity: O(n) where n = object graph size - O(n), n为对象图大小
  • Space complexity: O(n) for XML output - XML输出 O(n)
Since:
JDK 25, opencode-base-serialization V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Constructor Details

    • XmlSerializer

      public XmlSerializer()
  • Method Details

    • serialize

      public byte[] serialize(Object obj)
      Description copied from interface: Serializer
      Serializes an object to byte array. 将对象序列化为字节数组。
      Specified by:
      serialize in interface Serializer
      Parameters:
      obj - the object to serialize - 要序列化的对象
      Returns:
      the serialized bytes - 序列化后的字节数组
    • deserialize

      public <T> T deserialize(byte[] data, Class<T> type)
      Description copied from interface: Serializer
      Deserializes byte array to an object of the specified class. 将字节数组反序列化为指定类的对象。
      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      type - the target class - 目标类
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public <T> T deserialize(byte[] data, TypeReference<T> typeRef)
      Description copied from interface: Serializer
      Deserializes byte array to a generic type using TypeReference. 使用 TypeReference 将字节数组反序列化为泛型类型。

      This method preserves generic type information that would otherwise be lost due to type erasure.

      此方法保留了由于类型擦除而丢失的泛型类型信息。

      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      typeRef - the type reference - 类型引用
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public <T> T deserialize(byte[] data, Type type)
      Description copied from interface: Serializer
      Deserializes byte array using a Type. 使用 Type 反序列化字节数组。
      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      type - the target type - 目标类型
      Returns:
      the deserialized object - 反序列化后的对象
    • getFormat

      public String getFormat()
      Description copied from interface: Serializer
      Returns the format name of this serializer. 返回此序列化器的格式名称。

      Examples: "json", "xml", "kryo", "protobuf", "jdk"

      示例: "json", "xml", "kryo", "protobuf", "jdk"

      Specified by:
      getFormat in interface Serializer
      Returns:
      the format name - 格式名称
    • getMimeType

      public String getMimeType()
      Description copied from interface: Serializer
      Returns the MIME type for this serializer. 返回此序列化器的 MIME 类型。
      Specified by:
      getMimeType in interface Serializer
      Returns:
      the MIME type (default: "application/octet-stream") - MIME 类型
    • isTextBased

      public boolean isTextBased()
      Description copied from interface: Serializer
      Returns whether this serializer produces text output. 返回此序列化器是否产生文本输出。

      Text-based serializers (JSON, XML) can be converted to String without data loss.

      基于文本的序列化器(JSON、XML)可以无损转换为字符串。

      Specified by:
      isTextBased in interface Serializer
      Returns:
      true if text-based - 如果是基于文本的则返回 true
    • serializeToString

      public String serializeToString(Object obj)
      Serializes an object to XML string. 将对象序列化为 XML 字符串。
      Parameters:
      obj - the object to serialize - 要序列化的对象
      Returns:
      the XML string - XML 字符串
    • deserialize

      public <T> T deserialize(String xml, Class<T> type)
      Deserializes from XML string. 从 XML 字符串反序列化。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      xml - the XML string - XML 字符串
      type - the target type - 目标类型
      Returns:
      the deserialized object - 反序列化后的对象