js-yaml is an implementation of YAML written by Dervus Grim and is a human friendly data serialization language. It started as PyYAML port, but was completely rewritten from scratch. Full support for 1.2 spec.
1
|
npm install js-yaml |
Example
You can parse a YAML document into an object:
1
2
3
|
var fs = require( 'fs' ); var yaml = require( 'js-yaml' ); var obj = yaml.load(fs.readFileSync(filename, 'utf8' )); |
And of course you can stringify an object into a YAML document:
1
|
yaml.dump(object, opts); |
YAML to JSON is what CoffeeScript to JavaScript – pure awesomeness. On the surface it’s a one to one data format that is much more human friendly. Under the surface, YAML allows you to reference other nodes and values within the document to avoid repetition. For example, the header for post in the source looks like this:
1
2
3
4
5
|
npm: repo: &repo nodeca/js-yaml name: &name js-yaml slug: *name title: *name |
Where &name
is an anchor and repeated using *name
reference. This is a very simple example, but anchors could point to entire nodes consisting of other references and objects. Checkout js-yaml repo for more examples and explanation.
YAML is awesome! I wish package.json
was package.yaml
. Are you using YAML in your project?