[ANN] @glennsl/rescript-json-combinators (aka bs-json 2.0)

These have been out for a while already, but still I’m happy to announce @glennsl/rescript-json-combinators, the spiritual successor to bs-json, a combinator library for JSON decoding and encoding.

rescript-json-combinators is based on the same design principles as bs-json, but takes some of the lessons learned and improvements to the compiler and standard library since it was created to make it even faster, safer and more ergonomic. This means there are a couple of breaking changes, but it should still be very familiar if you’ve used bs-json before.

The biggest changes are:

  • A new API for decoding objects
  • A result based API (result didn’t even exist when bs-json was created :laughing:)

A small example to illustrate both:

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

module Decode = {
  open Json.Decode

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

let json = `{ "x": 1, "y": -4 }`

switch json->Json.parseExn->Json.decode(Decode.point) {
| Ok({x, y}) => Js.log(`Got point - x: ${x}, y: ¢{y}`)
| Error(err) => Js.log(err)
}
16 Likes

Wow, I thought it was with us from the beginning :exploding_head:

1 Like

There wasn’t a proper result until the compiler was rebased on top of OCaml 4.06. Before that there was something like a Js.Result.t I think, but it wasn’t entirely clear how that would interoperate with the actual result type when that was later added, or with code that was meant to target 4.06+ native, so library authors tended to avoid it. I’m pretty sure bs-json pre-dates that one as well though.

2 Likes