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

@glennsl/rescript-json-combinators

v1.4.0

Published

Combinator library for JSON decoding and encoding.

Downloads

4,565

Readme

rescript-json-combinators

Combinator library for JSON decoding and encoding.

npm Issues Last Commit

Example

type point = {
  x: int,
  y: int,
}

type polyline = {
  points: array<point>,
  thickness: option<int>,
}

module Decode = {
  open Json.Decode

  let point = object(field => {
    x: field.required(. "x", int),
    y: field.required(. "y", int),
  })

  let polyline = object(field => {
    points: field.required(. "points", array(point)),
    thickness: field.optional(. "thickness", int),
  })
}

let data = `{
  "points": [
    { "x": 1, "y": -4 },
    { "x": 5, "y": 8 }
  ]
}`

let _ = data->Js.Json.parseExn->Json.decode(Decode.polyline)->Js.log

See examples for more.

Installation

npm install --save @glennsl/rescript-json-combinators

Then add @glennsl/rescript-json-combinators to bs-dependencies in your bsconfig.json:

 {
   "bs-dependencies": [
+    "@glennsl/rescript-json-combinators"
   ]
 }

Optionally, add this to automatically open the library in order to use Json.Encode and Json.Decode directly:

 {
   "bsc-flags": [
+    "-open JsonCombinators",
   ]
 }

Differences from bs-json

rescript-json-combinators is the spriritual successor of bs-json. It was rewritten from scratch and not intended to be backwards compatible. And while for the most part is does share the same design principles,there are also some notable differences:

  • Decoders have been made abstract. While the internal representation is still the same, a function that takes a Js.Json.t and returns either the decoded value or raises an exception, they can no longer be called directly. Instead they have to be run via Decode.decode. This is in order to ensure that the DecodeError exceptions don't leak and returns a result instead. Custom decoders can still be created with Decode.custom.

  • A new object decoder has been added, and while it's still possible to decode fields individually in a custom decoder, this addresses a couple ergonomic problems with that approach, such as distinguishing between a field missing and having the wrong type, and the over-use and under-encapsulation of custom decoders.

  • Uncurrying and raw javascript is used in some places to improve performance. For the most part this isn't visible to the end user because decoders can no longer be called directly. There are a couple exceptions however, such as field decoders inside the object deocder, and map.

Documentation

API

For the moment, please see the interface files:

License

This work is dual-licensed under LGPL 3.0 and MPL 2.0. You can choose between one of them if you use this work.

Please see LICENSE.LGPL-3.0 and LICENSE.MPL-2.0 for the full text of each license.

SPDX-License-Identifier: LGPL-3.0 OR MPL-2.0

Changes

1.4.0

  • explicitly curried encode combinators to reduce breakage in rescript 11 uncurried mode
  • updated examples to rescript 11

1.3.0

  • add Decode.indirect to help with decoding recursive data structures

1.2.0

  • add Decode.flatMap

1.1.0

  • recript v11-compatible formatting (#3)

1.0.0

Initial release