Class OpenYml
java.lang.Object
cloud.opencode.base.yml.OpenYml
OpenYml - Main entry point for YAML operations
OpenYml - YAML 操作的主入口点
This class provides a simple API for parsing, writing, and manipulating YAML.
此类提供解析、写入和操作 YAML 的简单 API。
Features | 主要功能:
- Parse YAML from strings, files, and streams - 从字符串、文件和流解析 YAML
- Write/dump YAML to strings, files, and writers - 将 YAML 写入字符串、文件和写入器
- Object binding (YAML to Java objects and back) - 对象绑定(YAML 到 Java 对象的双向转换)
- Document merging with configurable strategies - 可配置策略的文档合并
- Placeholder resolution (${key:default}) - 占位符解析
- Multi-document YAML support - 多文档 YAML 支持
Usage Examples | 使用示例:
// Parse YAML
Map<String, Object> data = OpenYml.load("server:\n port: 8080");
YmlDocument doc = OpenYml.parse("server:\n port: 8080");
// Load from file
YmlDocument doc = OpenYml.loadFile(Path.of("config.yml"));
// Write YAML
String yaml = OpenYml.dump(data);
// Bind to object
ServerConfig config = OpenYml.bind(doc, ServerConfig.class);
// Merge documents
Map<String, Object> merged = OpenYml.merge(base, overlay);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: No (null input may throw exceptions) - 空值安全: 否(空输入可能抛出异常)
- Uses SafeConstructor to prevent arbitrary type deserialization - 使用 SafeConstructor 防止任意类型反序列化
- Since:
- JDK 25, opencode-base-yml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tbind(YmlDocument document, Class<T> type) Binds document to a Java object.static <T> Tbind(YmlDocument document, String path, Class<T> type) Binds document at path to a Java object.static <T> TBinds YAML string to a Java object.diff(YmlDocument base, YmlDocument other) Compares two YAML documents and returns a list of differences.Compares two YAML maps and returns a list of differences.static Stringdump(YmlDocument document) Dumps document to YAML string.static StringDumps data to YAML string.static StringDumps data to YAML string with config.static StringDumps multiple documents.static StringdumpObject(Object object) Dumps object to YAML string.static booleanChecks if YAML string is valid.Parses YAML string to a Map.static List<YmlDocument> Loads multi-document YAML.static YmlDocumentloadDefaultProfile(Path directory, String... profiles) Loads configuration with profile overlays using default name "application".static YmlDocumentLoads YAML from a file.static YmlDocumentLoads YAML from a file with charset.static YmlDocumentloadProfile(Path basePath, String name, String... profiles) Loads configuration with profile overlays.static YmlDocumentloadStream(InputStream input) Loads YAML from an input stream.loadWithIncludes(Path file) Loads a YAML file with!includedirective resolution.static YmlDocumentmerge(YmlDocument base, YmlDocument overlay) Merges two documents.Merges two YAML maps.Merges two YAML maps with strategy.Merges multiple YAML maps.static YmlDocumentParses YAML to a document.static YmlNodeParses YAML to a node tree.static YmlDocumentparseWithPlaceholders(String yaml) Parses YAML with placeholder resolution.static YmlDocumentparseWithPlaceholders(String yaml, Map<String, String> properties) Parses YAML with placeholder resolution using custom properties.static StringresolvePlaceholders(String yaml) Resolves placeholders in YAML string.static StringresolvePlaceholders(String yaml, PlaceholderResolver resolver) Resolves placeholders in YAML string with resolver.resolvePlaceholders(Map<String, Object> data) Resolves placeholders in map values.Converts object to a Map.static voidWrites YAML to a writer.static voidWrites YAML to a file.static voidWrites YAML to a file with charset.
-
Method Details
-
load
-
parse
Parses YAML to a document. 将 YAML 解析为文档。- Parameters:
yaml- the YAML string | YAML 字符串- Returns:
- the document | 文档
-
loadFile
Loads YAML from a file. 从文件加载 YAML。- Parameters:
path- the file path | 文件路径- Returns:
- the document | 文档
-
loadFile
Loads YAML from a file with charset. 使用字符集从文件加载 YAML。- Parameters:
path- the file path | 文件路径charset- the charset | 字符集- Returns:
- the document | 文档
-
loadStream
Loads YAML from an input stream. 从输入流加载 YAML。- Parameters:
input- the input stream | 输入流- Returns:
- the document | 文档
-
loadAll
Loads multi-document YAML. 加载多文档 YAML。- Parameters:
yaml- the YAML string | YAML 字符串- Returns:
- list of documents | 文档列表
-
dump
-
dump
-
dump
Dumps document to YAML string. 将文档转储为 YAML 字符串。- Parameters:
document- the document | 文档- Returns:
- the YAML string | YAML 字符串
-
dumpObject
-
writeFile
-
writeFile
-
write
-
dumpAll
-
bind
Binds document to a Java object. 将文档绑定到 Java 对象。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
document- the document | 文档type- the target type | 目标类型- Returns:
- the bound object | 绑定的对象
-
bind
Binds document at path to a Java object. 将指定路径的文档绑定到 Java 对象。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
document- the document | 文档path- the path prefix | 路径前缀type- the target type | 目标类型- Returns:
- the bound object | 绑定的对象
-
bind
-
toMap
-
merge
-
merge
public static Map<String,Object> merge(Map<String, Object> base, Map<String, Object> overlay, MergeStrategy strategy) Merges two YAML maps with strategy. 使用策略合并两个 YAML 映射。- Parameters:
base- the base map | 基础映射overlay- the overlay map | 覆盖映射strategy- the merge strategy | 合并策略- Returns:
- the merged map | 合并后的映射
-
mergeAll
-
merge
Merges two documents. 合并两个文档。- Parameters:
base- the base document | 基础文档overlay- the overlay document | 覆盖文档- Returns:
- the merged document | 合并后的文档
-
resolvePlaceholders
-
resolvePlaceholders
Resolves placeholders in YAML string with resolver. 使用解析器解析 YAML 字符串中的占位符。- Parameters:
yaml- the YAML string | YAML 字符串resolver- the placeholder resolver | 占位符解析器- Returns:
- the resolved YAML | 解析后的 YAML
-
resolvePlaceholders
-
parseWithPlaceholders
Parses YAML with placeholder resolution. 解析 YAML 并解析占位符。- Parameters:
yaml- the YAML string | YAML 字符串- Returns:
- the document with resolved placeholders | 解析了占位符的文档
-
parseWithPlaceholders
Parses YAML with placeholder resolution using custom properties. 使用自定义属性解析 YAML 并解析占位符。- Parameters:
yaml- the YAML string | YAML 字符串properties- the properties for placeholder resolution | 用于占位符解析的属性- Returns:
- the document with resolved placeholders | 解析了占位符的文档
-
isValid
Checks if YAML string is valid. 检查 YAML 字符串是否有效。- Parameters:
yaml- the YAML string | YAML 字符串- Returns:
- true if valid | 如果有效则返回 true
-
parseTree
-
diff
-
diff
Compares two YAML documents and returns a list of differences. 比较两个 YAML 文档并返回差异列表。- Parameters:
base- the base document | 基础文档other- the other document | 另一个文档- Returns:
- list of diff entries | 差异条目列表
-
loadProfile
Loads configuration with profile overlays. 加载带有 Profile 覆盖的配置。Loads
{basePath}/{name}.ymlas the base, then overlays{basePath}/{name}-{profile}.ymlfor each profile.加载
{basePath}/{name}.yml作为基础,然后为每个 Profile 覆盖{basePath}/{name}-{profile}.yml。- Parameters:
basePath- the directory containing config files | 包含配置文件的目录name- the base config file name (without extension) | 基础配置文件名(无扩展名)profiles- the profile names to overlay | 要覆盖的 Profile 名称- Returns:
- the merged document | 合并后的文档
-
loadDefaultProfile
Loads configuration with profile overlays using default name "application". 使用默认名称 "application" 加载带有 Profile 覆盖的配置。- Parameters:
directory- the directory containing config files | 包含配置文件的目录profiles- the profile names to overlay | 要覆盖的 Profile 名称- Returns:
- the merged document | 合并后的文档
-
loadWithIncludes
-