@glennsl/rescript-json-combinators

中文文档
1.6.0发布于 2026/10/16(LGPL-3.0 OR MPL-2.0)零依赖近 30 天下载 3168

Combinator library for JSON decoding and encoding.

原始使用文档
文档目录14 节
  1. Example
  2. Installation
  3. Differences from bs-json
  4. Documentation
  5. API
  6. License
  7. Changes
  8. 1.6.0
  9. 1.5.0
  10. 1.4.0
  11. 1.3.0
  12. 1.2.0
  13. 1.1.0
  14. 1.0.0

rescript-json-combinators

Combinator library for JSON decoding and encoding.

npm Issues Last Commit

Example

type point = {
  x: int,
  y: int,
}

type polyline = {
  points: array<point>,
  thickness: option<int>,
}

module Decode = {
  open Json.Decode

  let point = object(field => {
    x: field.required(. "x", int),
    y: field.required(. "y", int),
  })

  let polyline = object(field => {
    points: field.required(. "points", array(point)),
    thickness: field.optional(. "thickness", int),
  })
}

let data = `{
  "points": [
    { "x": 1, "y": -4 },
    { "x": 5, "y": 8 }
  ]
}`

let _ = data->Js.Json.parseExn->Json.decode(Decode.polyline)->Js.log

See examples for more.

Installation

npm install --save @glennsl/rescript-json-combinators

Then add @glennsl/rescript-json-combinators to bs-dependencies in your bsconfig.json:

 {
   "bs-dependencies": [
+    "@glennsl/rescript-json-combinators"
   ]
 }

Optionally, add this to automatically open the library in order to use Json.Encode and Json.Decode directly:

 {
   "bsc-flags": [
+    "-open JsonCombinators",
   ]
 }

Differences from bs-json

rescript-json-combinators is the spriritual successor of bs-json. It was rewritten from scratch and not intended to be backwards compatible. And while for the most part is does share the same design principles,there are also some notable differences:

  • Decoders have been made abstract. While the internal representation is still the same, a function that takes a Js.Json.t and returns either the decoded value or raises an exception, they can no longer be called directly. Instead they have to be run via Decode.decode. This is in order to ensure that the DecodeError exceptions don't leak and returns a result instead. Custom decoders can still be created with Decode.custom.

  • A new object decoder has been added, and while it's still possible to decode fields individually in a custom decoder, this addresses a couple ergonomic problems with that approach, such as distinguishing between a field missing and having the wrong type, and the over-use and under-encapsulation of custom decoders.

  • Uncurrying and raw javascript is used in some places to improve performance. For the most part this isn't visible to the end user because decoders can no longer be called directly. There are a couple exceptions however, such as field decoders inside the object deocder, and map.

Documentation

API

For the moment, please see the interface files:

License

This work is dual-licensed under LGPL 3.0 and MPL 2.0. You can choose between one of them if you use this work.

Please see LICENSE.LGPL-3.0 and LICENSE.MPL-2.0 for the full text of each license.

SPDX-License-Identifier: LGPL-3.0 OR MPL-2.0

Changes

1.6.0

  • add Decode.recursive for self-referencing decoders

1.5.0

  • BREAKING: updated to rescript 12

1.4.0

  • explicitly curried encode combinators to reduce breakage in rescript 11 uncurried mode
  • updated examples to rescript 11

1.3.0

  • add Decode.indirect to help with decoding recursive data structures

1.2.0

  • add Decode.flatMap

1.1.0

  • recript v11-compatible formatting (#3)

1.0.0

Initial release

版本与发布记录

@glennsl/rescript-json-combinators 共发布 9 个版本,首次发布于 2022/07/17;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.6.0latest2026/10/161 个月前
1.5.12025/12/0511 个月前
1.5.02025/12/0511 个月前
1.4.02024/02/132 年前
1.3.02023/11/013 年前
1.2.12023/05/293 年前

关于 @glennsl/rescript-json-combinators 的常见问题

@glennsl/rescript-json-combinators 是什么?

Combinator library for JSON decoding and encoding.

如何安装 @glennsl/rescript-json-combinators?

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

@glennsl/rescript-json-combinators 支持 TypeScript 吗?

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

@glennsl/rescript-json-combinators 需要什么版本的 Node.js?

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

@glennsl/rescript-json-combinators 可以商用吗?

@glennsl/rescript-json-combinators 使用 (LGPL-3.0 OR MPL-2.0) 许可。可在 LGPL-3.0、MPL-2.0 中任选其一遵守。以上为概括,不构成法律意见,请以许可原文为准。

@glennsl/rescript-json-combinators 的下载量有多少?

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

@glennsl/rescript-json-combinators 还在维护吗?

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