README
r/yaml.js
Standalone JavaScript RAML 1.0/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. RAML Specification
How to use
Install module:
npm install ryaml
Use it:
RYAML = require('ryaml');
// parse RYAML string
nativeObject = RYAML.parse(ryamlString);
// Generate YAML
yamlString = RYAML.stringify(nativeObject, 4);
// Load yaml file using RYAML.load
nativeObject = RYAML.load('myfile.raml');
Command line tools
You can enable the command line tools by installing yamljs as a global module:
npm install -g ryaml
Then, two cli commands should become available: ryaml2json and json2yaml. They let you convert RYAML to JSON and JSON to YAML very easily.
ryaml2json
usage: ryaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input
Positional arguments:
input RYAML file or directory containing RYAML 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
# Convert YAML to JSON and output resulting JSON on the console
ryaml2json myfile.yml
# Store output inside a JSON file
ryaml2json myfile.yml > ouput.json
# Output "pretty" (indented) JSON
ryaml2json myfile.yml --pretty
# Save the output inside a file called myfile.json
ryaml2json myfile.yml --pretty --save
# Watch a full directory and convert any YAML file into its JSON equivalent
ryaml2json mydirectory --pretty --save --recursive
# Convert JSON to YAML and store output inside a JSON file
json2yaml myfile.json > ouput.yml
# Output YAML that will be inlined only after 8 levels of indentation
json2yaml myfile.json --depth 8
# Save the output inside a file called myfile.json with 4 spaces for each indentation
json2yaml myfile.json --indentation 4
# Watch a full directory and convert any JSON file into its YAML equivalent
json2yaml mydirectory --pretty --save --recursive