Please use the YamlBeans discussion group for support.
YamlBeans makes it easy to serialize and deserialize Java object graphs to and from YAML, a human-friendly data format. Replace XML and properties files with YAML for more expressive power (lists, maps, anchors, etc) and easier hand-editing.
Maven Central: http://repo1.maven.org/maven2/com/esotericsoftware/yamlbeans/yamlbeans/
The YamlReader class is used to deserialize YAML to Java objects. The following YAML defines a Map with four entries. The "phone numbers" entry is a List of two items, each of which is a Map.
name: Nathan Sweetage: 28address: 4011 16th Ave Sphone numbers: - name: Homenumber: 206-555-5138 - name: Worknumber: 425-555-2306The "read" method reads the next YAML document and deserializes it into HashMaps, ArrayLists, and Strings. Since we know the root object defined in the YAML of our example is a Map, we can cast the object and make use of it.
YamlReaderreader = newYamlReader(newFileReader("contact.yml")); Objectobject = reader.read(); System.out.println(object); Mapmap = (Map)object; System.out.println(map.get("address"));A stream of YAML can contain more than one YAML document. Each call to YamlReader#read() deserializes the next document into an object. YAML documents are delimited by "---" (this is optional for the first document).
name: Nathan Sweetage: 28---name: Some Oneage: 25This prints the String "28" then "25":
YamlReaderreader = newYamlReader(newFileReader("contact.yml")); while (true){Mapcontact = reader.read(); if (contact == null) break; System.out.println(contact.get("age"))}There are two ways to deserialize something other than HashMaps, ArrayLists, and Strings. Imagine this YAML document and Java class:
name: Nathan Sweetage: 28publicclassContact{publicStringname; publicintage}The "read" method can be passed a class, so the YamlReader knows what it is deserializing:
YamlReaderreader = newYamlReader(newFileReader("contact.yml")); Contactcontact = reader.read(Contact.class); System.out.println(contact.age);The YamlReader creates an instance of the Contact class and sets the "name" and "age" fields. The YamlReader determines the "age" value in the YAML needs to be converted into a int. Deserialization would have failed if the age was not a valid int. The YamlReader can set public fields and bean setter methods.
Instead of telling the YamlReader what type to deserialize, the type can alternatively be specified in the YAML using a tag:
!com.example.Contactname: Nathan Sweetage: 28The YamlWriter class is used to serialize Java objects to YAML. The "write" method automatically handles this by recognizing public fields and bean getter methods.
Contactcontact = newContact(); contact.name = "Nathan Sweet"; contact.age = 28; YamlWriterwriter = newYamlWriter(newFileWriter("output.yml")); writer.write(contact); writer.close();This outputs:
!com.example.Contactname: Nathan Sweetage: 28The tags are automatically output as needed so that the YamlReader class will be able to reconstruct the object graph. For example, serializing this ArrayList does not output any tag for the list because YamlReader uses an ArrayList by default.
Listlist = newArrayList(); list.add("moo"); list.add("cow"); - moo - cowIf the list was a LinkedList, then YamlWriter knows that a tag is needed and outputs:
Listlist = newLinkedList(); list.add("moo"); list.add("cow");!java.util.LinkedList - moo - cowNote that it is not advisable to subclass Collection or Map. YamlBeans will only serialize the collection or map and its elements, not any additional fields.
YamlBeans can serialize any object graph.
publicclassContact{publicStringname; publicintage; publicListphoneNumbers} publicclassPhone{publicStringname; publicStringnumber}friends: - !com.example.Contactname: Bobage: 29phoneNumbers: - !com.example.Phonename: Homenumber: 206-555-1234 - !com.example.Phonename: Worknumber: 206-555-5678 - !com.example.Contactname: Mikeage: 31phoneNumbers: - !com.example.Phonenumber: 206-555-4321enemies: - !com.example.Contactname: BillphoneNumbers: - !com.example.Phonename: Cellnumber: 206-555-1234This is a map of lists of contacts, each with a list of phone numbers. Again, the public fields could also have been bean properties.
Tags can be lengthy sometimes and can clutter up the YAML. Alternate tags can be defined for a class and will be used instead of the full class name.
YamlWriterwriter = newYamlWriter(newFileWriter("output.yml")); writer.getConfig().setClassTag("contact", Contact.class); writer.write(contact); writer.close();The output no longer contains the full classname for the Contact class.
!contactname: Nathan Sweetage: 28When reading or writing a List or Map, YamlBeans cannot know what type of objects are supposed to be in the List or Map, so it will write out a tag.
!com.example.Contactname: BillphoneNumbers: - !com.example.Phonenumber: 206-555-1234 - !com.example.Phonenumber: 206-555-5678 - !com.example.Phonenumber: 206-555-7654This can make the YAML less readable. To improve this, you may define what element type should be expected for a List or Map field on your object.
YamlWriterwriter = newYamlWriter(newFileWriter("output.yml")); writer.getConfig().setPropertyElementType(Contact.class, "phoneNumbers", Phone.class); writer.write(contact); writer.close();Now YamlBeans knows what to expect for elements of the "phoneNumbers" field, so extra tags will not be output.
!com.example.Contactname: BillphoneNumbers: - number: 206-555-1234 - number: 206-555-5678 - number: 206-555-7654Setting the element type for a Map field tells YamlBeans what to expect for values in the Map. Keys in a Map are always Strings.
When an object graph contains multiple references to the same object, an anchor may be used so that the object only needs to be defined once in the YAML.
oldest friend: &1 !contactname: Bobage: 29best friend: *1In this map, the "oldest friend" and "best friend" keys reference the same object. The YamlReader automatically handles the anchors in the YAML when rebuilding the object graph. By default, the YamlWriter automatically outputs anchors when writing objects.
Contactcontact = newContact(); contact.name = "Bob"; contact.age = 29; Mapmap = newHashMap(); map.put("oldest friend", contact); map.put("best friend", contact);The YAML tokenizer, parser, and emitter are based on those from the JvYAML project. They have been heavily refactored, bugs fixed, etc. The rest of the JvYAML project was not used because of its complexity. YamlBeans strives for the simplest possible thing that works, with the goal being to make it easy to use the YAML data format with Java.
YamlBeans supports YAML version 1.0 and 1.1.
See the javadocs for various other features available on the YamlConfig class.