Hướng dẫn dùng js yaml python
Show Nội dung chính
Online Demo This is an implementation of YAML, a human-friendly data serialization language. Started as PyYAML port, it was completely rewritten from scratch. Now it's very fast, and supports 1.2 spec. InstallationYAML module for node.js
CLI executableIf you want to inspect your YAML files from CLI, install js-yaml globally:
Usage
APIHere we cover the most 'useful' methods. If you need advanced details (creating your own tags), see examples for more info. const yaml = require('js-yaml'); const fs = require('fs'); // Get document, or throw exception on error try { const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8')); console.log(doc); } catch (e) { console.log(e); } load (string [ , options ])Parses options:
NOTE: This function does not understand multi-document sources, it throws exception on those. NOTE: JS-YAML does not support schema-specific tag resolution restrictions. So, the JSON schema is not as strictly defined in the YAML specification.
It allows numbers in any notation, use loadAll (string [, iterator] [, options ])Same as const yaml = require('js-yaml'); yaml.loadAll(data, function (doc) { console.log(doc); }); dump (object [ , options ])Serializes options:
The following table show availlable styles (e.g. "canonical", "binary"...) available for each tag (.e.g. !!null, !!int ...). Yaml output is shown on the right side after
Example: dump(object, { 'styles': { '!!null': 'canonical' // dump null as ~ }, 'sortKeys': true // sort object keys }); Supported YAML typesThe list of standard YAML tags and corresponding JavaScript types. See also YAML tag discussion and YAML types repository.
JavaScript-specific tags See js-yaml-js-types for extra types. CaveatsNote, that you use arrays or objects as key in JS-YAML. JS does not allow objects or arrays as keys, and stringifies (by calling --- ? [ foo, bar ] : - baz ? { foo: bar } : - baz - baz { "foo,bar": ["baz"], "[object Object]": ["baz", "baz"] } Also, reading of properties on implicit block mapping keys is not supported yet. So, the following YAML document cannot be loaded. &anchor foo: foo: bar *anchor: duplicate key baz: bat *anchor: duplicate key js-yaml for enterpriseAvailable as part of the Tidelift Subscription The maintainers of js-yaml and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more. |