rescriptbs-json

Trying to decode an event using rescript-json-combinators


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")
    }

Solution

  • 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)