Class YamlParserUtils


  • public class YamlParserUtils
    extends Object
    This class contains utility methods to load standard yaml file and convert object to standard yaml syntax.
    • Constructor Detail

      • YamlParserUtils

        public YamlParserUtils()
    • Method Detail

      • loadYamlFile

        @Nonnull
        public static Map<String,​Object> loadYamlFile​(File file)
                                                     throws Exception
        Loads the contents of the given YAML file into a map.
        Parameters:
        file - the YAML file to load.
        Returns:
        a non-null map representing the YAML content. If the file is empty or only contains comments, an empty map is returned.
        Throws:
        FileNotFoundException - if the YAML file is not found.
        org.snakeyaml.engine.v2.exceptions.YamlEngineException - if the file cannot be parsed.
        IOException - if an I/O error occurs while reading from the file stream.
        Exception
      • toYAMLString

        public static String toYAMLString​(Object value)
        Converts the given value to a string representation in the YAML syntax. This method uses a YAML parser to convert the object to YAML format.

        The resulting YAML string may have line breaks at the end of each line. This method removes the line break at the end of the string if it exists.

        Note: This method may perform escaping on certain characters in the value to ensure proper YAML syntax.

        Parameters:
        value - The value to be converted.
        Returns:
        The string representation of the value in YAML syntax.
      • convertAndDumpYamlFromFlatMap

        public static List<String> convertAndDumpYamlFromFlatMap​(Map<String,​Object> flattenMap)
        Converts a flat map into a nested map structure and outputs the result as a list of YAML-formatted strings. Each item in the list represents a single line of the YAML data. The method is synchronized and thus thread-safe.
        Parameters:
        flattenMap - A map containing flattened keys (e.g., "parent.child.key") associated with their values.
        Returns:
        A list of strings that represents the YAML data, where each item corresponds to a line of the data.
      • convertToObject

        public static <T> T convertToObject​(String value,
                                            Class<T> type)