1 JS-YAML - YAML 1.2 parser and serializer for JavaScript
2 =======================================================
4 [](https://travis-ci.org/nodeca/js-yaml)
5 [](https://www.npmjs.org/package/js-yaml)
7 [Online Demo](http://nodeca.github.com/js-yaml/)
10 This is an implementation of [YAML](http://yaml.org/), a human friendly data
11 serialization language. Started as [PyYAML](http://pyyaml.org/) port, it was
12 completely rewritten from scratch. Now it's very fast, and supports 1.2 spec.
18 ### YAML module for node.js
27 If you want to inspect your YAML files from CLI, install js-yaml globally:
30 npm install -g js-yaml
36 usage: js-yaml [-h] [-v] [-c] [-t] file
39 file File with YAML document(s)
42 -h, --help Show this help message and exit.
43 -v, --version Show program's version number and exit.
44 -c, --compact Display errors in compact mode
45 -t, --trace Show stack trace on error
49 ### Bundled YAML library for browsers
52 <!-- esprima required only for !!js/function -->
53 <script src="esprima.js"></script>
54 <script src="js-yaml.min.js"></script>
55 <script type="text/javascript">
56 var doc = jsyaml.load('greeting: hello\nname: world');
60 Browser support was done mostly for online demo. If you find any errors - feel
61 free to send pull requests with fixes. Also note, that IE and other old browsers
62 needs [es5-shims](https://github.com/kriskowal/es5-shim) to operate.
66 1. We have no resourses to support browserified version. Don't expect it to be
67 well tested. Don't expect fast fixes if something goes wrong there.
68 2. `!!js/function` in browser bundle will not work by default. If you really need
69 it - load `esprima` parser first (via amd or directly).
70 3. `!!bin` in browser will return `Array`, because browsers do not support
71 node.js `Buffer` and adding Buffer shims is completely useless on practice.
77 Here we cover the most 'useful' methods. If you need advanced details (creating
78 your own tags), see [wiki](https://github.com/nodeca/js-yaml/wiki) and
79 [examples](https://github.com/nodeca/js-yaml/tree/master/examples) for more
83 yaml = require('js-yaml');
86 // Get document, or throw exception on error
88 var doc = yaml.safeLoad(fs.readFileSync('/home/ixti/example.yml', 'utf8'));
96 ### safeLoad (string [ , options ])
98 **Recommended loading way.** Parses `string` as single YAML document. Returns a JavaScript
99 object or throws `YAMLException` on error. By default, does not support regexps,
100 functions and undefined. This method is safe for untrusted data.
104 - `filename` _(default: null)_ - string to be used as a file path in
105 error/warning messages.
106 - `onWarning` _(default: null)_ - function to call on warning messages.
107 Loader will throw on warnings if this function is not provided.
108 - `schema` _(default: `DEFAULT_SAFE_SCHEMA`)_ - specifies a schema to use.
109 - `FAILSAFE_SCHEMA` - only strings, arrays and plain objects:
110 http://www.yaml.org/spec/1.2/spec.html#id2802346
111 - `JSON_SCHEMA` - all JSON-supported types:
112 http://www.yaml.org/spec/1.2/spec.html#id2803231
113 - `CORE_SCHEMA` - same as `JSON_SCHEMA`:
114 http://www.yaml.org/spec/1.2/spec.html#id2804923
115 - `DEFAULT_SAFE_SCHEMA` - all supported YAML types, without unsafe ones
116 (`!!js/undefined`, `!!js/regexp` and `!!js/function`):
117 http://yaml.org/type/
118 - `DEFAULT_FULL_SCHEMA` - all supported YAML types.
120 NOTE: This function **does not** understand multi-document sources, it throws
123 NOTE: JS-YAML **does not** support schema-specific tag resolution restrictions.
124 So, JSON schema is not as strict as defined in the YAML specification.
125 It allows numbers in any notaion, use `Null` and `NULL` as `null`, etc.
126 Core schema also has no such restrictions. It allows binary notation for integers.
129 ### load (string [ , options ])
131 **Use with care with untrusted sources**. The same as `safeLoad()` but uses
132 `DEFAULT_FULL_SCHEMA` by default - adds some JavaScript-specific types:
133 `!!js/function`, `!!js/regexp` and `!!js/undefined`. For untrusted sources you
134 must additionally validate object structure, to avoid injections:
137 var untrusted_code = '"toString": !<tag:yaml.org,2002:js/function> "function (){very_evil_thing();}"';
139 // I'm just converting that string, what could possibly go wrong?
140 require('js-yaml').load(untrusted_code) + ''
144 ### safeLoadAll (string, iterator [ , options ])
146 Same as `safeLoad()`, but understands multi-document sources and apply
147 `iterator` to each document.
150 var yaml = require('js-yaml');
152 yaml.safeLoadAll(data, function (doc) {
158 ### loadAll (string, iterator [ , options ])
160 Same as `safeLoadAll()` but uses `DEFAULT_FULL_SCHEMA` by default.
163 ### safeDump (object [ , options ])
165 Serializes `object` as YAML document. Uses `DEFAULT_SAFE_SCHEMA`, so it will
166 throw exception if you try to dump regexps or functions. However, you can
167 disable exceptions by `skipInvalid` option.
171 - `indent` _(default: 2)_ - indentation width to use (in spaces).
172 - `skipInvalid` _(default: false)_ - do not throw on invalid types (like function
173 in the safe schema) and skip pairs and single values with such types.
174 - `flowLevel` (default: -1) - specifies level of nesting, when to switch from
175 block to flow style for collections. -1 means block style everwhere
176 - `styles` - "tag" => "style" map. Each tag may have own set of styles.
177 - `schema` _(default: `DEFAULT_SAFE_SCHEMA`)_ specifies a schema to use.
186 "binary" => "0b1", "0b101010", "0b1110001111010"
187 "octal" => "01", "052", "016172"
188 "decimal" => "1", "42", "7290"
189 "hexadecimal" => "0x1", "0x2A", "0x1C7A"
191 !!null, !!bool, !!float
192 "lowercase" => "null", "true", "false", ".nan", '.inf'
193 "uppercase" => "NULL", "TRUE", "FALSE", ".NAN", '.INF'
194 "camelcase" => "Null", "True", "False", ".NaN", '.Inf'
197 By default, !!int uses `decimal`, and !!null, !!bool, !!float use `lowercase`.
201 ### dump (object [ , options ])
203 Same as `safeDump()` but without limits (uses `DEFAULT_FULL_SCHEMA` by default).
209 The list of standard YAML tags and corresponding JavaScipt types. See also
210 [YAML tag discussion](http://pyyaml.org/wiki/YAMLTagDiscussion) and
211 [YAML types repository](http://yaml.org/type/).
216 !!int '3...' # number
217 !!float '3.14...' # number
218 !!binary '...base64...' # buffer
219 !!timestamp 'YYYY-...' # date
220 !!omap [ ... ] # array of key-value pairs
221 !!pairs [ ... ] # array or array pairs
222 !!set { ... } # array of objects with given keys and null values
224 !!seq [ ... ] # array
225 !!map { ... } # object
228 **JavaScript-specific tags**
231 !!js/regexp /pattern/gim # RegExp
232 !!js/undefined '' # Undefined
233 !!js/function 'function () {...}' # Function
239 Note, that you use arrays or objects as key in JS-YAML. JS do not allows objects
240 or array as keys, and stringifies (by calling .toString method) them at the
241 moment of adding them.
253 { "foo,bar": ["baz"], "[object Object]": ["baz", "baz"] }
256 Also, reading of properties on implicit block mapping keys is not supported yet.
257 So, the following YAML document cannot be loaded.
262 *anchor: duplicate key
264 *anchor: duplicate key
268 Breaking changes in 2.x.x -> 3.x.x
269 ----------------------------------
271 If your have not used __custom__ tags or loader classes and not loaded yaml
272 files via `require()` - no changes needed. Just upgrade library.
274 In other case, you should:
276 1. Replace all occurences of `require('xxxx.yml')` by `fs.readFileSync()` +
278 2. rewrite your custom tags constructors and custom loader
279 classes, to conform new API. See
280 [examples](https://github.com/nodeca/js-yaml/tree/master/examples) and
281 [wiki](https://github.com/nodeca/js-yaml/wiki) for details.
287 View the [LICENSE](https://github.com/nodeca/js-yaml/blob/master/LICENSE) file