Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
npm install yamljsyaml.js
=======
Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
Mainly inspired from Symfony Yaml Component.
How to use
----------
Import yaml.js in your html page:
`` html`
Parse yaml string:
` js`
nativeObject = YAML.parse(yamlString);
Dump native object into yaml string:
` js`
yamlString = YAML.stringify(nativeObject[, inline / @integer depth to start using inline notation at /[, spaces / @integer number of spaces to use for indentation /] ]);
Load yaml file:
` js`
nativeObject = YAML.load('file.yml');
Load yaml file:
` js`
YAML.load('file.yml', function(result)
{
nativeObject = result;
});
Use with node.js
----------------
Install module:
` bash`
npm install yamljs
Use it:
` js
YAML = require('yamljs');
// parse YAML string
nativeObject = YAML.parse(yamlString);
// Generate YAML
yamlString = YAML.stringify(nativeObject, 4);
// Load yaml file using YAML.load
nativeObject = YAML.load('myfile.yml');
`
Command line tools
------------------
You can enable the command line tools by installing yamljs as a global module:
` bash`
npm install -g yamljs
Then, two cli commands should become available: yaml2json and json2yaml. They let you convert YAML to JSON and JSON to YAML very easily.
yaml2json
`
usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input
Positional arguments:
input YAML file or directory containing YAML files.
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-p, --pretty Output pretty (indented) JSON.
-i INDENTATION, --indentation INDENTATION
Number of space characters used to indent code (use
with --pretty, default: 2).
-s, --save Save output inside JSON file(s) with the same name.
-r, --recursive If the input is a directory, also find YAML files in
sub-directories recursively.
-w, --watch Watch for changes.
`
json2yaml
`
usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input
Positional arguments:
input JSON file or directory containing JSON files.
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-d DEPTH, --depth DEPTH
Set minimum level of depth before generating inline
YAML (default: 2).
-i INDENTATION, --indentation INDENTATION
Number of space characters used to indent code
(default: 2).
-s, --save Save output inside YML file(s) with the same name.
-r, --recursive If the input is a directory, also find JSON files in
sub-directories recursively.
-w, --watch Watch for changes.
`
examples
`` bash