Class YamlHelper

java.lang.Object
org.kiwiproject.yaml.YamlHelper

public class YamlHelper extends Object
Some utilities to make it easy to work with YAML.
Implementation Note:
This uses Jackson to perform YAML operations, which in turn uses SnakeYAML, so both of those must be available at runtime.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new instance using an ObjectMapper created with a YAMLFactory to support YAML.
    YamlHelper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Create a new instance using the given ObjectMapper, which must support the YAML format.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    toMap(String yaml)
    Convert the given YAML into a map with String keys and Object values.
    <K, V> Map<K,V>
    toMap(String yaml, com.fasterxml.jackson.core.type.TypeReference<Map<K,V>> targetMapType)
    Convert the given YAML into a map with keys of type K and values of type V.
    <T> T
    toObject(String yaml, com.fasterxml.jackson.core.type.TypeReference<T> targetType)
    Convert the given YAML into an object of type T using the given TypeReference.
    <T> T
    toObject(String yaml, Class<T> targetClass)
    Convert the given YAML into the specified type.
    <T> List<T>
    toObjectList(String yaml, com.fasterxml.jackson.core.type.TypeReference<List<T>> targetListType)
    Convert the given YAML into a List of objects of type T.
    toYaml(Object object)
    Convert the given object to YAML.
    toYaml(Object object, Class<?> yamlView)
    Convert the given object to YAML using the given JsonView.

    Methods inherited from class java.lang.Object

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

    • YamlHelper

      public YamlHelper()
      Create a new instance using an ObjectMapper created with a YAMLFactory to support YAML. In addition, the YAMLFactory disables the SPLIT_LINES feature.
    • YamlHelper

      public YamlHelper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Create a new instance using the given ObjectMapper, which must support the YAML format. Otherwise, it can be configured however you want.
      Parameters:
      objectMapper - the ObjectMapper to use
      Throws:
      IllegalArgumentException - if the object mapper is null or does not support YAML
      See Also:
      • YAMLFactory.getFormatName()
      • YAMLFactory.FORMAT_NAME_YAML
  • Method Details

    • toYaml

      public String toYaml(Object object)
      Convert the given object to YAML.
      Parameters:
      object - the object to convert
      Returns:
      a YAML representation of the given object
    • toYaml

      public String toYaml(Object object, Class<?> yamlView)
      Convert the given object to YAML using the given JsonView.
      Parameters:
      object - the object to convert
      yamlView - the nullable JsonView class
      Returns:
      a YAML representation of the given object
    • toObject

      public <T> T toObject(String yaml, Class<T> targetClass)
      Convert the given YAML into the specified type.
      Type Parameters:
      T - the object type
      Parameters:
      yaml - the YAML content
      targetClass - the type of obejct to convert into
      Returns:
      a new instance of the given type
      Throws:
      IllegalArgumentException - if the YAML is blank or null
    • toObject

      public <T> T toObject(String yaml, com.fasterxml.jackson.core.type.TypeReference<T> targetType)
      Convert the given YAML into an object of type T using the given TypeReference.
      Type Parameters:
      T - the object type
      Parameters:
      yaml - the YAML content
      targetType - the TypeReference representing the target object type
      Returns:
      a new instance of the given type reference
      Throws:
      IllegalArgumentException - if the YAML is blank or null
    • toObjectList

      public <T> List<T> toObjectList(String yaml, com.fasterxml.jackson.core.type.TypeReference<List<T>> targetListType)
      Convert the given YAML into a List of objects of type T.
      Type Parameters:
      T - the object type
      Parameters:
      yaml - the YAML content
      targetListType - the TypeReference representing the target object type
      Returns:
      a list containing objects of the given type
      Throws:
      IllegalArgumentException - if the YAML is blank or null
    • toMap

      public Map<String,Object> toMap(String yaml)
      Convert the given YAML into a map with String keys and Object values.
      Parameters:
      yaml - the YAML content
      Returns:
      the parsed map
      Throws:
      IllegalArgumentException - if the YAML is blank or null
    • toMap

      public <K, V> Map<K,V> toMap(String yaml, com.fasterxml.jackson.core.type.TypeReference<Map<K,V>> targetMapType)
      Convert the given YAML into a map with keys of type K and values of type V.
      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      yaml - the YAML content
      targetMapType - the TypeReference representing the target map type
      Returns:
      the parsed map
      Throws:
      IllegalArgumentException - if the YAML is blank or null
    • checkYamlNotBlank

      public void checkYamlNotBlank(String yaml)