Class TreeSerializer
java.lang.Object
cloud.opencode.base.tree.serialization.TreeSerializer
Tree Serializer
树序列化器
Utility for serializing trees to JSON and XML formats without external dependencies.
无外部依赖的树序列化工具,支持JSON和XML格式。
Key Features | 主要功能:
- JSON export - Serialize to JSON format - JSON导出
- XML export - Serialize to XML format - XML导出
- Map conversion - Convert to/from Map - Map转换
- Custom field mapping - Configure field names - 自定义字段映射
- Pretty print - Formatted output - 格式化输出
Usage Examples | 使用示例:
// Serialize to JSON - 序列化为JSON
String json = TreeSerializer.toJson(roots);
// Serialize to XML - 序列化为XML
String xml = TreeSerializer.toXml(roots);
// Convert to Maps - 转换为Map列表
List<Map<String, Object>> maps = TreeSerializer.toMaps(roots);
// Custom config - 自定义配置
SerializerConfig config = SerializerConfig.builder()
.prettyPrint(true)
.childrenField("items")
.build();
String json = TreeSerializer.toJson(roots, config);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Partial (null values serialized as "null") - 空值安全: 部分(null值序列化为"null")
Performance | 性能特性:
- Time complexity: O(n) - each node visited exactly once during recursive traversal - 时间复杂度: O(n) - 递归遍历中每个节点恰好访问一次
- Space complexity: O(n) - output StringBuilder and result structures proportional to total node count - 空间复杂度: O(n) - 输出 StringBuilder 和结果结构与总节点数成正比
- Since:
- JDK 25, opencode-base-tree V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordSerializer Configuration 序列化配置 -
Method Summary
Modifier and TypeMethodDescriptiontoFlatMaps(List<T> roots) Flatten tree to list of maps (without hierarchy) 将树扁平化为Map列表(无层级)toFlatMaps(List<T> roots, TreeSerializer.SerializerConfig config) Flatten tree to list of maps with config 使用配置将树扁平化为Map列表Serialize tree to JSON 将树序列化为JSONtoJson(List<T> roots, TreeSerializer.SerializerConfig config) Serialize tree to JSON with config 使用配置将树序列化为JSONtoJsonSingle(T root) Serialize single tree node to JSON 将单个树节点序列化为JSONtoJsonSingle(T root, TreeSerializer.SerializerConfig config) Serialize single tree node to JSON with config 使用配置将单个树节点序列化为JSONtoMap(T node) Convert single tree node to map 将单个树节点转换为MapConvert tree to list of maps 将树转换为Map列表toMaps(List<T> roots, TreeSerializer.SerializerConfig config) Convert tree to list of maps with config 使用配置将树转换为Map列表Serialize tree to XML 将树序列化为XMLtoXml(List<T> roots, TreeSerializer.SerializerConfig config) Serialize tree to XML with config 使用配置将树序列化为XMLtoXmlSingle(T root) Serialize single tree node to XML 将单个树节点序列化为XMLtoXmlSingle(T root, TreeSerializer.SerializerConfig config) Serialize single tree node to XML with config 使用配置将单个树节点序列化为XMLstatic <T> StringSerialize TreeNode to JSON 将TreeNode序列化为JSONstatic <T> StringtreeNodeToJson(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer, TreeSerializer.SerializerConfig config) Serialize TreeNode to JSON with config 使用配置将TreeNode序列化为JSONstatic <T> StringSerialize TreeNode to XML 将TreeNode序列化为XMLstatic <T> StringtreeNodeToXml(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer, TreeSerializer.SerializerConfig config) Serialize TreeNode to XML with config 使用配置将TreeNode序列化为XML
-
Method Details
-
toJson
-
toJson
public static <T extends Treeable<T,ID>, ID> String toJson(List<T> roots, TreeSerializer.SerializerConfig config) Serialize tree to JSON with config 使用配置将树序列化为JSON- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点config- the serializer config | 序列化配置- Returns:
- the JSON string | JSON字符串
-
toJsonSingle
-
toJsonSingle
public static <T extends Treeable<T,ID>, ID> String toJsonSingle(T root, TreeSerializer.SerializerConfig config) Serialize single tree node to JSON with config 使用配置将单个树节点序列化为JSON- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
root- the root node | 根节点config- the serializer config | 序列化配置- Returns:
- the JSON string | JSON字符串
-
treeNodeToJson
public static <T> String treeNodeToJson(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer) Serialize TreeNode to JSON 将TreeNode序列化为JSON- Type Parameters:
T- the data type | 数据类型- Parameters:
root- the root node | 根节点dataSerializer- function to serialize data | 数据序列化函数- Returns:
- the JSON string | JSON字符串
-
treeNodeToJson
public static <T> String treeNodeToJson(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer, TreeSerializer.SerializerConfig config) Serialize TreeNode to JSON with config 使用配置将TreeNode序列化为JSON- Type Parameters:
T- the data type | 数据类型- Parameters:
root- the root node | 根节点dataSerializer- function to serialize data | 数据序列化函数config- the serializer config | 序列化配置- Returns:
- the JSON string | JSON字符串
-
toXml
-
toXml
public static <T extends Treeable<T,ID>, ID> String toXml(List<T> roots, TreeSerializer.SerializerConfig config) Serialize tree to XML with config 使用配置将树序列化为XML- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点config- the serializer config | 序列化配置- Returns:
- the XML string | XML字符串
-
toXmlSingle
-
toXmlSingle
public static <T extends Treeable<T,ID>, ID> String toXmlSingle(T root, TreeSerializer.SerializerConfig config) Serialize single tree node to XML with config 使用配置将单个树节点序列化为XML- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
root- the root node | 根节点config- the serializer config | 序列化配置- Returns:
- the XML string | XML字符串
-
treeNodeToXml
public static <T> String treeNodeToXml(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer) Serialize TreeNode to XML 将TreeNode序列化为XML- Type Parameters:
T- the data type | 数据类型- Parameters:
root- the root node | 根节点dataSerializer- function to serialize data | 数据序列化函数- Returns:
- the XML string | XML字符串
-
treeNodeToXml
public static <T> String treeNodeToXml(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer, TreeSerializer.SerializerConfig config) Serialize TreeNode to XML with config 使用配置将TreeNode序列化为XML- Type Parameters:
T- the data type | 数据类型- Parameters:
root- the root node | 根节点dataSerializer- function to serialize data | 数据序列化函数config- the serializer config | 序列化配置- Returns:
- the XML string | XML字符串
-
toMaps
-
toMaps
public static <T extends Treeable<T,ID>, ID> List<Map<String,Object>> toMaps(List<T> roots, TreeSerializer.SerializerConfig config) Convert tree to list of maps with config 使用配置将树转换为Map列表- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点config- the serializer config | 序列化配置- Returns:
- the map list | Map列表
-
toMap
-
toFlatMaps
Flatten tree to list of maps (without hierarchy) 将树扁平化为Map列表(无层级)- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点- Returns:
- the flat map list | 扁平Map列表
-
toFlatMaps
public static <T extends Treeable<T,ID>, ID> List<Map<String,Object>> toFlatMaps(List<T> roots, TreeSerializer.SerializerConfig config) Flatten tree to list of maps with config 使用配置将树扁平化为Map列表- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点config- the serializer config | 序列化配置- Returns:
- the flat map list | 扁平Map列表
-