Combinator library for JSON decoding and encoding.
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.
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",
]
}
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 viaDecode.decode
. This is in order to ensure that theDecodeError
exceptions don't leak and returns aresult
instead. Custom decoders can still be created withDecode.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, andmap
.
For the moment, please see the interface files:
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
- explicitly curried encode combinators to reduce breakage in rescript 11 uncurried mode
- updated examples to rescript 11
- add
Decode.indirect
to help with decoding recursive data structures
- add
Decode.flatMap
- recript v11-compatible formatting (#3)
Initial release