Package org.kiwiproject.yaml
Class YamlHelper
java.lang.Object
org.kiwiproject.yaml.YamlHelper
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
ConstructorsConstructorDescriptionCreate 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
Modifier and TypeMethodDescriptionvoidcheckYamlNotBlank(String yaml) Convert the given YAML into a map with String keys and Object values.<K,V> Map<K, V> Convert the given YAML into a map with keys of typeKand values of typeV.<T> TConvert the given YAML into an object of typeTusing the givenTypeReference.<T> TConvert 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.Convert the given object to YAML.Convert the given object to YAML using the givenJsonView.
-
Constructor Details
-
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 Details
-
toYaml
Convert the given object to YAML.- Parameters:
object- the object to convert- Returns:
- a YAML representation of the given object
-
toYaml
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
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
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
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
-