From b0cf54362379ccba6076ed658c1d96bcd6a7989a Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Thu, 12 Jun 2014 23:08:33 -0700 Subject: [PATCH] use atdgen to rewrite parse_book.ml to better show the advantage --- code/json/book.atd | 3 + code/json/book_j.ml | 679 ++++++++++++++++++++++++++++++++++++ code/json/book_j.mli | 54 +++ code/json/book_t.ml | 13 + code/json/book_t.mli | 13 + code/json/parse_book_atd.ml | 15 + 6 files changed, 777 insertions(+) create mode 100644 code/json/book.atd create mode 100644 code/json/book_j.ml create mode 100644 code/json/book_j.mli create mode 100644 code/json/book_t.ml create mode 100644 code/json/book_t.mli create mode 100644 code/json/parse_book_atd.ml diff --git a/code/json/book.atd b/code/json/book.atd new file mode 100644 index 0000000..3d6caf1 --- /dev/null +++ b/code/json/book.atd @@ -0,0 +1,3 @@ +type author = { name : string; affiliation: string } + +type book = { title: string; tags: string list; pages: int; authors: author list; ?is_online: bool option; ?is_translated: bool option } diff --git a/code/json/book_j.ml b/code/json/book_j.ml new file mode 100644 index 0000000..1606422 --- /dev/null +++ b/code/json/book_j.ml @@ -0,0 +1,679 @@ +(* Auto-generated from "book.atd" *) + + +type author = Book_t.author = { name: string; affiliation: string } + +type book = Book_t.book = { + title: string; + tags: string list; + pages: int; + authors: author list; + is_online: bool option; + is_translated: bool option +} + +let write_author : _ -> author -> _ = ( + fun ob x -> + Bi_outbuf.add_char ob '{'; + let is_first = ref true in + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"name\":"; + ( + Yojson.Safe.write_string + ) + ob x.name; + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"affiliation\":"; + ( + Yojson.Safe.write_string + ) + ob x.affiliation; + Bi_outbuf.add_char ob '}'; +) +let string_of_author ?(len = 1024) x = + let ob = Bi_outbuf.create len in + write_author ob x; + Bi_outbuf.contents ob +let read_author = ( + fun p lb -> + Yojson.Safe.read_space p lb; + Yojson.Safe.read_lcurl p lb; + let (x : author) = + { + name = Obj.magic 0.0; + affiliation = Obj.magic 0.0; + } + in + let bits0 = ref 0 in + try + Yojson.Safe.read_space p lb; + Yojson.Safe.read_object_end lb; + Yojson.Safe.read_space p lb; + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + match len with + | 4 -> ( + if String.unsafe_get s pos = 'n' && String.unsafe_get s (pos+1) = 'a' && String.unsafe_get s (pos+2) = 'm' && String.unsafe_get s (pos+3) = 'e' then ( + 0 + ) + else ( + -1 + ) + ) + | 11 -> ( + if String.unsafe_get s pos = 'a' && String.unsafe_get s (pos+1) = 'f' && String.unsafe_get s (pos+2) = 'f' && String.unsafe_get s (pos+3) = 'i' && String.unsafe_get s (pos+4) = 'l' && String.unsafe_get s (pos+5) = 'i' && String.unsafe_get s (pos+6) = 'a' && String.unsafe_get s (pos+7) = 't' && String.unsafe_get s (pos+8) = 'i' && String.unsafe_get s (pos+9) = 'o' && String.unsafe_get s (pos+10) = 'n' then ( + 1 + ) + else ( + -1 + ) + ) + | _ -> ( + -1 + ) + in + let i = Yojson.Safe.map_ident p f lb in + Ag_oj_run.read_until_field_value p lb; + ( + match i with + | 0 -> + let v = + ( + Ag_oj_run.read_string + ) p lb + in + Obj.set_field (Obj.repr x) 0 (Obj.repr v); + bits0 := !bits0 lor 0x1; + | 1 -> + let v = + ( + Ag_oj_run.read_string + ) p lb + in + Obj.set_field (Obj.repr x) 1 (Obj.repr v); + bits0 := !bits0 lor 0x2; + | _ -> ( + Yojson.Safe.skip_json p lb + ) + ); + while true do + Yojson.Safe.read_space p lb; + Yojson.Safe.read_object_sep p lb; + Yojson.Safe.read_space p lb; + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + match len with + | 4 -> ( + if String.unsafe_get s pos = 'n' && String.unsafe_get s (pos+1) = 'a' && String.unsafe_get s (pos+2) = 'm' && String.unsafe_get s (pos+3) = 'e' then ( + 0 + ) + else ( + -1 + ) + ) + | 11 -> ( + if String.unsafe_get s pos = 'a' && String.unsafe_get s (pos+1) = 'f' && String.unsafe_get s (pos+2) = 'f' && String.unsafe_get s (pos+3) = 'i' && String.unsafe_get s (pos+4) = 'l' && String.unsafe_get s (pos+5) = 'i' && String.unsafe_get s (pos+6) = 'a' && String.unsafe_get s (pos+7) = 't' && String.unsafe_get s (pos+8) = 'i' && String.unsafe_get s (pos+9) = 'o' && String.unsafe_get s (pos+10) = 'n' then ( + 1 + ) + else ( + -1 + ) + ) + | _ -> ( + -1 + ) + in + let i = Yojson.Safe.map_ident p f lb in + Ag_oj_run.read_until_field_value p lb; + ( + match i with + | 0 -> + let v = + ( + Ag_oj_run.read_string + ) p lb + in + Obj.set_field (Obj.repr x) 0 (Obj.repr v); + bits0 := !bits0 lor 0x1; + | 1 -> + let v = + ( + Ag_oj_run.read_string + ) p lb + in + Obj.set_field (Obj.repr x) 1 (Obj.repr v); + bits0 := !bits0 lor 0x2; + | _ -> ( + Yojson.Safe.skip_json p lb + ) + ); + done; + assert false; + with Yojson.End_of_object -> ( + if !bits0 <> 0x3 then Ag_oj_run.missing_fields [| !bits0 |] [| "name"; "affiliation" |]; + Ag_oj_run.identity x + ) +) +let author_of_string s = + read_author (Yojson.Safe.init_lexer ()) (Lexing.from_string s) +let write__3 = ( + Ag_oj_run.write_option ( + Yojson.Safe.write_bool + ) +) +let string_of__3 ?(len = 1024) x = + let ob = Bi_outbuf.create len in + write__3 ob x; + Bi_outbuf.contents ob +let read__3 = ( + fun p lb -> + Yojson.Safe.read_space p lb; + match Yojson.Safe.start_any_variant p lb with + | `Edgy_bracket -> ( + Yojson.Safe.read_space p lb; + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + try + if len = 4 then ( + match String.unsafe_get s pos with + | 'N' -> ( + if String.unsafe_get s (pos+1) = 'o' && String.unsafe_get s (pos+2) = 'n' && String.unsafe_get s (pos+3) = 'e' then ( + 0 + ) + else ( + raise (Exit) + ) + ) + | 'S' -> ( + if String.unsafe_get s (pos+1) = 'o' && String.unsafe_get s (pos+2) = 'm' && String.unsafe_get s (pos+3) = 'e' then ( + 1 + ) + else ( + raise (Exit) + ) + ) + | _ -> ( + raise (Exit) + ) + ) + else ( + raise (Exit) + ) + with Exit -> ( + Ag_oj_run.invalid_variant_tag (String.sub s pos len) + ) + in + let i = Yojson.Safe.map_ident p f lb in + match i with + | 0 -> + Yojson.Safe.read_space p lb; + Yojson.Safe.read_gt p lb; + (None : _ option) + | 1 -> + Ag_oj_run.read_until_field_value p lb; + let x = ( + Ag_oj_run.read_bool + ) p lb + in + Yojson.Safe.read_space p lb; + Yojson.Safe.read_gt p lb; + (Some x : _ option) + | _ -> ( + assert false + ) + ) + | `Double_quote -> ( + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + try + if len = 4 && String.unsafe_get s pos = 'N' && String.unsafe_get s (pos+1) = 'o' && String.unsafe_get s (pos+2) = 'n' && String.unsafe_get s (pos+3) = 'e' then ( + 0 + ) + else ( + raise (Exit) + ) + with Exit -> ( + Ag_oj_run.invalid_variant_tag (String.sub s pos len) + ) + in + let i = Yojson.Safe.map_string p f lb in + match i with + | 0 -> + (None : _ option) + | _ -> ( + assert false + ) + ) + | `Square_bracket -> ( + Yojson.Safe.read_space p lb; + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + try + if len = 4 && String.unsafe_get s pos = 'S' && String.unsafe_get s (pos+1) = 'o' && String.unsafe_get s (pos+2) = 'm' && String.unsafe_get s (pos+3) = 'e' then ( + 0 + ) + else ( + raise (Exit) + ) + with Exit -> ( + Ag_oj_run.invalid_variant_tag (String.sub s pos len) + ) + in + let i = Yojson.Safe.map_ident p f lb in + match i with + | 0 -> + Yojson.Safe.read_space p lb; + Yojson.Safe.read_comma p lb; + Yojson.Safe.read_space p lb; + let x = ( + Ag_oj_run.read_bool + ) p lb + in + Yojson.Safe.read_space p lb; + Yojson.Safe.read_rbr p lb; + (Some x : _ option) + | _ -> ( + assert false + ) + ) +) +let _3_of_string s = + read__3 (Yojson.Safe.init_lexer ()) (Lexing.from_string s) +let write__2 = ( + Ag_oj_run.write_list ( + write_author + ) +) +let string_of__2 ?(len = 1024) x = + let ob = Bi_outbuf.create len in + write__2 ob x; + Bi_outbuf.contents ob +let read__2 = ( + Ag_oj_run.read_list ( + read_author + ) +) +let _2_of_string s = + read__2 (Yojson.Safe.init_lexer ()) (Lexing.from_string s) +let write__1 = ( + Ag_oj_run.write_list ( + Yojson.Safe.write_string + ) +) +let string_of__1 ?(len = 1024) x = + let ob = Bi_outbuf.create len in + write__1 ob x; + Bi_outbuf.contents ob +let read__1 = ( + Ag_oj_run.read_list ( + Ag_oj_run.read_string + ) +) +let _1_of_string s = + read__1 (Yojson.Safe.init_lexer ()) (Lexing.from_string s) +let write_book : _ -> book -> _ = ( + fun ob x -> + Bi_outbuf.add_char ob '{'; + let is_first = ref true in + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"title\":"; + ( + Yojson.Safe.write_string + ) + ob x.title; + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"tags\":"; + ( + write__1 + ) + ob x.tags; + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"pages\":"; + ( + Yojson.Safe.write_int + ) + ob x.pages; + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"authors\":"; + ( + write__2 + ) + ob x.authors; + (match x.is_online with None -> () | Some x -> + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"is_online\":"; + ( + Yojson.Safe.write_bool + ) + ob x; + ); + (match x.is_translated with None -> () | Some x -> + if !is_first then + is_first := false + else + Bi_outbuf.add_char ob ','; + Bi_outbuf.add_string ob "\"is_translated\":"; + ( + Yojson.Safe.write_bool + ) + ob x; + ); + Bi_outbuf.add_char ob '}'; +) +let string_of_book ?(len = 1024) x = + let ob = Bi_outbuf.create len in + write_book ob x; + Bi_outbuf.contents ob +let read_book = ( + fun p lb -> + Yojson.Safe.read_space p lb; + Yojson.Safe.read_lcurl p lb; + let (x : book) = + { + title = Obj.magic 0.0; + tags = Obj.magic 0.0; + pages = Obj.magic 0.0; + authors = Obj.magic 0.0; + is_online = None; + is_translated = None; + } + in + let bits0 = ref 0 in + try + Yojson.Safe.read_space p lb; + Yojson.Safe.read_object_end lb; + Yojson.Safe.read_space p lb; + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + match len with + | 4 -> ( + if String.unsafe_get s pos = 't' && String.unsafe_get s (pos+1) = 'a' && String.unsafe_get s (pos+2) = 'g' && String.unsafe_get s (pos+3) = 's' then ( + 1 + ) + else ( + -1 + ) + ) + | 5 -> ( + match String.unsafe_get s pos with + | 'p' -> ( + if String.unsafe_get s (pos+1) = 'a' && String.unsafe_get s (pos+2) = 'g' && String.unsafe_get s (pos+3) = 'e' && String.unsafe_get s (pos+4) = 's' then ( + 2 + ) + else ( + -1 + ) + ) + | 't' -> ( + if String.unsafe_get s (pos+1) = 'i' && String.unsafe_get s (pos+2) = 't' && String.unsafe_get s (pos+3) = 'l' && String.unsafe_get s (pos+4) = 'e' then ( + 0 + ) + else ( + -1 + ) + ) + | _ -> ( + -1 + ) + ) + | 7 -> ( + if String.unsafe_get s pos = 'a' && String.unsafe_get s (pos+1) = 'u' && String.unsafe_get s (pos+2) = 't' && String.unsafe_get s (pos+3) = 'h' && String.unsafe_get s (pos+4) = 'o' && String.unsafe_get s (pos+5) = 'r' && String.unsafe_get s (pos+6) = 's' then ( + 3 + ) + else ( + -1 + ) + ) + | 9 -> ( + if String.unsafe_get s pos = 'i' && String.unsafe_get s (pos+1) = 's' && String.unsafe_get s (pos+2) = '_' && String.unsafe_get s (pos+3) = 'o' && String.unsafe_get s (pos+4) = 'n' && String.unsafe_get s (pos+5) = 'l' && String.unsafe_get s (pos+6) = 'i' && String.unsafe_get s (pos+7) = 'n' && String.unsafe_get s (pos+8) = 'e' then ( + 4 + ) + else ( + -1 + ) + ) + | 13 -> ( + if String.unsafe_get s pos = 'i' && String.unsafe_get s (pos+1) = 's' && String.unsafe_get s (pos+2) = '_' && String.unsafe_get s (pos+3) = 't' && String.unsafe_get s (pos+4) = 'r' && String.unsafe_get s (pos+5) = 'a' && String.unsafe_get s (pos+6) = 'n' && String.unsafe_get s (pos+7) = 's' && String.unsafe_get s (pos+8) = 'l' && String.unsafe_get s (pos+9) = 'a' && String.unsafe_get s (pos+10) = 't' && String.unsafe_get s (pos+11) = 'e' && String.unsafe_get s (pos+12) = 'd' then ( + 5 + ) + else ( + -1 + ) + ) + | _ -> ( + -1 + ) + in + let i = Yojson.Safe.map_ident p f lb in + Ag_oj_run.read_until_field_value p lb; + ( + match i with + | 0 -> + let v = + ( + Ag_oj_run.read_string + ) p lb + in + Obj.set_field (Obj.repr x) 0 (Obj.repr v); + bits0 := !bits0 lor 0x1; + | 1 -> + let v = + ( + read__1 + ) p lb + in + Obj.set_field (Obj.repr x) 1 (Obj.repr v); + bits0 := !bits0 lor 0x2; + | 2 -> + let v = + ( + Ag_oj_run.read_int + ) p lb + in + Obj.set_field (Obj.repr x) 2 (Obj.repr v); + bits0 := !bits0 lor 0x4; + | 3 -> + let v = + ( + read__2 + ) p lb + in + Obj.set_field (Obj.repr x) 3 (Obj.repr v); + bits0 := !bits0 lor 0x8; + | 4 -> + if not (Yojson.Safe.read_null_if_possible p lb) then ( + let v = + Some ( + ( + Ag_oj_run.read_bool + ) p lb + ) + in + Obj.set_field (Obj.repr x) 4 (Obj.repr v); + ) + | 5 -> + if not (Yojson.Safe.read_null_if_possible p lb) then ( + let v = + Some ( + ( + Ag_oj_run.read_bool + ) p lb + ) + in + Obj.set_field (Obj.repr x) 5 (Obj.repr v); + ) + | _ -> ( + Yojson.Safe.skip_json p lb + ) + ); + while true do + Yojson.Safe.read_space p lb; + Yojson.Safe.read_object_sep p lb; + Yojson.Safe.read_space p lb; + let f = + fun s pos len -> + if pos < 0 || len < 0 || pos + len > String.length s then + invalid_arg "out-of-bounds substring position or length"; + match len with + | 4 -> ( + if String.unsafe_get s pos = 't' && String.unsafe_get s (pos+1) = 'a' && String.unsafe_get s (pos+2) = 'g' && String.unsafe_get s (pos+3) = 's' then ( + 1 + ) + else ( + -1 + ) + ) + | 5 -> ( + match String.unsafe_get s pos with + | 'p' -> ( + if String.unsafe_get s (pos+1) = 'a' && String.unsafe_get s (pos+2) = 'g' && String.unsafe_get s (pos+3) = 'e' && String.unsafe_get s (pos+4) = 's' then ( + 2 + ) + else ( + -1 + ) + ) + | 't' -> ( + if String.unsafe_get s (pos+1) = 'i' && String.unsafe_get s (pos+2) = 't' && String.unsafe_get s (pos+3) = 'l' && String.unsafe_get s (pos+4) = 'e' then ( + 0 + ) + else ( + -1 + ) + ) + | _ -> ( + -1 + ) + ) + | 7 -> ( + if String.unsafe_get s pos = 'a' && String.unsafe_get s (pos+1) = 'u' && String.unsafe_get s (pos+2) = 't' && String.unsafe_get s (pos+3) = 'h' && String.unsafe_get s (pos+4) = 'o' && String.unsafe_get s (pos+5) = 'r' && String.unsafe_get s (pos+6) = 's' then ( + 3 + ) + else ( + -1 + ) + ) + | 9 -> ( + if String.unsafe_get s pos = 'i' && String.unsafe_get s (pos+1) = 's' && String.unsafe_get s (pos+2) = '_' && String.unsafe_get s (pos+3) = 'o' && String.unsafe_get s (pos+4) = 'n' && String.unsafe_get s (pos+5) = 'l' && String.unsafe_get s (pos+6) = 'i' && String.unsafe_get s (pos+7) = 'n' && String.unsafe_get s (pos+8) = 'e' then ( + 4 + ) + else ( + -1 + ) + ) + | 13 -> ( + if String.unsafe_get s pos = 'i' && String.unsafe_get s (pos+1) = 's' && String.unsafe_get s (pos+2) = '_' && String.unsafe_get s (pos+3) = 't' && String.unsafe_get s (pos+4) = 'r' && String.unsafe_get s (pos+5) = 'a' && String.unsafe_get s (pos+6) = 'n' && String.unsafe_get s (pos+7) = 's' && String.unsafe_get s (pos+8) = 'l' && String.unsafe_get s (pos+9) = 'a' && String.unsafe_get s (pos+10) = 't' && String.unsafe_get s (pos+11) = 'e' && String.unsafe_get s (pos+12) = 'd' then ( + 5 + ) + else ( + -1 + ) + ) + | _ -> ( + -1 + ) + in + let i = Yojson.Safe.map_ident p f lb in + Ag_oj_run.read_until_field_value p lb; + ( + match i with + | 0 -> + let v = + ( + Ag_oj_run.read_string + ) p lb + in + Obj.set_field (Obj.repr x) 0 (Obj.repr v); + bits0 := !bits0 lor 0x1; + | 1 -> + let v = + ( + read__1 + ) p lb + in + Obj.set_field (Obj.repr x) 1 (Obj.repr v); + bits0 := !bits0 lor 0x2; + | 2 -> + let v = + ( + Ag_oj_run.read_int + ) p lb + in + Obj.set_field (Obj.repr x) 2 (Obj.repr v); + bits0 := !bits0 lor 0x4; + | 3 -> + let v = + ( + read__2 + ) p lb + in + Obj.set_field (Obj.repr x) 3 (Obj.repr v); + bits0 := !bits0 lor 0x8; + | 4 -> + if not (Yojson.Safe.read_null_if_possible p lb) then ( + let v = + Some ( + ( + Ag_oj_run.read_bool + ) p lb + ) + in + Obj.set_field (Obj.repr x) 4 (Obj.repr v); + ) + | 5 -> + if not (Yojson.Safe.read_null_if_possible p lb) then ( + let v = + Some ( + ( + Ag_oj_run.read_bool + ) p lb + ) + in + Obj.set_field (Obj.repr x) 5 (Obj.repr v); + ) + | _ -> ( + Yojson.Safe.skip_json p lb + ) + ); + done; + assert false; + with Yojson.End_of_object -> ( + if !bits0 <> 0xf then Ag_oj_run.missing_fields [| !bits0 |] [| "title"; "tags"; "pages"; "authors" |]; + Ag_oj_run.identity x + ) +) +let book_of_string s = + read_book (Yojson.Safe.init_lexer ()) (Lexing.from_string s) diff --git a/code/json/book_j.mli b/code/json/book_j.mli new file mode 100644 index 0000000..681671d --- /dev/null +++ b/code/json/book_j.mli @@ -0,0 +1,54 @@ +(* Auto-generated from "book.atd" *) + + +type author = Book_t.author = { name: string; affiliation: string } + +type book = Book_t.book = { + title: string; + tags: string list; + pages: int; + authors: author list; + is_online: bool option; + is_translated: bool option +} + +val write_author : + Bi_outbuf.t -> author -> unit + (** Output a JSON value of type {!author}. *) + +val string_of_author : + ?len:int -> author -> string + (** Serialize a value of type {!author} + into a JSON string. + @param len specifies the initial length + of the buffer used internally. + Default: 1024. *) + +val read_author : + Yojson.Safe.lexer_state -> Lexing.lexbuf -> author + (** Input JSON data of type {!author}. *) + +val author_of_string : + string -> author + (** Deserialize JSON data of type {!author}. *) + +val write_book : + Bi_outbuf.t -> book -> unit + (** Output a JSON value of type {!book}. *) + +val string_of_book : + ?len:int -> book -> string + (** Serialize a value of type {!book} + into a JSON string. + @param len specifies the initial length + of the buffer used internally. + Default: 1024. *) + +val read_book : + Yojson.Safe.lexer_state -> Lexing.lexbuf -> book + (** Input JSON data of type {!book}. *) + +val book_of_string : + string -> book + (** Deserialize JSON data of type {!book}. *) + diff --git a/code/json/book_t.ml b/code/json/book_t.ml new file mode 100644 index 0000000..f30dcb6 --- /dev/null +++ b/code/json/book_t.ml @@ -0,0 +1,13 @@ +(* Auto-generated from "book.atd" *) + + +type author = { name: string; affiliation: string } + +type book = { + title: string; + tags: string list; + pages: int; + authors: author list; + is_online: bool option; + is_translated: bool option +} diff --git a/code/json/book_t.mli b/code/json/book_t.mli new file mode 100644 index 0000000..f30dcb6 --- /dev/null +++ b/code/json/book_t.mli @@ -0,0 +1,13 @@ +(* Auto-generated from "book.atd" *) + + +type author = { name: string; affiliation: string } + +type book = { + title: string; + tags: string list; + pages: int; + authors: author list; + is_online: bool option; + is_translated: bool option +} diff --git a/code/json/parse_book_atd.ml b/code/json/parse_book_atd.ml new file mode 100644 index 0000000..6a3002e --- /dev/null +++ b/code/json/parse_book_atd.ml @@ -0,0 +1,15 @@ +open Core.Std + +let () = + In_channel.read_all "book.json" + |> Book_j.book_of_string + |> fun book -> + let open Book_t in + let names = List.map book.authors ~f:(fun author -> author.name) in + (* Print the results of the parsing *) + printf "Title: %s (%d)\n" book.title book.pages; + printf "Authors: %s\n" (String.concat ~sep:", " names); + printf "Tags: %s\n" (String.concat ~sep:", " book.tags); + printf "Online: %B\n" (Option.value ~default:false book.is_online); + printf "Translated: %B\n" (Option.value ~default:false book.is_translated) +