Skip to content

Commit

Permalink
Add an example for constructing json
Browse files Browse the repository at this point in the history
  • Loading branch information
gs0510 committed Dec 5, 2019
1 parent 8a2a677 commit 2411aa8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/constructing.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(*
dune exec examples/constructing.exe <<EOF
{
"id": "398eb027",
"name": "John Doe",
"pages": [
{
"id": 1,
"title": "The Art of Flipping Coins",
}
]
}
EOF
*)

open Yojson.Basic.Util

let json_output =
`Assoc
[
("id", `String "398eb027");
("name", `String "John Doe");
( "pages",
`Assoc
[ ("id", `Int 1); ("title", `String "The Art of Flipping Coins") ] );
]

let main () =
let oc = stdout in
Yojson.Basic.pretty_to_channel oc json_output;
output_string oc "\n";
close_out oc

let () = main ()
7 changes: 7 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
(executable
(name filtering)
(modules filtering)
(flags (-safe-string))
(libraries yojson))

(executable
(name constructing)
(modules constructing)
(flags (-safe-string))
(libraries yojson))

Expand Down

0 comments on commit 2411aa8

Please sign in to comment.