Giter Club home page Giter Club logo

weeping's Introduction

weeping

License: MIT

The Functional JSON parsing library for BuckleScript inspired by Argo

How to use

1 Getting number

  • JSON
{
  "x": 6
}

Example 1

  • OCaml
let _ =
 let json = Js.Json.parseExn "{\"x\":6}" in
 match (json <| prop "x" Int) with
  | Some n -> print_int n
  | None -> ()

..note:

`( <| )` is function type is`Js.Json.t -> 'a kind -> 'a option`

and

`prop "x" Int` is `int kind`

`prop "x" Int` is syntax sugar `Object("x", Int)`

2 Nesting value

  • JSON
{
  "x": {
    "y": "Hey"
  }
}

Example 2

  • OCaml
let _ =
 let json = Js.Json.parseExn "{\"x\":{\"y\":\"Hey\"}}" in
 match (json <| path ["x"; "y"] String) with
 | Some str -> print_endline str
 | None -> ()

Note

path ["x"; "y"] String is string kind

path ["x"; "y"] String is syntax sugar Object("x", Object("y", String))

3 Record Type and Pattern Matching

  • JSON
{
  "x": {
    "key1": "Hello",
    "key2": 5
  }
}
type foo = {
  str: string;
  num: int;
}

let init_foo str num = {str;num;}

let match_foo json = Some init_foo <*> (json <| prop "key1" String) <*> (json <| prop "key2" Int)

let _ =
 let json = Js.Json.parseExn "{\"x\":{\"key1\":\"Hello\",\"key2\":5}}" in
 match (json <| prop "x" (Match match_foo)) with
 | Some {str; num} -> print_string str; print_int num; print_newline()
 | None -> ()

Authors

@kdxu, @hiroqn

License

This project is licensed under the MIT License - see the LICENSE.md file for details

weeping's People

Contributors

hiroqn avatar kdxu avatar

Watchers

James Cloos avatar  avatar

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.