解析和序列化 YAML
在 Github 上编辑
YAML 是一种广泛使用的数据序列化语言,旨在易于人类阅读和编写。
import { parse, stringify } from "jsr:@std/yaml";
要解析 YAML 字符串,您可以使用标准库的 YAML parse 函数。该值将作为 JavaScript 对象返回。
const text = `
foo: bar
baz:
- qux
- quux
`;
const data = parse(text);
console.log(data.foo);
console.log(data.baz.length);
要将 JavaScript 对象转换为 YAML 字符串,您可以使用标准库的 YAML stringify 函数。
const obj = {
hello: "world",
numbers: [1, 2, 3],
};
const yaml = stringify(obj);
console.log(yaml);
// hello: word
// numbers:
// - 1
// - 2
// - 3