New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

rescript-funicular

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rescript-funicular

composable JSON parsing for ReScript

latest
Source
npmnpm
Version
1.0.0-alpha.5
Version published
Maintainers
1
Created
Source

funicular - composable JSON parsing for ReScript

  • easily composable using builtin primitives, or extend with your own
  • write typesafe and idiomatic parsers
  • meaningful error output with result types

Install

  • Install from npm
npm i rescript-funicular -S # yarn add rescript-funicular
  • Add to your bsconfig.json
  "bs-dependencies": [
    ...
    "rescript-funicular",
  ]

Example

Define your type:

type customer = {
  customerNo: int,
  name: string,
  orders: array<order>,
}

And write a decoder function:

// Write decoder functions that translate the JSON structure into your fields
let decodeCustomer = value => {
  open Funicular.Decode

  // first decode the value as an object
  let o = value->object_

  // extract and parse each field of the object using the relevant decoder
  let customerNo = o->field("customerNo", integer)
  let name = o->field("name", string)
  // `decodeOrder` is a custom decoder for the `order` type
  let orders = o->field("orders", array(decodeOrder)) 

  // Use `rmap()` to wrap your builder function, and then feed in each parameter with `v()`
  rmap((customerNo, name, orders) => {customerNo: customerNo, name: name, orders: orders})
  ->v(customerNo)
  ->v(name)
  ->v(orders)
}

Use the Funicular.Decode.parse function to parse your string, passing in your decoder:

// Parse with your custom decoder function for the root object
let customerString = `{"customerNo": 20, "name": "Chris", "orders": [] }`
let myCustomer = Funicular.Decode.parse(customerString, decodeCustomer);

Encoding is simpler - a straight type conversion without the error-handling overhead:

let encodeCustomer = val => {
  open Funicular.Encode
  object_([
    ("customerNo", integer(val.customerNo)),
    ("name", string(val.name)),
    ("orders", array(val.orders, encodeOrder)),
  ])
}

Keywords

rescript

FAQs

Package last updated on 21 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts