YAML minimizes the amount of structural characters in order to view and understand more easily the data.
For example:
YAML want to be an official subset of JSON. Every JSON file is also a valid YAML file.
YAML supports the following style to denote scope :
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
YAML’s representation of native data structure is a rooted, connected, directed graph of tagged nodes.
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.
I prefer SnakeYaml because:
The object must have:
Dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.2</version>
</dependency>
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);