weeping
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