GithubHelp home page GithubHelp logo

esbullington / bs-decode Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mlms13/bs-decode

0.0 2.0 0.0 355 KB

Type-safe JSON decoding for ReasonML and OCaml

Home Page: https://mlms13.github.io/bs-decode/docs/what-and-why

License: MIT License

OCaml 89.23% JavaScript 10.19% CSS 0.58%

bs-decode's Introduction

bs-decode

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

Update your bsconfig.json

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

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": "Michael", "age": 32, "isAdmin": true }
 *
 * you can write a function to convert this JSON into a value of type `user`
 */
module D = Decode.AsResult.OfParseError; /* module alias for brevity */

let decode = json =>
  D.Pipeline.(
    succeed(make)
    |> field("name", D.string)
    |> field("age", D.intFromNumber)
    |> field("isAdmin", D.boolean)
    |> optionalField("lastLogin", D.date)
    |> run(json)
  );

let myUser = decode(json); /* Belt.Result.Ok({...}) */

Contributing

  1. Fork and clone this repository
  2. npm install
  3. Add features and tests
  4. npm run test

A note on project structure:

  • DecodeBase.re is where most of the actual functionality lives
  • Decode_As*.re define the actual implementations, but those are minimal
  • Decode_ParseError.re defines the error type useful for Results, as well as a collection of helpers to work with Results of that kind.

License

Released under the MIT license. See LICENSE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.