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
resultbased API (resultdidn’t even exist whenbs-jsonwas created
)
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)
}
