@@ -7,9 +7,9 @@ JS-YAML - YAML 1.2 parser / writer for JavaScript
77__ [ Online Demo] ( https://nodeca.github.io/js-yaml/ ) __
88
99
10- This is an implementation of [ YAML] ( https://yaml.org/ ) , a human-friendly data
11- serialization language. Started as [ PyYAML ] ( https://pyyaml.org/ ) port, it was
12- completely rewritten from scratch. Now it's very fast, and supports 1.2 spec .
10+ A fast and complete [ YAML] ( https://yaml.org/ ) parser and writer for JavaScript.
11+ Supports both the 1.2 and 1.1 specs, and passes the entire
12+ [ YAML Test Suite ] ( https://github.com/yaml/yaml-test-suite ) .
1313
1414
1515Installation
@@ -19,13 +19,14 @@ Installation
1919npm install js-yaml
2020```
2121
22+ Upgrading from v4? See the [ v5 migration guide] ( docs/migrate_v4_to_v5.md ) .
23+
2224
2325API
2426---
2527
26- Here we cover the most 'useful' methods. If you need advanced details (creating
27- your own tags), see [ examples] ( https://github.com/nodeca/js-yaml/tree/master/examples )
28- for more info.
28+ Here we cover the most useful methods. If you need advanced details (such as
29+ creating your own tags), see the [ examples] ( examples/ ) for more info.
2930
3031``` javascript
3132import { load } from ' js-yaml'
@@ -43,38 +44,57 @@ try {
4344
4445### load (string [ , options ] )
4546
46- Parses ` string ` as single YAML document. Throws ` YAMLException ` on error.
47- This function ** does not** understand multi-document and empty sources,
48- it throws exception on those.
47+ Parses ` string ` as a single YAML document. Throws ` YAMLException ` on error.
48+ This function ** does not** understand multi-document or empty sources; it throws
49+ an exception on those.
50+
51+ > [ !WARNING]
52+ > When processing untrusted input, see the
53+ > [ security considerations] ( docs/safety.md ) .
4954
5055options:
5156
5257- ` filename ` _ (default: null)_ - string to be used as a file path in
5358 error/warning messages.
5459- ` schema ` _ (default: ` CORE_SCHEMA ` )_ - specifies a schema to use.
55- - ` FAILSAFE_SCHEMA ` - only strings, arrays and plain objects:
56- - ` JSON_SCHEMA ` - all JSON-supported types:
57- - ` CORE_SCHEMA ` - superset of ` JSON_SCHEMA ` , accepting more notations for the
58- same types
60+ - ` FAILSAFE_SCHEMA ` - only strings, arrays and plain objects.
61+ - ` JSON_SCHEMA ` - all JSON-supported types.
62+ - ` CORE_SCHEMA ` - a superset of ` JSON_SCHEMA ` , accepting more notations for
63+ the same types.
5964 - ` YAML11_SCHEMA ` - adds the legacy YAML 1.1 types (` !!binary ` , ` !!timestamp ` ,
6065 ` !!omap ` , ` !!pairs ` , ` !!set ` , merge keys ` << ` , and the broader 1.1 scalar
6166 notations).
62- - ` json ` _ (default: false)_ - compatibility with JSON.parse behaviour. If true,
63- then duplicate keys in a mapping will override values rather than throwing an
67+ - ` json ` _ (default: false)_ - compatibility with ` JSON.parse ` behaviour. If
68+ ` true ` , duplicate keys in a mapping override values rather than throwing an
6469 error.
65- - ` maxDepth ` _ (default: 100)_ - limits nesting depth for collections (does not
66- take aliasees into account).
70+ - ` maxDepth ` _ (default: 100)_ - limits the nesting depth for collections (does
71+ not take aliases into account).
6772- ` maxMergeSeqLength ` _ (default: 20)_ - limits the number of items in merge
6873 (` << ` ) sequences.
6974
70- NOTE: The default ` CORE_SCHEMA ` goes without ` !!merge ` tag. You can easily
71- enable it if needed:
72-
73- ``` javascript
74- import { load , CORE_SCHEMA , mergeTag } from ' js-yaml'
75-
76- load (data, { schema: CORE_SCHEMA .withTags (mergeTag) })
77- ```
75+ > [ !NOTE]
76+ >
77+ > The default ` CORE_SCHEMA ` comes without the ` !!merge ` tag. You can easily
78+ > enable it if needed:
79+ >
80+ > ``` javascript
81+ > import { load , CORE_SCHEMA , mergeTag } from ' js-yaml'
82+ >
83+ > load (data, { schema: CORE_SCHEMA .withTags (mergeTag) })
84+ > ` ` `
85+
86+ > [!WARNING]
87+ >
88+ > The default ` mapTag` is ` {}` -object based and does not allow complex keys
89+ > (objects, arrays and so on). That's an intentional choice for convenience.
90+ > Also, non-string scalar keys, such as ` null ` , numbers or booleans, are
91+ > converted to strings.
92+ >
93+ > In the rare cases where you really need complex keys, use ` realMapTag` in the
94+ > schema instead. It stores any key exactly as provided, at the cost of less
95+ > convenient access.
96+
97+ See [examples](examples/) for advanced customization approaches.
7898
7999
80100### loadAll (string [, options ])
@@ -92,34 +112,46 @@ console.log(loadAll(data))
92112### dump (object [ , options ] )
93113
94114Serializes ` object ` as a YAML document. By default it can dump every supported
95- YAML type, so it will throw an exception if you try to dump regexps or
96- functions. However, you can disable exceptions by setting the ` skipInvalid `
97- option to ` true ` .
115+ YAML type, so it throws an exception if you try to dump regexps or functions.
116+ However, you can disable exceptions by setting the ` skipInvalid ` option to
117+ ` true ` .
98118
99119options:
100120
101121- ` indent ` _ (default: 2)_ - indentation width to use (in spaces).
102122- ` flowLevel ` _ (default: -1)_ - nesting level at which collections switch from
103123 block to flow style (` -1 ` means never).
104- - ` seqNoIndent ` _ (default: false)_ - when true, will not add an indentation level to array elements.
105- - ` seqInlineFirst ` _ (default: true)_ - when true, allows a nested collection to start on the same line after ` - ` .
106- - ` skipInvalid ` _ (default: false)_ - do not throw on invalid types (like function
107- in the safe schema) and skip pairs and single values with such types.
108- - ` schema ` _ (default: a ` YAML11_SCHEMA ` -based schema)_ specifies a schema to use.
124+ - ` seqNoIndent ` _ (default: false)_ - when ` true ` , does not add an indentation
125+ level to array elements, ` ␣␣- 1 ` => ` - 1 ` .
126+ - ` seqInlineFirst ` _ (default: true)_ - when ` true ` , allows a nested collection
127+ to start on the same line after ` - ` , ` -\n - 1 ` => ` - - 1 ` .
128+ - ` skipInvalid ` _ (default: false)_ - do not throw on invalid types (such as a
129+ function in the schema). Invalid mapping pairs and sequence items are skipped;
130+ ` undefined ` sequence items are serialized as ` null ` .
131+ - ` schema ` _ (default: a ` YAML11_SCHEMA ` -based schema)_ - specifies a schema to
132+ use.
109133- ` sortKeys ` _ (default: ` false ` )_ - if ` true ` , sort keys when dumping YAML. If a
110134 function, use the function to sort the keys.
111- - ` lineWidth ` _ (default: ` 80 ` )_ - set max line width. Set ` -1 ` for unlimited width.
112- - ` noRefs ` _ (default: ` false ` )_ - if ` true ` , don't convert duplicate objects into references
113- - ` quoteStyle ` _ (` auto ` , ` single ` , or ` double ` , default: ` auto ` )_ - preferred quote style when a scalar needs quotes.
114- - ` flowBracketPadding ` _ (default: ` false ` )_ - add spaces inside flow collection brackets.
115- - ` flowSkipCommaSpace ` _ (default: ` false ` )_ - omit the space after commas in flow collections.
116- - ` flowSkipColonSpace ` _ (default: ` false ` )_ - omit the space after ` : ` in flow mappings.
117- - ` quoteFlowKeys ` _ (default: ` false ` )_ - quote flow mapping keys.
118- - ` tagBeforeAnchor ` _ (default: ` false ` )_ - print an explicit tag before an anchor.
135+ - ` lineWidth ` _ (default: ` 80 ` )_ - sets the max line width. Set ` -1 ` for unlimited
136+ width.
137+ - ` noRefs ` _ (default: ` false ` )_ - if ` true ` , don't convert duplicate objects into
138+ references; inline them instead.
139+ - ` quoteStyle ` _ (` auto ` , ` single ` , or ` double ` , default: ` auto ` )_ - force quotes
140+ to single/double, or select the most suitable.
141+ - ` flowBracketPadding ` _ (default: ` false ` )_ - add spaces inside flow collection
142+ brackets, ` {a: 1} ` => ` { a: 1 } ` .
143+ - ` flowSkipCommaSpace ` _ (default: ` false ` )_ - omit the space after commas in
144+ flow collections, ` [1, 2] ` => ` [1,2] ` .
145+ - ` flowSkipColonSpace ` _ (default: ` false ` )_ - omit the space after ` : ` in flow
146+ mappings, ` {a: 1} ` => ` {a:1} ` .
147+ - ` quoteFlowKeys ` _ (default: ` false ` )_ - quote flow mapping keys, ` {a: 1} ` =>
148+ ` {"a": 1} ` .
149+ - ` tagBeforeAnchor ` _ (default: ` false ` )_ - print an explicit tag before an
150+ anchor, ` &ref_0 !!set ` => ` !!set &ref_0 ` .
119151- ` transform ` - a function ` (documents: Document[]) => void ` that can mutate the
120152 generated AST before it is rendered.
121153
122- See [ examples] ( examples ) for advanced customization approaches.
154+ See [ examples] ( examples/ ) for advanced customization approaches.
123155
124156
125157Supported YAML types
@@ -146,7 +178,7 @@ The types below are only available in `YAML11_SCHEMA` (not in the default
146178!!binary '...base64...' # Uint8Array
147179!!timestamp 'YYYY-...' # date
148180!!omap [ ... ] # array of key-value pairs
149- !!pairs [ ... ] # array or array pairs
181+ !!pairs [ ... ] # array of array pairs
150182!!set { ... } # Set
151183```
152184
@@ -156,36 +188,14 @@ See [js-yaml-js-types](https://github.com/nodeca/js-yaml-js-types) for
156188extra types.
157189
158190
159- Caveats
160- -------
161-
162- By default, ` !!map ` is loaded as a plain JavaScript object. Scalar keys that are
163- not strings, such as ` null ` , numbers or booleans, are converted to strings
164- because object property keys are strings. Complex keys, such as arrays or
165- objects, are rejected.
166-
167- ``` yaml
168- ---
169- ? null
170- : empty
171- ? 1
172- : number
173- ? true
174- : boolean
175- ```
176-
177- ``` javascript
178- { " null" : " empty" , " 1" : " number" , " true" : " boolean" }
179- ```
180-
181-
182191CLI
183192---
184193
185- This can be useful sometimes for quick- check.
194+ This can be useful sometimes for a quick check.
186195
187196```
188197npx js-yaml -h
189198```
190199
191- Note, CLI script goes with minimalistic options and without big plans to extend.
200+ Note: the CLI script comes with minimal options, and there are no big plans to
201+ extend it.
0 commit comments