package melange-json

  1. Overview
  2. Docs

Source file ppx_deriving_json_runtime.ml

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
type t = Yojson.Basic.t

let to_json t = t
let of_json t = t
let to_string t = Yojson.Basic.to_string t
let of_string s = Yojson.Basic.from_string s

exception Of_json_error of string

let of_json_error msg = raise (Of_json_error msg)

module To_json = struct
  let string_to_json v = `String v
  let bool_to_json v = `Bool v
  let int_to_json v = `Int v
  let float_to_json v = `Float v
  let unit_to_json () = `Null
  let list_to_json v_to_json vs = `List (List.map v_to_json vs)

  let option_to_json v_to_json = function
    | None -> `Null
    | Some v -> v_to_json v
end

module Of_json = struct
  let string_of_json = Yojson.Basic.Util.to_string
  let bool_of_json = Yojson.Basic.Util.to_bool
  let int_of_json = Yojson.Basic.Util.to_int
  let float_of_json = Yojson.Basic.Util.to_float

  let unit_of_json = function
    | `Null -> ()
    | _ -> of_json_error "expected null"

  let option_of_json v_of_json = Yojson.Basic.Util.to_option v_of_json

  let list_of_json v_of_json json =
    List.map v_of_json (Yojson.Basic.Util.to_list json)
end

module Primitives = struct
  include To_json
  include Of_json
end

module Classify = struct
  let classify :
      t ->
      [ `Null
      | `String of string
      | `Float of float
      | `Int of int
      | `Bool of bool
      | `List of t list
      | `Assoc of (string * t) list ] =
   fun x -> x
end
OCaml

Innovation. Community. Security.