I am using yamlbeans to deserialize yaml files in Java objects. This works great as long as I have only one class. The problem is that when I want to nest a field, I am forced to specify a nested class in the yaml description.
An example of one class:
Java:
public class MessageField { public String name; public String type; public int length; public String comment; }
YAML:
name: field1 type: int length: 4 comment: first field
Multiple classes (required! Com.mylib.VariableField in yaml file)
Java:
public class MessageField { public String name; public String type; public int length; public String comment; public List<VariableField> variableFields; } public class VariableField extends MessageField{ public int id; }
YAML:
name: field3 type: short length: 2 comment: variableFields: - !com.mylib.VariableField id: 1 name: nestedField 1 type: string length: -1 comment:
The yaml documentation page describes how to deserialize the class by specifying the type when reading the class, which is what I do for the top-level class:
YamlReader reader = new YamlReader(new FileReader("sample.yml")); MessageField mf = reader.read(MessageField.class);
This parses my fields correctly for the top-level class, but does not allow me to avoid the identifier! com.mylib.VariableField for my nested class. I am trying to figure out if there is a way to change the Java code so that yaml files do not require to know the class name.
source share