Skip to content

Commit

Permalink
feat: remove easy-format
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Nov 26, 2019
1 parent 5c6d342 commit 6022b68
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 48 deletions.
66 changes: 30 additions & 36 deletions lib/pretty.ml
Original file line number Diff line number Diff line change
@@ -1,69 +1,63 @@
open Printf

let array = Easy_format.list
let record = Easy_format.list
let tuple = { Easy_format.list with
space_after_opening = false;
space_before_closing = false;
align_closing = false }
let variant = { Easy_format.list with
space_before_closing = false; }
let pp_list sep ppx out l =
let pp_sep out () = Format.fprintf out "%s@ " sep in
Format.pp_print_list ~pp_sep ppx out l

let rec format std (x : t) =
let rec format std (out:Format.formatter) (x : t) : unit =
match x with
`Null -> Easy_format.Atom ("null", Easy_format.atom)
| `Bool x -> Easy_format.Atom ((if x then "true" else "false"), Easy_format.atom)
| `Int x -> Easy_format.Atom (json_string_of_int x, Easy_format.atom)
| `Null -> Format.pp_print_string out "null"
| `Bool x -> Format.pp_print_bool out x
| `Int x -> Format.pp_print_string out (json_string_of_int x)
| `Float x ->
let s =
if std then std_json_string_of_float x
else json_string_of_float x
in
Easy_format.Atom (s, Easy_format.atom)
| `String s -> Easy_format.Atom (json_string_of_string s, Easy_format.atom)
Format.pp_print_string out s
| `String s -> Format.pp_print_string out (json_string_of_string s)
| `Intlit s
| `Floatlit s
| `Stringlit s -> Easy_format.Atom (s, Easy_format.atom)
| `List [] -> Easy_format.Atom ("[]", Easy_format.atom)
| `List l -> Easy_format.List (("[", ",", "]", array), List.map (format std) l)
| `Assoc [] -> Easy_format.Atom ("{}", Easy_format.atom)
| `Assoc l -> Easy_format.List (("{", ",", "}", record), List.map (format_field std) l)
| `Stringlit s -> Format.pp_print_string out s
| `List [] -> Format.pp_print_string out "[]"
| `List l -> Format.fprintf out "[@;<1 0>@[<hov>%a@]@;<1 -2>]" (pp_list "," (format std)) l
| `Assoc [] -> Format.pp_print_string out "{}"
| `Assoc l ->
Format.fprintf out "{@;<1 0>%a@;<1 -2>}" (pp_list "," (format_field std)) l
| `Tuple l ->
if std then
format std (`List l)
format std out (`List l)
else
if l = [] then
Easy_format.Atom ("()", Easy_format.atom)
Format.pp_print_string out "()"
else
Easy_format.List (("(", ",", ")", tuple), List.map (format std) l)
Format.fprintf out "(@,%a@;<0 -2>)" (pp_list "," (format std)) l

| `Variant (s, None) ->
if std then
format std (`String s)
format std out (`String s)
else
Easy_format.Atom ("<" ^ json_string_of_string s ^ ">", Easy_format.atom)
Format.fprintf out "<%s>" (json_string_of_string s)

| `Variant (s, Some x) ->
if std then
format std (`List [ `String s; x ])
format std out (`List [ `String s; x ])
else
let op = "<" ^ json_string_of_string s ^ ":" in
Easy_format.List ((op, "", ">", variant), [format std x])
let op = json_string_of_string s in
Format.fprintf out "<@[<hv2>%s: %a@]>" op (format std) x

and format_field std (name, x) =
let s = sprintf "%s:" (json_string_of_string name) in
Easy_format.Label ((Easy_format.Atom (s, Easy_format.atom), Easy_format.label), format std x)
and format_field std out (name, x) =
Format.fprintf out "@[<hv2>%s: %a@]" (json_string_of_string name) (format std) x


let format ?(std = false) x =
let pp ?(std = false) out x =
if std && not (is_object_or_array x) then
json_error
"Root is not an object or array as requested by the JSON standard"
else
format std (x :> t)
Format.fprintf out "@[<hv2>%a@]" (format std) (x :> t)

let to_string ?std x =
Easy_format.Pretty.to_string (format ?std x)
Format.asprintf "%a" (pp ?std) x

let to_channel ?std oc x =
Easy_format.Pretty.to_channel oc (format ?std x)
let fmt = Format.formatter_of_out_channel oc in
Format.fprintf fmt "%a@?" (pp ?std) x
2 changes: 1 addition & 1 deletion lib/pretty.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
val format : ?std:bool -> json -> Easy_format.t
val pp : ?std:bool -> Format.formatter -> json -> unit
val to_string : ?std:bool -> json -> string
val to_channel : ?std:bool -> out_channel -> json -> unit
4 changes: 1 addition & 3 deletions lib/write2.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let pretty_format ?std (x : t) =
Pretty.format ?std (x :> json_max)

let pretty_print ?std out (x : t) =
Easy_format.Pretty.to_formatter out (pretty_format ?std x)
Pretty.pp ?std out (x :> json_max)

let pretty_to_string ?std (x : t) =
Pretty.to_string ?std (x :> json_max)
Expand Down
7 changes: 0 additions & 7 deletions lib/write2.mli
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
(** {2 JSON pretty-printing} *)

val pretty_format : ?std:bool -> t -> Easy_format.t
(** Convert into a pretty-printable tree.
See [to_string] for the role of the optional [std] argument.
@see <http://martin.jambon.free.fr/easy-format.html> Easy-format
*)

val pretty_print : ?std:bool -> Format.formatter -> t -> unit
(** Pretty-print into a {!Format.formatter}.
See [to_string] for the role of the optional [std] argument.
Expand Down
1 change: 0 additions & 1 deletion yojson.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ depends: [
"ocaml" {>= "4.02.3"}
"dune"
"cppo" {build}
"easy-format"
"biniou" {>= "1.2.0"}
"alcotest" {with-test & >= "0.8.5"}
]
Expand Down

0 comments on commit 6022b68

Please sign in to comment.