~james/scope

ref: 6e057d536590a73e00a473ec6176185a088491b2 scope/lib/json.ml -rw-r--r-- 1.4 KiB
6e057d53 — james chore(import): snapshot james.dad.town/scope at 034e6b67d44c 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type t = Null | Bool of bool | Int of int | String of string | List of t list | Object of (string * t) list

let string s = String s
let hex x = String (Address.hex x)
let option f = function None -> Null | Some x -> f x

let rec output channel = function
  | Null -> output_string channel "null"
  | Bool b -> output_string channel (string_of_bool b)
  | Int n -> output_string channel (string_of_int n)
  | String s ->
      output_char channel '"';
      String.iter (function
        | '"' -> output_string channel "\\\""
        | '\\' -> output_string channel "\\\\"
        | '\n' -> output_string channel "\\n"
        | '\r' -> output_string channel "\\r"
        | '\t' -> output_string channel "\\t"
        | c when Char.code c < 32 || Char.code c > 126 ->
            Printf.fprintf channel "\\u%04x" (Char.code c)
        | c -> output_char channel c) s;
      output_char channel '"'
  | List values ->
      output_char channel '[';
      List.iteri (fun i value -> if i > 0 then output_char channel ','; output channel value) values;
      output_char channel ']'
  | Object fields ->
      output_char channel '{';
      List.iteri (fun i (key, value) ->
        if i > 0 then output_char channel ',';
        output channel (String key);
        output_char channel ':';
        output channel value) fields;
      output_char channel '}'

let print value = output stdout value; output_char stdout '\n'