@fregante/relaxed-json

中文文档
2.0.0发布于 2024/08/25BSD-3-ClauseCommonJS零依赖近 30 天下载 149.1万

Relaxed JSON is strict superset JSON, relaxing strictness of valilla JSON

原始使用文档
文档目录3 节
  1. API
  2. Changelog
  3. Related projects

Fork of Relaxed JSON without CLI

npm install @fregante/relaxed-json
const RJSON = require('@fregante/relaxed-json');
RJSON.transform('{ "foo": "bar" }');
// => '{"foo":"bar"}'

RJSON.parse('{ "foo": "bar" }');
// => { foo: 'bar' }

RJSON.stringify({ foo: 'bar' });
// => '{"foo":"bar"}'

Relaxed JSON

NPM version

Are you frustrated that you cannot add comments into your configuration JSON Relaxed JSON is a simple solution. Small JavaScript library with only one exposed function RJSON.transform(text : string) : string (and few convenient helpers).

Relaxed JSON (modified BSD license) is a strict superset of JSON, relaxing strictness of vanilla JSON. Valid, vanilla JSON will not be changed by RJSON.transform. But there are few additional features helping writing JSON by hand.

  • Comments are stripped : // foo and /* bar */ → . Comments are converted into whitespace, so your formatting is preserved.
  • Trailing comma is allowed : [1, 2, 3, ] → [1, 2, 3]. Works also in objects { "foo": "bar", } → { "foo": "bar" }.
  • Identifiers are transformed into strings : { foo: bar } → { "foo": "bar" }.
  • Single quoted strings are allowed : 'say "Hello"' → "say \"Hello\"".
  • More different characters is supported in identifiers: foo-bar → "foo-bar".

API

  • RJSON.transform(text : string) : string. Transforms Relaxed JSON text into JSON text. Doesn't verify (parse) the JSON, i.e result JSON might be invalid as well
  • RJSON.parse(text : string, reviver : function | opts : obj) : obj. Parse the RJSON text, virtually JSON.parse(JSON.transform(text), reviver). You could pass a reviver function or an options object as the second argument. Supported options:
    • reviver: you could still pass a reviver
    • relaxed: use relaxed version of JSON (default: true)
    • warnings: use relaxed JSON own parser, supports better error messages (default: false)
    • tolerant: wait until the end to throw errors
    • duplicate: fail if there are duplicate keys in objects

Changelog

  • 2.0.0 — 2024-08-25 — Dropped CLI
  • 1.0.1 — 2017-03-08 — Meteor compatibility
  • 1.0.0 — 2015-07-13 — Stable release
    • Forward slashes bug fixed
  • 0.2.9 Dependencies bump
  • 0.2.8 Dev dependencies update
  • 0.2.7 rjson executable
    • also depedencies update
    • jscs style check
  • 0.2.6 Dependencies update
  • 0.2.5 Use make
  • 0.2.4 Maintenance release
  • 0.2.3 Bugfixes
    • $ is valid identifier character
    • single line comments may end with CR and CRLF also
  • 0.2.2 Bugfix
  • 0.2.1 Code reogranization
    • More though into toleration, handles valid json without colons and commas
    • trailing comma stripping is more strict
  • 0.2.0 Shiny new features
    • overloaded rjson.parse
    • tolerating parser support
    • duplicate key warning
    • test suite (!)
  • 0.1.1 RJSON.parse
  • 0.1.0 Initial release

For truly human writable configuration consider using YAML.

版本与发布记录

@fregante/relaxed-json 共发布 1 个版本,首次发布于 2024/08/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2024/08/252 年前

关于 @fregante/relaxed-json 的常见问题

@fregante/relaxed-json 是什么?

Relaxed JSON is strict superset JSON, relaxing strictness of valilla JSON

如何安装 @fregante/relaxed-json?

在项目目录运行 npm install @fregante/relaxed-json,或使用 pnpm add / yarn add / bun add。

@fregante/relaxed-json 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fregante__relaxed-json 是否存在。

@fregante/relaxed-json 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fregante/relaxed-json 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.10.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fregante/relaxed-json 可以商用吗?

@fregante/relaxed-json 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@fregante/relaxed-json 的下载量有多少?

根据 npm 官方下载统计,@fregante/relaxed-json 在 2026/10/16 – 2026/11/14 这 30 天内被下载 1,490,872 次,日均约 49,696 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fregante/relaxed-json 还在维护吗?

最新版本 2.0.0 发布于 2024/08/25(2 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。