Package org.kiwiproject.yaml
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 YamlHelper()Create a new instance using anObjectMappercreated with aYAMLFactoryto support YAML.YamlHelper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)Create a new instance using the givenObjectMapper, which must support the YAML format.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckYamlNotBlank(String yaml)Map<String,Object>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 typeKand values of typeV.<T> TtoObject(String yaml, com.fasterxml.jackson.core.type.TypeReference<T> targetType)Convert the given YAML into an object of typeTusing the givenTypeReference.<T> TtoObject(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 typeT.StringtoYaml(Object object)Convert the given object to YAML.StringtoYaml(Object object, Class<?> yamlView)Convert the given object to YAML using the givenJsonView.
-
-
-
Constructor Detail
-
YamlHelper
public YamlHelper()
Create a new instance using anObjectMappercreated with aYAMLFactoryto support YAML. In addition, theYAMLFactorydisables theSPLIT_LINESfeature.
-
YamlHelper
public YamlHelper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Create a new instance using the givenObjectMapper, which must support the YAML format. Otherwise it can be configured however you want.- Parameters:
objectMapper- theObjectMapperto use- Throws:
IllegalArgumentException- if the object mapper is null or does not support YAML- See Also:
YAMLFactory.getFormatName(),YAMLFactory.FORMAT_NAME_YAML
-
-
Method Detail
-
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 givenJsonView.- Parameters:
object- the object to convertyamlView- the nullableJsonViewclass- 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 contenttargetClass- 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 typeTusing the givenTypeReference.- Type Parameters:
T- the object type- Parameters:
yaml- the YAML contenttargetType- theTypeReferencerepresenting 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 typeT.- Type Parameters:
T- the object type- Parameters:
yaml- the YAML contenttargetListType- theTypeReferencerepresenting 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 typeKand values of typeV.- Type Parameters:
K- the type of keys in the mapV- the type of values in the map- Parameters:
yaml- the YAML contenttargetMapType- theTypeReferencerepresenting the target map type- Returns:
- the parsed map
- Throws:
IllegalArgumentException- if the YAML is blank or null
-
checkYamlNotBlank
public void checkYamlNotBlank(String yaml)
-
-