It’s been ages since I worked with Format; the ability to do so with printf-like formats is quite nice. I just tried out Fmt, and following Drup’s hints, got a JSON pretty-printer working, albeit not as perfectly as I’d like. I can’t speak to whether Fmt is a better way to go than easy-format (I need to try out easy-format also) but at least, Drup seems to be correct, that you can write nontrivial formatters this way.
type json =
JInt of int
| JQString of string
| JAssoc of (string * json) list
| JList of json list
open Fmt
let qstring ppf v = pf ppf "%S" v
let label ppf s = pf ppf "%S: " s
let rec pprec (f, v) ppf = function
| JInt n -> pf ppf "%a%a" f v int n
| JQString s -> pf ppf "%a%a" f v qstring s
| JAssoc l ->
pf ppf {|@[<hv2>%a{@ %a@]@ }|}
f v
Fmt.(list ~sep:comma ppfield) l
| JList l ->
pf ppf {|@[<hv2>%a[@ %a@]@ ]|}
f v Fmt.(list ~sep:comma (pprec (string, ""))) l
and ppfield ppf (k,v) = pprec (label, k) ppf v
let pptop ppf j = pf ppf "@[<hv>%a@]@." (pprec (string, "")) j
let pp j = pptop std_formatter j
let j1 = JAssoc [
"field", JAssoc[ "number", JInt 123;
"text", JQString "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ] ;
"field2", JList [ JInt 1; JInt 2; JInt 3; JInt 4; JInt 5; JInt 6; JInt 7; JInt 8; JInt 9; JInt 10 ]
]
with output
# Json.pp Json.j1 ;;
{
"field": {
"number": 123,
"text": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
"field2": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
]
}
One observation is that even with Fmt (or Format) it’s a long distance between theoretically understanding the meaning of the various boxes and breaks, and actually being able to use them to produce pretty output.