package jsonrpc

  1. Overview
  2. Docs

Source file import.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
module Option = struct
  let map t ~f =
    match t with
    | None -> None
    | Some x -> Some (f x)
end

module Json = struct
  type t =
    [ `Assoc of (string * t) list
    | `Bool of bool
    | `Float of float
    | `Int of int
    | `Intlit of string
    | `List of t list
    | `Null
    | `String of string
    | `Tuple of t list
    | `Variant of string * t option
    ]

  exception Of_json of (string * t)

  let () =
    Printexc.register_printer (function
      | Of_json (msg, _) -> Some ("Jsonrpc: json conversion failed: " ^ msg)
      | _ -> None)

  let error msg json = raise (Of_json (msg, json))

  module Jsonable = struct
    module type S = sig
      type json

      type t

      val yojson_of_t : t -> json

      val t_of_yojson : json -> t
    end
    with type json := t
  end

  let field fields name conv = List.assoc_opt name fields |> Option.map ~f:conv

  let field_exn fields name conv =
    match field fields name conv with
    | Some f -> f
    | None -> error ("missing field " ^ name) (`Assoc fields)

  module Conv = struct
    let string_of_yojson = function
      | `String s -> s
      | json -> error "expected string" json
  end
end
OCaml

Innovation. Community. Security.