diff --git a/server/json.ml b/server/json.ml index b8cefe0..82704e9 100644 --- a/server/json.ml +++ b/server/json.ml @@ -38,24 +38,49 @@ let str_to_string s = let rec to_string j = match j with - | Num f -> + | Num f -> if float_of_int (int_of_float f) = f then string_of_int (int_of_float f) else string_of_float f - | Str s -> + | Str s -> str_to_string s - | Arr js -> + | Arr js -> "[" ^ String.concat "," (List.map to_string js) ^ "]" - | Rec kvs -> + | Rec kvs -> "{" ^ String.concat "," (List.map kv_to_string kvs) ^ "}" - | Flg b -> + | Flg b -> if b then "true" else "false" - | Nil -> + | Nil -> "null" - and kv_to_string (k, v) = str_to_string k ^ ":" ^ to_string v +let rec pretty_string ?indent:(indent=2) j = + let rec walk current j = + match j with + | Num f -> + if float_of_int (int_of_float f) = f + then string_of_int (int_of_float f) + else string_of_float f + | Str s -> + str_to_string s + | Arr js -> + "[" ^ String.concat ", " (List.map (walk current) js) ^ "]" + | Rec [] -> + "{}" + | Rec [(k, v)] -> + "{" ^ str_to_string k ^ ": " ^ walk current v ^ "}" + | Rec kvs -> + "{" ^ String.concat "," (List.map (kv_walk (current + indent)) kvs) ^ + "\n" ^ String.make current ' ' ^ "}" + | Flg b -> + if b then "true" else "false" + | Nil -> + "null" + and kv_walk current (k, v) = + "\n" ^ String.make current ' ' ^ str_to_string k ^ ": " ^ walk current v + in walk 0 j + let accumulate_utf8 codepoint (acc, len) = (* Of course, at the moment, the codepoint is limited to 16 bits... *) if codepoint < 0x80 then