java.lang.Object
org.miaixz.bus.setting.metric.yaml.Yaml
A utility class for reading and writing YAML files, based on the SnakeYAML library.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidDumps a Java object (e.g., a Map or a bean) to aWriterin YAML format using default pretty-printing options.static voidDumps a Java object to aWriterin YAML format using the specified dumper options.static <T> Tload(InputStream in, Class<T> type) Loads YAML data from anInputStreamand maps it to the specified class type.static org.miaixz.bus.core.center.map.DictionaryLoads YAML data from aReader, closing the reader upon completion.static <T> TLoads YAML data from aReaderand maps it to the specified class type, closing the reader upon completion.static <T> TLoads YAML data from aReaderand maps it to the specified class type.static org.miaixz.bus.core.center.map.DictionaryLoads a YAML file from the classpath or an absolute path into aDictionary.static <T> TLoads a YAML file from the classpath or an absolute path and maps it to the specified class type.static <T> TParses a YAML string into a nested map structure and flattens it.static <T> TRecursively parses a nested map structure, flattening it into a single map with dot-separated keys.static StringreplaceRefValue(Properties properties, String value) Replaces placeholders in the format${key}or${key:defaultValue}within a string.
-
Constructor Details
-
Yaml
public Yaml()
-
-
Method Details
-
load
Loads a YAML file from the classpath or an absolute path into aDictionary.- Parameters:
path- The path to the YAML file (relative to the classpath or absolute).- Returns:
- The loaded content as a
Dictionary.
-
load
Loads a YAML file from the classpath or an absolute path and maps it to the specified class type.- Type Parameters:
T- The type of the bean to map to.- Parameters:
path- The path to the YAML file (relative to the classpath or absolute).type- The class type of the target bean.- Returns:
- The loaded content as an instance of the specified type.
-
load
Loads YAML data from anInputStreamand maps it to the specified class type. The stream is automatically closed after loading.- Type Parameters:
T- The type of the bean to map to.- Parameters:
in- The input stream containing the YAML data.type- The class type of the target bean.- Returns:
- The loaded content as an instance of the specified type.
-
load
Loads YAML data from aReader, closing the reader upon completion.- Parameters:
reader- The reader containing the YAML data.- Returns:
- The loaded content as a
Dictionary.
-
load
Loads YAML data from aReaderand maps it to the specified class type, closing the reader upon completion.- Type Parameters:
T- The type of the bean to map to.- Parameters:
reader- The reader containing the YAML data.type- The class type of the target bean.- Returns:
- The loaded content as an instance of the specified type.
-
load
Loads YAML data from aReaderand maps it to the specified class type.- Type Parameters:
T- The type of the bean to map to.- Parameters:
reader- The reader containing the YAML data.type- The class type of the target bean.isCloseReader- Iftrue, the reader will be closed after loading.- Returns:
- The loaded content as an instance of the specified type.
-
parse
Parses a YAML string into a nested map structure and flattens it.- Type Parameters:
T- The expected return type (typically Map).- Parameters:
content- The YAML content as a string.- Returns:
- A flattened map with dot-separated keys.
-
parse
Recursively parses a nested map structure, flattening it into a single map with dot-separated keys.- Type Parameters:
T- The expected return type (typically Map).- Parameters:
prefix- The current key prefix for flattening.map- The map to parse.- Returns:
- A flattened map.
-
dump
Dumps a Java object (e.g., a Map or a bean) to aWriterin YAML format using default pretty-printing options.- Parameters:
object- The object to dump.writer- The writer to which the YAML data will be written.
-
dump
public static void dump(Object object, Writer writer, org.yaml.snakeyaml.DumperOptions dumperOptions) Dumps a Java object to aWriterin YAML format using the specified dumper options.- Parameters:
object- The object to dump.writer- The writer to which the YAML data will be written.dumperOptions- The SnakeYAML dumper options to control the output format.
-
replaceRefValue
Replaces placeholders in the format${key}or${key:defaultValue}within a string. The values are resolved from system properties, environment variables, and the providedPropertiesobject, in that order.- Parameters:
properties- The properties object to use for resolving placeholders.value- The string containing placeholders.- Returns:
- The string with placeholders replaced.
-