Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to a YAML file using SnakeYaml?

Consider the following code:

public static void dumpObjectToYaml(String key, Object O, String path) throws IOException
{
    Map<String, Object> data = new HashMap<>();
    data.put(key, O);

    File F = new File(path);
    F.mkdirs();
    F.createNewFile();

    //write data to File
}

This method is aiming to write the given Object O at the given key, into the YAML file at the given path. (if it doesn't exist it is created.) But obviously the main part is still missing.

Now following the documentation of SnakeYaml, to create a YAML I only need to create a map and put in the Objects at the right keys, which I did.

But nowhere (at least I don't see it) is described how to create a yaml file at a certain path!

The only thing I found was:

"The Yaml.dump(Object data) method accepts a Java object and produces a YAML document"

public void testDump() 
{
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("name", "Silenthand Olleander");
    data.put("race", "Human");
    data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
    Yaml yaml = new Yaml();
    String output = yaml.dump(data);
    System.out.println(output);
}

and

"Yaml.dump(Object data, Writer output) will write the produced YAML document into the specified file/stream."

public void testDumpWriter() 
{
   Map<String, Object> data = new HashMap<String, Object>();
   data.put("name", "Silenthand Olleander");
   data.put("race", "Human");
   data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
   Yaml yaml = new Yaml();
   StringWriter writer = new StringWriter();
   yaml.dump(data, writer);
   System.out.println(writer.toString());
}

But still, even though it says exactly that at the second bit of code, it doesn't seem to support the manipulation of a certain file and it's certainly not shown how to do it.

Is it only me or does the documentation feel very cryptic and specified? Half of it is about special applications that I have never even heard about. I feel really dumb just by looking at it and it makes me kind of angry.

Anyhow; I would really appreciate any help that you could give me.

like image 314
Jonas Bartkowski Avatar asked Jul 25 '14 06:07

Jonas Bartkowski


People also ask

How do I write a Yaml script?

The pre-requisites of this tutorial include basic knowledge of HTML, XML and JSON. YAML was specifically created to work well for common use cases such as configuration files, log files and cross language sharing files and data sharing.

What is Yaml SnakeYAML?

SnakeYAML is a YAML-parsing library with a high-level API for serialization and deserialization of YAML documents. The entry point for SnakeYAML is the Yaml class, similar to how the ObjectMapper class is the entry point in Jackson.

What is a Yaml file in Java?

yml) File: YAML is a configuration language. Languages like Python, Ruby, Java heavily use it for configuring the various properties while developing the applications. If you have ever used Elastic Search instance and MongoDB database, both of these applications use YAML(. yml) as their default configuration format.

How do I read a YML file value?

If your key is loginUrl (inside your yaml file), you can inject its value with the @Value annotation, inside a Spring component. @Value("${loginUrl}") private String loginUrl; If it's a second level property, the path is @Value("${yourFirstKey. loginUrl}") .


2 Answers

Else one way to write data

 Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");

        YamlWriter writer = new YamlWriter(new FileWriter("test.yml"));
        writer.write(map);
        writer.close();

Output:

  key1: value1
  key2: value2
  key3: value3

For me, in this case, I see a more readable output. From the first solution, we will see output like this:

{key1: value1, key2: value2, key3: value3}

If we will have a lot of data it will be hard to read

P.S. we need some dependency

    <dependency>
        <groupId>com.esotericsoftware.yamlbeans</groupId>
        <artifactId>yamlbeans</artifactId>
        <version>1.13</version>
    </dependency>
like image 119
Alexandr Kovalenko Avatar answered Sep 20 '22 15:09

Alexandr Kovalenko


To pretty print in expected yaml format (without the braces, like in accepted answer):

public static void saveYamlToFile(final Object object) throws IOException {
    final DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setPrettyFlow(true);
    final Yaml yaml = new Yaml(options);

    final FileWriter writer = new FileWriter("/path/to/file.yaml");
    yaml.dump(object, writer);
}
like image 22
Herberts Markūns Avatar answered Sep 19 '22 15:09

Herberts Markūns