@@ 67,6 67,7 @@ signature JSON = sig
val parse : string -> json result
val serialise : json -> string
+ val serialiseIndented : json -> string
end
@@ 367,6 368,33 @@ structure Json :> JSON = struct
| STRING s => "\"" ^ stringEscape s ^ "\""
| BOOL b => Bool.toString b
| NULL => "null"
+
+ fun serialiseIndented json =
+ let fun indent 0 = ""
+ | indent i = " " ^ indent (i - 1)
+ fun serialiseIndented' i json =
+ let val ser = serialiseIndented' (i + 1)
+ in
+ case json of
+ OBJECT [] => "{}"
+ | ARRAY [] => "[]"
+ | OBJECT pp => "{\n" ^ indent (i + 1) ^
+ String.concatWith
+ (",\n" ^ indent (i + 1))
+ (map (fn (key, value) =>
+ ser (STRING key) ^ ": " ^
+ ser value) pp) ^
+ "\n" ^ indent i ^ "}"
+ | ARRAY arr => "[\n" ^ indent (i + 1) ^
+ String.concatWith
+ (",\n" ^ indent (i + 1))
+ (map ser arr) ^
+ "\n" ^ indent i ^ "]"
+ | other => serialise other
+ end
+ in
+ serialiseIndented' 0 json ^ "\n"
+ end
end