TOML 解析與序列化
在 Github 上編輯
TOML 是一種廣泛使用的組態語言,旨在功能豐富且直觀易寫。
import { parse, stringify } from "jsr:@std/toml";
若要解析 TOML 字串,您可以使用標準函式庫的 TOML parse 函式。該值會以 JavaScript 物件的形式傳回。
const text = `
int = 1_000_000
bool = true
[[bin]]
name = "deno"
path = "cli/main.rs"
[[bin]]
name = "deno_core"
path = "src/foo.rs"
`;
const data = parse(text);
console.log(data.int);
console.log(data.bin.length);
若要將 JavaScript 物件轉換為 TOML 字串,您可以使用標準函式庫的 TOML stringify 函式。
const obj = {
ping: "pong",
complex: [
{ name: "bob", age: 10 },
{ name: "alice", age: 12 },
],
};
const toml = stringify(obj);
console.log(toml);
// ping = "pong"
//
// [[complex]]
// name = "bob"
// age = 10
//
// [[complex]]
// name = "alice"
// age = 12