YAML minimizes the amount of structural characters in order to view and understand more easily the data.
- indentation may be used for structure,
- colons separate key: value pairs,
- dashes are used to create “bullet” lists.
YAML want to be an official subset of JSON. Every JSON file is also a valid YAML file.
Flow style uses explicit indicators rather than indentation where:
--- start of a document (optional for the first document) ... end document without starting a new one, for use in communication channels
Example: Two Documents in a Stream (each with a leading comment)
# Ranking of 1998 home runs --- - Mark McGwire - Sammy Sosa - Ken Griffey # Team ranking --- - Chicago Cubs - St Louis Cardinals
A YAML node represents a single native data structure.
Each node has a tag which serves to restrict the set of possible values the content can have.
- Sequence - The content of a sequence node is an ordered series of zero or more nodes. In particular, a sequence may contain the same node more than once. It could even contain itself (directly or indirectly).
- Mapping - The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.
I prefer SnakeYaml because:
- the errors are more comprehesnible
- Jackson use SnakeYaml
InputStream inputStream = dataGenYmlTest.class.getResourceAsStream("/DataGen/dataGen.yml"); ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); TypeFactory typeFactory = mapper.getTypeFactory(); // For a collection`of object CollectionType collectionType = typeFactory.constructCollectionType(List.class, myObjectToBuild.class); List<myObjectToBuild> myObjectToBuilds = mapper.readValue(inputStream, collectionType);
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // Object To String String yaml = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectToSerialize); System.out.println(yaml); // Object To File mapper.writeValue(new File("./objectSerialized.yml"), objectToSerialize);