1 个不稳定版本

0.1.0 2023年11月26日

2560解析器实现

MIT 许可证

32KB
673

fson

FSON (灵活序列化对象符号) 是一种用于配置的JSON扩展。用户可以使用 引用模板字符串 快速配置配置。

特性

注释和新值

/* Multiline comment */ [null, NaN, Infinity, -Infinity, 0x1ABC /*Hexadecimal*/] // Single comment

标识符

  • 您可以使用标识符,无需引号,也可以使用单引号
{
    "double quotes": null,
    'single quotes': null,
    withoutQuotes: null
}

引用

  • 一个 引用 是一个可以在任何地方(它可以是对象的成对或它可以在数组中)的对象,可以使用其标识符或路径来引用它。例如
{
  something: {
      key: #{ #id: "identifier"; #value: "value"; }
  }
}
  • 上面示例中的 引用 可以通过两种方式引用
    • 使用其标识符: #"identifier"#identifier
    • 使用其路径: #/something/identifier#/"something"/"identifier"

模板字符串

  • 模板字符串 是用反引号包围的字符串。它们允许您使用 ${value} 在其中嵌入其他值(包括 引用)。例如
    {
      x: 5,
      something: `x is ${x}`
    }
    

其他

  • 对象和数组可以有尾随逗号: { x: { y: [], }, }
  • 数字可以以加号开头: +1.5
  • 字符串可以是多行的
"hello
world"
  • 空白不重要

示例

查看所有示例请访问示例目录。
如何运行示例: cargo run --example EXAMPLE_NAME

编译为WebAssembly

FSON 已准备好编译为 WebAssembly 并已包含必要的函数。仅在编译为 WebAssembly 时使用 js-syswasm-bindgen 库和函数。
使用以下命令编译为wasm

# Install wasm-pack
cargo install wasm-pack

# Compile to wasm
wasm-pack build --target web
JavaScript示例
import init, { parse, stringify } from "./jsonparser.js";
init().then(() => {
  console.log(stringify({
    // Creating reference
    x: {
      "#id": "test",
      "#value": "value",
    },

    // Using reference
    y: [
      // Identifier
      { "#reference_id": "test" },

      // Path
      { "#reference_path": ["x"] },

      // Template string
      {
        "@template_string": [
          "test is ",
          { "#reference_id": "test" }, /* Reference */
          "; 2 + 2 = ",
          4, /* Normal value */
        ],
      },
    ],
  }));
});

许可证

MIT

依赖

~2–3MB
~59K SLoC