Module bus.setting

Class Yaml

java.lang.Object
org.miaixz.bus.setting.metric.yaml.Yaml

public class Yaml extends Object
A utility class for reading and writing YAML files, based on the SnakeYAML library.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    dump(Object object, Writer writer)
    Dumps a Java object (e.g., a Map or a bean) to a Writer in YAML format using default pretty-printing options.
    static void
    dump(Object object, Writer writer, org.yaml.snakeyaml.DumperOptions dumperOptions)
    Dumps a Java object to a Writer in YAML format using the specified dumper options.
    static <T> T
    load(InputStream in, Class<T> type)
    Loads YAML data from an InputStream and maps it to the specified class type.
    static org.miaixz.bus.core.center.map.Dictionary
    load(Reader reader)
    Loads YAML data from a Reader, closing the reader upon completion.
    static <T> T
    load(Reader reader, Class<T> type)
    Loads YAML data from a Reader and maps it to the specified class type, closing the reader upon completion.
    static <T> T
    load(Reader reader, Class<T> type, boolean isCloseReader)
    Loads YAML data from a Reader and maps it to the specified class type.
    static org.miaixz.bus.core.center.map.Dictionary
    load(String path)
    Loads a YAML file from the classpath or an absolute path into a Dictionary.
    static <T> T
    load(String path, Class<T> type)
    Loads a YAML file from the classpath or an absolute path and maps it to the specified class type.
    static <T> T
    parse(String content)
    Parses a YAML string into a nested map structure and flattens it.
    static <T> T
    parse(String prefix, Map<String,Object> map)
    Recursively parses a nested map structure, flattening it into a single map with dot-separated keys.
    static String
    replaceRefValue(Properties properties, String value)
    Replaces placeholders in the format ${key} or ${key:defaultValue} within a string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Yaml

      public Yaml()
  • Method Details

    • load

      public static org.miaixz.bus.core.center.map.Dictionary load(String path)
      Loads a YAML file from the classpath or an absolute path into a Dictionary.
      Parameters:
      path - The path to the YAML file (relative to the classpath or absolute).
      Returns:
      The loaded content as a Dictionary.
    • load

      public static <T> T load(String path, Class<T> type)
      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

      public static <T> T load(InputStream in, Class<T> type)
      Loads YAML data from an InputStream and 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

      public static org.miaixz.bus.core.center.map.Dictionary load(Reader reader)
      Loads YAML data from a Reader, closing the reader upon completion.
      Parameters:
      reader - The reader containing the YAML data.
      Returns:
      The loaded content as a Dictionary.
    • load

      public static <T> T load(Reader reader, Class<T> type)
      Loads YAML data from a Reader and 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

      public static <T> T load(Reader reader, Class<T> type, boolean isCloseReader)
      Loads YAML data from a Reader and 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 - If true, the reader will be closed after loading.
      Returns:
      The loaded content as an instance of the specified type.
    • parse

      public static <T> T parse(String content)
      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

      public static <T> T parse(String prefix, Map<String,Object> map)
      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

      public static void dump(Object object, Writer writer)
      Dumps a Java object (e.g., a Map or a bean) to a Writer in 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 a Writer in 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

      public static String replaceRefValue(Properties properties, String value)
      Replaces placeholders in the format ${key} or ${key:defaultValue} within a string. The values are resolved from system properties, environment variables, and the provided Properties object, in that order.
      Parameters:
      properties - The properties object to use for resolving placeholders.
      value - The string containing placeholders.
      Returns:
      The string with placeholders replaced.