Skip to content
Bucklescript PPX which generates JSON (de)serializers for user-defined types
OCaml JavaScript Shell
Branch: master
Clone or download
Latest commit da69e10 Aug 10, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
__tests__ Belt.Result.t support Aug 10, 2019
ppx Belt.Result.t support Aug 10, 2019
src
.gitignore Fix publishing exe Apr 24, 2019
.travis.yml deploy on tags May 19, 2019
.yarnrc travis, 5.0.0 Apr 21, 2019
CHANGELOG.md 0.1.0 Aug 10, 2019
LICENSE Initial commit Jun 24, 2018
README.md Update ppx-flags argument May 29, 2019
bsconfig.json scoped package Jan 21, 2019
package.json 0.1.0 Aug 10, 2019
yarn.lock Upgrade deps Aug 10, 2019

README.md

ppx_decco

What is it?

Bucklescript PPX which generates JSON serializers and deserializers for user-defined types.

Example:

/* Define types */
[@decco] type variant('a) = A | B(int) | C(int, 'a);
type dict = Js.Dict.t(string);
[@decco] type mytype = {
    s: string,
    i: int,
    o: option(int),
    complex: array(option(list(variant(string)))),
    [@decco.default 1.0] f: float,
    [@decco.key "other_key"] otherKey: string,
    magic: [@decco.codec Decco.Codecs.magic] dict,
};

/* Use <typename>_encode to encode */
let encoded = mytype_encode({
    s: "hello",
    i: 12,
    o: None,
    complex: [| Some([ C(25, "bullseye") ]) |],
    f: 13.,
    otherKey: "other",
    magic: Js.Dict.fromArray([|("key","value")|]),
});

Js.log(Js.Json.stringifyWithSpace(encoded, 2));
/* {
     "s": "hello",
     "i": 12,
     "o": null,
     "complex": [ [ ["C", 25, "bullseye"] ] ],
     "f": 13,
     "other_key": "other",
     "magic": { "key": "value" }
  } */

/* Use <typename>_decode to decode */
let { s, i, o, complex, f, otherKey, magic } =
    mytype_decode(encoded)
    |> Belt.Result.getExn;

What state is it in?

I've been using it for personal projects for a while, but it's never been used for anything important, so YMMV.

How do I install it?

  1. Install package
npm i @ryb73/decco
  1. Update your bsconfig.json
{
  ...,
  "bs-dependencies": [ "@ryb73/decco" ],
  "ppx-flags": [ "@ryb73/decco/ppx/ppx_decco.sh" ],
  ...
}

Adding ppx_decco to ppx-flags will enable the PPX. Adding decco to bs-dependencies is required because the code generated by the PPX references the Decco module.

How do I use it?

See __tests__/test.re for some examples.

You can’t perform that action at this time.