OCaml library for JSONfeed parsing and creation
at main 1.1 kB view raw
1(** Example: JSON Feed Echo 2 3 Reads a JSON Feed from stdin, parses it, and outputs it to stdout. Useful 4 for testing round-trip parsing and identifying any changes during 5 serialization/deserialization. 6 7 Usage: feed_echo < feed.json cat feed.json | feed_echo > output.json diff 8 <(cat feed.json | feed_echo) feed.json 9 10 Exit codes: 0 - Success 1 - Parsing or encoding failed *) 11 12let echo_feed () = 13 (* Create a bytesrw reader from stdin *) 14 let stdin = Bytesrw.Bytes.Reader.of_in_channel In_channel.stdin in 15 16 (* Parse the JSON feed *) 17 match Jsonfeed.decode ~locs:true stdin with 18 | Error err -> 19 Format.eprintf "Parsing failed:\n %s\n%!" (Jsont.Error.to_string err); 20 exit 1 21 | Ok feed -> ( 22 (* Encode the feed back to stdout *) 23 match Jsonfeed.to_string ~minify:false feed with 24 | Error err -> 25 Format.eprintf "Encoding failed:\n %s\n%!" 26 (Jsont.Error.to_string err); 27 exit 1 28 | Ok json -> 29 print_string json; 30 print_newline (); 31 exit 0) 32 33let () = echo_feed ()