0

Trying to create an equivalent for decodeEvent using rescript-json-combinators. I couldn't figure out how to replace "Decoder(decoder)" since Decoders have been made abstract in rescript-json-combinators. Trying to use Decode.decode didn't work. Any idea on how this could be solved?

   let decodeEvent = (Decoder(decoder), value: Web_node.event) =>
    try decoder(Obj.magic(value)) catch {
    | ParseFail(e) => Error(e)
    | _ => Error("Unknown JSON parsing error")
    }
Fer iel
  • 23
  • 4

1 Answers1

0

The purpose of decodeEvent seems to be two-fold:

  1. To convert a Web_node.event to Json.t in order to use Json decoders on it.
  2. To return a result instead of raising an exception on error.

The rescript-json-combinators API already has a result-based API. That's part of the reason why the implementation is now hidden and requires the use of Json.decode to run the decoders. That seems to be all that's missing here:

let decodeEvent = (decoder, value: Web_node.event) =>
  value->Obj.magic->Json.decode(decoder)
glennsl
  • 28,186
  • 12
  • 57
  • 75
  • Trying to do the same thing to this function `let succeed = v => Decoder(_value => Ok(v))` for some reason if I use the expression bellow where the decode function is defined it works, if I try to use it in a different file it says that the expression inside decode should not be a function. `let succeed = v => Json.decode(_value => Ok(v))` Any idea on why this is happening and is there a better equivalent for this function ? Thank you – Fer iel Oct 04 '22 at 19:38
  • 1
    Well, it shouldn't be a function, but a decoder. `Json.decode` is used to _run_ a decoder. `succeed` should _be_ a decoder. Or rather a function that returns a decoder. This is where you need to use `Decode.custom`. Something like `let succeed = v => Decode.custom((. _json) => v)` should work, I think. – glennsl Oct 04 '22 at 20:43