I have the following SnakeYAML-based code (v1.17):
public abstract class Beverage {
protected int quantityOunces;
// Getters, setters, ctors, etc.
}
public class AlcoholicBeverage extends Beverage {
protected Double alcoholByVolume;
// Getters, setters, ctors, etc.
}
public class SnakeTest {
public static void main(String[] args) {
new SnakeTest().serialize();
}
void serialize() {
AlcoholicBeverage alcBev = new AlcoholicBeverage(20, 7.5);
String alcYml = "/Users/myuser/tmp/alcohol.yml";
FileWriter alcWriter = new FileWriter(alcYml);
Yaml yaml = new Yaml();
yaml.dump(alcBev, alcWriter);
}
}
Which produces the following /Users/myuser/tmp/alcohol.yml
file:
!!me.myapp.model.AlcoholicBeverage {}
I would have expected the file's contents to be something like:
quantityOunces: 20
alcoholByVolume: 7.5
So I ask:
- How do I get the
yaml.dump(...)
to properly serialize object properties into the file?; and - That
!!me.myapp.model.AlcoholicBeverage {}
metadata is annoying...anyway to configureYaml
to ignore/omit it?
You can obtain the required output by modifying your code as follows:
SnakeTest.java
The code has been included in a maven project with the following pom.xml:
output is: