I'm not entirely sure what you are trying to do with targetValue
here, but the type constructor for a decoder is Json.Decode.t
, and only takes one argument, the type it will produce. Then there's a function Json.Decode.decode
, which is also available through Json.decode
, that takes a decoder and a Js.Json.t
value, and runs the decoder on that value to produce a result
that, if successful, will contain the decoded value.
Here's an abbreviated version of the README example with some type annotations to hopefully illustrate it better:
type point = {
x: int,
y: int,
}
module Decode = {
open Json.Decode
let point: t<point> = object(field => {
x: field.required(. "x", int),
y: field.required(. "y", int),
})
}
let result: result<point, string> =
Json.decode(json, Decode.point)
switch result {
| Ok(point) =>
let {x, y} = point
Js.log(`Got point - x: ${x}, y: ยข{y}`)
| Error(err) => Js.log(err)
}