npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decco

v1.6.0

Published

Bucklescript PPX which generates JSON (de)serializers for user-defined types

Downloads

3,347

Readme

Decco

Project Status

Decco is not being actively maintained, but PRs will be accepted and appreciated.

What is it?

A 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;

How do I install it?

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

Adding decco/ppx 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.

Note: If you need to use decco with BuckleScript 5, install @ryb73/decco version ^0.1.0 by following the old ReadMe here.

How do I use it?

See test.re for some examples.

Reference

Attributes

[@decco]

Applies to: type declarations, type signatures

Indicates that an encoder and decoder should be generated for the given type.

[@decco.encode]

Applies to: type declarations, type signatures

Indicates than an encoder (but no decoder) should be generated for the given type.

[@decco.decode]

Applies to: type declarations, type signatures

Indicates than an decoder (but no encoder) should be generated for the given type.

[@decco.codec]

Applies to: type expressions

Specifies custom encoders and decoders for the type. Note that both an encoder and decoder must be specified, even if the type expression is within a type for which [@decco.encode] or [@decco.decode] was specified.

[@decco] type t = [@decco.codec (fancyEncoder, fancyDecoder)] fancyType;

[@decco.key]

Applies to: record fields

By default, Reason record fields map to JS object fields of the same name. Use [@decco.key] to specify a custom JS field name. Useful if the JS field name is invalid as a Reason record field name.

[@decco]
type record = {
    [@decco.key "other_key"] otherKey: string,
};

[@decco.default]

Applies to: record fields Default: Js.Json.null

When decoding a record, the default value will be used for keys that are missing from the JSON object being decoded.

[@decco] type record = {
    [@decco.default "def"] s: string,
};

let {s} = Js.Json.parseExn("{}") |> record_decode |> Belt.Result.getExn;
Js.log(s); /* def */

Contributing

Please read the CONTRIBUTING.md