Class YmlJson
java.lang.Object
cloud.opencode.base.yml.transform.YmlJson
YmlJson - YAML to JSON bidirectional converter with no external dependencies
YmlJson - 无外部依赖的 YAML 与 JSON 双向转换器
Converts between YAML and JSON formats using only JDK built-in capabilities. JSON generation is done via StringBuilder; JSON parsing leverages the fact that JSON is valid YAML 1.2, so the existing YAML parser handles it.
使用纯 JDK 内置能力在 YAML 和 JSON 格式之间进行转换。 JSON 生成通过 StringBuilder 完成;JSON 解析利用 JSON 是有效 YAML 1.2 的特性, 由现有 YAML 解析器处理。
Features | 主要功能:
- YAML string to JSON string conversion - YAML 字符串到 JSON 字符串转换
- Map to JSON string conversion - Map 到 JSON 字符串转换
- JSON string to YAML Map conversion - JSON 字符串到 YAML Map 转换
- JSON string to YAML string conversion - JSON 字符串到 YAML 字符串转换
- Pretty-print support with 2-space indentation - 2 空格缩进的美化打印
- Proper JSON string escaping (security critical) - 正确的 JSON 字符串转义(安全关键)
- Max depth limit of 50 to prevent stack overflow - 最大深度 50 限制防止栈溢出
Usage Examples | 使用示例:
// YAML to JSON
String json = YmlJson.toJson("server:\n port: 8080");
// => {"server":{"port":8080}}
// Pretty JSON
String pretty = YmlJson.toJson("server:\n port: 8080", true);
// JSON to YAML Map
Map<String, Object> data = YmlJson.fromJson("{\"port\": 8080}");
// JSON to YAML string
String yaml = YmlJson.fromJsonToYaml("{\"port\": 8080}");
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- All JSON special characters properly escaped - 所有 JSON 特殊字符正确转义
- Depth limit prevents stack overflow from deeply nested structures - 深度限制防止深层嵌套导致的栈溢出
- Since:
- JDK 25, opencode-base-yml V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionParses a JSON string to a YAML-compatible Map.static StringfromJsonToYaml(String json) Converts a JSON string to a YAML string.static StringConverts a YAML string to a compact JSON string.static StringConverts a YAML string to a JSON string with optional pretty-printing.static StringConverts a Map to a compact JSON string.static StringConverts a Map to a JSON string with optional pretty-printing.
-
Method Details
-
toJson
Converts a YAML string to a compact JSON string. 将 YAML 字符串转换为紧凑 JSON 字符串。- Parameters:
yaml- the YAML string | YAML 字符串- Returns:
- the JSON string | JSON 字符串
- Throws:
OpenYmlException- if the YAML is invalid or exceeds depth limit | 如果 YAML 无效或超过深度限制
-
toJson
Converts a YAML string to a JSON string with optional pretty-printing. 将 YAML 字符串转换为 JSON 字符串,可选美化打印。- Parameters:
yaml- the YAML string | YAML 字符串pretty- whether to pretty-print the output | 是否美化打印输出- Returns:
- the JSON string | JSON 字符串
- Throws:
OpenYmlException- if the YAML is invalid or exceeds depth limit | 如果 YAML 无效或超过深度限制
-
toJson
Converts a Map to a compact JSON string. 将 Map 转换为紧凑 JSON 字符串。- Parameters:
data- the data map | 数据映射- Returns:
- the JSON string | JSON 字符串
- Throws:
OpenYmlException- if the data exceeds depth limit | 如果数据超过深度限制
-
toJson
Converts a Map to a JSON string with optional pretty-printing. 将 Map 转换为 JSON 字符串,可选美化打印。- Parameters:
data- the data map | 数据映射pretty- whether to pretty-print the output | 是否美化打印输出- Returns:
- the JSON string | JSON 字符串
- Throws:
OpenYmlException- if the data exceeds depth limit | 如果数据超过深度限制
-
fromJson
Parses a JSON string to a YAML-compatible Map. 将 JSON 字符串解析为 YAML 兼容的 Map。Since JSON is valid YAML 1.2, this method uses the existing YAML parser.
由于 JSON 是有效的 YAML 1.2,此方法使用现有的 YAML 解析器。
- Parameters:
json- the JSON string | JSON 字符串- Returns:
- the parsed map | 解析后的映射
- Throws:
OpenYmlException- if the JSON is invalid | 如果 JSON 无效
-
fromJsonToYaml
Converts a JSON string to a YAML string. 将 JSON 字符串转换为 YAML 字符串。- Parameters:
json- the JSON string | JSON 字符串- Returns:
- the YAML string | YAML 字符串
- Throws:
OpenYmlException- if the JSON is invalid | 如果 JSON 无效
-