2

Is there a library in a JVM language that allows me to parse an existing YAML document, accomplish a small modification in the file and dump it as another file such as existing comments are preserved?

2
  • No responses in 6 months. Do you insist specifically on a Java library, or will any tool that can do what you ask be useful?
    – Ira Baxter
    Apr 3, 2016 at 4:44
  • In fact a library in any JVM language will do it (edited the question to reflect that). I would like this to be part of an existing tool-chain already written in Java.
    – Sergio
    Apr 3, 2016 at 12:18

1 Answer 1

1

As I understand it Jython runs on the JVM, with that you should be able to use ruamel.yaml¹:

$ python
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11) 
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_111
Type "help", "copyright", "credits" or "license" for more information.
>>> import ruamel.yaml
>>> yaml = ruamel.yaml.YAML()
>>> data = yaml.load("""\
... a:
...   - elem1   # not very insteresting
...   - elem2   # more of the same
... b:
...   - 42
... c: {key: value}   # does it work?
... """)
>>> import sys
>>> yaml.dump(data, sys.stdout)
a:
- elem1     # not very insteresting
- elem2     # more of the same
b:
- 42
c: {key: value}   # does it work?
>>> exit()
$ 

¹ Disclaimer: I am the author of that package.

2
  • Is there a self-contained Maven example?
    – AlexO
    Sep 6, 2017 at 2:29
  • I have no idea what Maven is, only heard about it by name. I am not sure it is appropriate here, but you could consider asking this as a full-fledged question to get some more people's notice.
    – Anthon
    Sep 6, 2017 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.