package melange-json-native

  1. Overview
  2. Docs
Compositional JSON encode/decode PPX for OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

melange-json-1.3.0.tbz
sha256=9ed376e19793c536f8a8a388f0e1ce7e402d1fde85de4e941ab5bd1190b25ac5
sha512=3b66695707a6a7cf9fed59fef9ddb02504a4e85d14dd904764ea049c4e92d0910e1d68b4edfe2b8a1d2e1c984bd061d01d3866dd575bfd7c0573ff5a4865c616

doc/src/melange-json-native.ppx-runtime/ppx_deriving_json_runtime.ml.html

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.