This module contains a class YamlUtil
providing static methods for YAML-file handling.
The YamlUtil
provides an overloaded method for loading YAML-files and one for serialization.
Given the following YAML-file
message: "Hello"
attribute: "attribute1"it's possible to load and serialize a single object:
import org.sdase.commons.shared.yaml.YamlUtil;
class MyClass {
public static void main(final String[] args) {
InputStream resource = this.getClass().getClassLoader().getResourceAsStream("sample.yml");
TestBean tb = YamlUtil.load(resource, TestBean.class);
// ...
String serializedClass = YamlUtil.writeValueAsString(tb);
}
}To load a list of objects from a YAML-file like
- message: Hello World!
attribute: Foo
id: 123
- message: Hello Universe!
attribute: Bar
id: 456use a TypeReference<T> as the second parameter:
List<TestBean> beans = YamlUtil.load(resource, new TypeReference<List<TestBean>>() {});To load a YAML-file that contains multiple YAML-documents, like
message: Hello World!
attribute: Foo
id: 123
---
message: Hello Universe!
attribute: Bar
id: 456use YamlUtil.loadList():
List<TestBean> beans = YamlUtil.loadList(resource, TestBean.class);