deno.com

解析和序列化 TOML

在 Github 上编辑

TOML 是一种被广泛使用的配置语言,旨在功能丰富且直观易写。

import { parse, stringify } from "jsr:@std/toml";
要解析 TOML 字符串,您可以使用标准库的 TOML 解析函数。该值将作为 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 字符串化函数。
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

使用 Deno CLI 在本地运行 此示例

deno run https://docs.deno.org.cn/examples/scripts/parsing_serializing_toml.ts

您找到所需的内容了吗?

隐私政策