Skip to content

Instantly share code, notes, and snippets.

@whitequark
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitequark/0a530e03da9bd4633149 to your computer and use it in GitHub Desktop.
Save whitequark/0a530e03da9bd4633149 to your computer and use it in GitHub Desktop.
type json = [%import: Yojson.Safe.json] [@@deriving Show]
type json =
[ `Tuple of json list | `Bool of bool | `Intlit of string | `Null
| `Variant of (string* json option) | `Assoc of (string* json) list
| `List of json list | `Float of float | `String of string | `Int of int]
[@@deriving Show]
let rec pp_json fmt =
function
| `Tuple x ->
(Format.fprintf fmt "`Tuple (@[<hov>";
((fun x ->
Format.fprintf fmt "[@[<hov>";
ignore
(List.fold_left
(fun sep ->
fun x ->
if sep then Format.fprintf fmt ";@ ";
(pp_json fmt) x;
true) false x);
Format.fprintf fmt "@]]")) x;
Format.fprintf fmt "@])")
| `Bool x ->
(Format.fprintf fmt "`Bool (@[<hov>";
(Format.fprintf fmt "%B") x;
Format.fprintf fmt "@])")
| `Intlit x ->
(Format.fprintf fmt "`Intlit (@[<hov>";
(Format.fprintf fmt "%S") x;
Format.fprintf fmt "@])")
| `Null -> Format.pp_print_string fmt "`Null"
| `Variant x ->
(Format.fprintf fmt "`Variant (@[<hov>";
((fun (a0,a1) ->
Format.fprintf fmt "(@[<hov>";
((Format.fprintf fmt "%S") a0;
Format.fprintf fmt ",@ ";
((function
| None -> Format.pp_print_string fmt "None"
| Some x ->
(Format.pp_print_string fmt "Some (";
(pp_json fmt) x;
Format.pp_print_string fmt ")"))) a1);
Format.fprintf fmt "@])")) x;
Format.fprintf fmt "@])")
| `Assoc x ->
(Format.fprintf fmt "`Assoc (@[<hov>";
((fun x ->
Format.fprintf fmt "[@[<hov>";
ignore
(List.fold_left
(fun sep ->
fun x ->
if sep then Format.fprintf fmt ";@ ";
((fun (a0,a1) ->
Format.fprintf fmt "(@[<hov>";
((Format.fprintf fmt "%S") a0;
Format.fprintf fmt ",@ ";
(pp_json fmt) a1);
Format.fprintf fmt "@])")) x;
true) false x);
Format.fprintf fmt "@]]")) x;
Format.fprintf fmt "@])")
| `List x ->
(Format.fprintf fmt "`List (@[<hov>";
((fun x ->
Format.fprintf fmt "[@[<hov>";
ignore
(List.fold_left
(fun sep ->
fun x ->
if sep then Format.fprintf fmt ";@ ";
(pp_json fmt) x;
true) false x);
Format.fprintf fmt "@]]")) x;
Format.fprintf fmt "@])")
| `Float x ->
(Format.fprintf fmt "`Float (@[<hov>";
(Format.fprintf fmt "%F") x;
Format.fprintf fmt "@])")
| `String x ->
(Format.fprintf fmt "`String (@[<hov>";
(Format.fprintf fmt "%S") x;
Format.fprintf fmt "@])")
| `Int x ->
(Format.fprintf fmt "`Int (@[<hov>";
(Format.fprintf fmt "%d") x;
Format.fprintf fmt "@])")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment