• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language Reason
  • License
    MIT License
  • Created about 6 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type-safe JSON decoding for ReasonML and OCaml

bs-decode

build status test coverage npm version license

Note

bs-decode has been stable and used in production for several years, so a v1 release makes sense. This is the final release that will be compatible with BuckleScript as we turn our attention to the newer OCaml features available in Melange.

Read the Documentation

Decode JSON values into structured ReasonML and OCaml types. Inspired by Elm's Json.Decode and the Decode Pipeline, bs-decode is an alternative to bs-json that focuses on structured, type-safe error handling, rather than exceptions. Additionally, bs-decode collects up everything that went wrong while parsing the JSON, rather than failing on the first error.

Installation

Install via npm:

npm install --save bs-decode relude bs-bastet

Update your bsconfig.json

"bs-dependencies": [
  "bs-bastet",
  "bs-decode",
  "relude"
],

Usage

The following is available to give you an idea of how the library works, but the complete documentation will probably be more useful if you want to write your own decoders.

// imagine you have a `user` type and `make` function to construct one
type user = {
  name: string,
  age: int,
  isAdmin: bool,
  lastLogin: option(Js.Date.t)
};

let make = (name, age, isAdmin, lastLogin) =>
  { name, age, isAdmin, lastLogin };

/**
 * Given a JSON value that looks like:
 * { "name": "Alice", "age": 44, "roles": ["admin"] }
 *
 * you can write a function to convert this JSON into a value of type `user`
 */
module Decode = Decode.AsResult.OfParseError; // module alias for brevity

let decode = json =>
  Decode.Pipeline.(
    succeed(make)
    |> field("name", string)
    |> field("age", intFromNumber)
    |> field("roles", list(string) |> map(List.contains("admin")))
    |> optionalField("lastLogin", date)
    |> run(json)
  );

let myUser = decode(json); /* Ok({ name: "Alice", ...}) */

Contributing

All contributions are welcome! This obviously includes code changes and documentation improvements (see CONTRIBUTING), but we also appreciate any feedback you want to provide (in the form of Github issues) about concepts that are confusing or poorly explained in the docs.

License

Released under the MIT license.

More Repositories

1

boidz

Just a simple flocking simulation
Haxe
11
star
2

FancyArea

Is it just a textarea? No. It's much fancier than that.
JavaScript
10
star
3

rr-sortable-table

A generically sortable table with ReasonReact and lenses-ppx
Reason
9
star
4

csv-reader

Client-side file picker that parses CSVs
Reason
8
star
5

build-optimizations

Speeding up our Gulp build
JavaScript
7
star
6

FancySearch

A simple typeahead search implementation for Haxe and JavaScript
Haxe
7
star
7

FancyTable

A table that is fancy.
Haxe
5
star
8

bs-nonempty

NonEmpty type with Array and List implementations for ReasonML and OCaml
OCaml
5
star
9

Navtacular

A horizontal navigation bar with menus that doesn't depend on Javascript for core functionality.
CSS
4
star
10

imagecrop

ImageCrop is a simple canvas-based tool to help you crop images.
JavaScript
4
star
11

SimpleSearch

Powerful list searching in simple packaging.
JavaScript
3
star
12

Codebreaker

A simple Javascript take on a Mastermind-style code breaking game.
JavaScript
2
star
13

reform-demo

Reason
2
star
14

tiki-tk

Tiki TK is a UI toolkit for the Doom virtual DOM library
Haxe
2
star
15

JS-Accordion

Javascript accordion menu, using jQuery.
CSS
2
star
16

re-yaml

ReasonML bindings for JS-YAML
OCaml
2
star
17

mod-negative-zero

Reason
2
star
18

cryptograms

OCaml
2
star
19

prismic-test

Hooking up prismic.io with Express 4 and Jade
JavaScript
1
star
20

swarm-game

A game of swarming
Haxe
1
star
21

foods

A list of foods
1
star
22

simple-note

JavaScript
1
star
23

github-api-project

OCaml
1
star
24

elm-weather

Elm
1
star
25

purs-homeworlds

Binary Homeworlds written in PureScript
PureScript
1
star
26

cropsicle

Cropsicle is a simple webapp for taking screenshots of other webpages and cropping them to your liking.
JavaScript
1
star
27

brown-shark

This is top secret, as you can tell from the name. It might also be called recline. Re-CLI...ne
Makefile
1
star
28

bsb-520-include

Demonstrate the Bucklescript considers "include" to be impure
OCaml
1
star
29

JS-Autocomplete

A Javascript library that makes it simple to add a dropdown box with suggestions below a form input.
JavaScript
1
star
30

recommend

Reason
1
star
31

Integrant

Generate forms automatically given a structured JSON schema
JavaScript
1
star
32

check-webhook-test

JavaScript
1
star
33

MagicProstgres

1
star
34

panel-slider

A simple slider plugin for jQuery. Slides move horizontally, and the tabs stack neatly on the sides.
JavaScript
1
star
35

FancyBrowser

A small collection of DOM and browser utilities for Haxe
Haxe
1
star
36

march-war-game

The theme for March is war games. I don't even have enough of an idea to name my repository.
Haxe
1
star