Home

Awesome

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:

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.4.0

1.3.0

1.2.0

1.1.0

1.0.0

Initial release