Niche Portobello Mushroom


    0.1.6 • Public • Published


    A ReScript PPX, which generates JSON (de)serializers.

    Spice is originated from

    • The Spice melange in the novel, Dune
    • A flavor for the (polymorphic) variant

    This PPX is highly influenced by Decco and developed with forking the source codes of Decco. Spice has implemented all the features of Decco@1.5.0 and additional useful features for the (polymorphic) variant of its own.


    1. Parse the string instead of the array to the (polymorphic) variant

    To parse the JSON data, Decco is being heavily used in many projects. But, there's a restriction to parse the JSON string into a (polymorphic) variant with the Decco. The JSON data should be formed in an array. It is obvious at some point. But generally, we face the string data which needs to be parsed into the variant in most use cases.

    Whenever it is needed to parse the response in Json, the custom encoder/decoder functions are needed to parse the Json string into the (polymorphic) variant with Decco. But you don't need to write it with the Spice as long as you add in (polymorphic) variants.

    with Decco

    let encoderStatus = v =>
      switch v {
      | WAITING => "waiting"
      | PROCESSING => "processing"
      | SUCCESS => "success"
      | FAIL => "fail"
    let decoderStatus = json => {
      switch json |> Js.Json.classify {
      | Js.Json.JSONString(str) =>
        switch str {
        | "waiting" => WAITING->Ok
        | "processing" => PROCESSING->Ok
        | "success" => SUCCESS->Ok
        | "fail" => FAIL->Ok
        | _ => Error({Decco.path: "", message: "Expected JSONString", value: json})
      | _ => Error({Decco.path: "", message: "Expected JSONString", value: json})
    let codecStatus: Decco.codec<status> = (encoderStatus, decoderStatus)
    type data = {
      status: @decco.codec(codecStatus) status,

    with Spice

    type status =
      |"waiting") WAITING
      |"processing") PROCESSING
      |"success") SUCCESS
      |"fail") FAIL
    type data = {
      status: status,
    1. Parse/stringify the Unicode string

    There are many cases to parse and stringify the string data into (polymorphic) variants. Furthermore, the Unicode string needs to be handled with a variant. Currently, pattern matching is not working for the Unicode string in ReScript, the Spice is using if ... then ... else to compare the Unicode string in case of adding attribute.


    1. Variant
    type t = |`하나`) One |`second`) Two
    // automatically generated
    let t_encode = ...
    // automatically generated
    let t_decode = ...
    let encoded = One->t_encode // Js.Json.JSONString(`하나`)
    let decoded = Js.Json.string(`second`)->t_decode // Belt.Result.Ok(Two)
    1. Record
    type t = {
      @spice.key("spice-label") label: string,
      @spice.key("spice-value") value: int,
    let sample = Js.Dict.empty()
    sample->Js.Dict.set("spice-label", Js.Json.string("sample"))
    sample->Js.Dict.set("spice-value", Js.Json.number(1.0))
    let sampleJson = sample->Js.Json.object_
    let sampleRecord: t = {
      label: "sample",
      value: 1,
    let encoded = sampleRecord->Records.t_encode // sampleJson
    let decoded = sampleJson->Records.t_decode // Belt.Result.Ok(sampleRecord)


    yarn add -D @greenlabs/ppx-spice
    // bsconfig.json
    "bs-dependencies": [
    "ppx-flags": [


    With Esy

    1. Install dependencies
    cd src
    esy install
    1. Build
    (make sure in /src directory)
    esy build
    1. Test
    cd test
    (install dependencies)
    (build --watch)
    yarn res:clean && yarn res:watch
    (run test --watch)
    yarn test:watch

    With Dune

    1. Create a sandbox with opam
    opam switch create spice 4.12.1
    1. Install dependencies
    opam install dune ppxlib ocaml-lsp-server ocamlformat ocp-indent
    1. Build
    dune build
    1. Test
    cd test
    (install dependencies)
    (build --watch)
    yarn res:clean && yarn res:watch
    (run test --watch)
    yarn test:watch



    npm i @greenlabs/ppx-spice

    DownloadsWeekly Downloads






    Unpacked Size

    27.9 MB

    Total Files


    Last publish


    • minukang
    • gyeop
    • miryangjung
    • namenu
    • moondaddi
    • yousleepwhen
    • kimchhickey