0

Trying to use decode from rescript-json-combinators library, it is mentionned that Decoders have been made abstract and they have to be run via Decode.decode

let targetValue: JsonCombinators.Json.Decode.decode<Web.Json.t, string>

Trying to use this way raises this error: This type constructor, JsonCombinators.Json.Decode.decode, can't be found.

Is this the correct way to use it? if not any idea on how this error can be fixed?

Fer iel
  • 23
  • 4

1 Answers1

0

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)
}
glennsl
  • 28,186
  • 12
  • 57
  • 75