Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Comment to a Node With SnakeYaml

As part of a large project I am working on, I am constructing a YAML file in memory, and then dumping it to a file, using SnakeYaml in this entire process. My YAML file is around 500 lines long and meant to be used by people who have no programming experience whatsoever, so comments on most nodes is a must. My past three hours have been spent staring at the SnakeYaml Documentation, as well as Googling a fair bit, but with no results. I can conclude one of three things:

  1. I may have missed it completely.
  2. Its not there (unlikely, if it does exists).
  3. Its not possible.

I'm leaning towards the third, but before I write my own YAML dumper, I wished to ask the StackOverflow community whether this is possible? So, is it? Something among the lines of yamlfile.setComment("a.random.node", "Its a random node!"); would be bliss, but it doesn't matter to me if it isn't anywhere close to as simple.

In the case that it is not, could someone give me some pointers as to what I should do to accomplish this?

like image 474
Xyene Avatar asked Oct 22 '12 01:10

Xyene


People also ask

Can YAML have comments?

YAML supports single line comments. # this is single line comment.

How do you add a comment in YAML?

In order to add comments to a YAML file, you simply have to use the # (hashtag symbol) at the start of the line. For example, below is an example of one commented line in a YAML file.

What is SnakeYAML used for?

SnakeYAML allows you to read a YAML file into a simple Map object or parse the file and convert it into a custom Java object. Depending on your requirements you can decide in which format you want to read your YAML files.

Is SnakeYAML thread safe?

Since the implementation is not thread safe, different threads must have their own Yaml instance.


1 Answers

Since the comments are not part of the representation tree, they are ignored by SnakeYAML. Since a YAML document is nothing but a text document, any Template processor can be used.

like image 71
Andrey Avatar answered Sep 19 '22 08:09

Andrey