1
+ [@@@ alert " -deprecated" ]
2
+
1
3
type t = Js.Json .t
2
4
3
5
let to_json t = t
4
6
let of_json t = t
5
7
let to_string t = Js.Json. stringify t
6
8
7
- exception Of_string_error of string
8
-
9
9
let of_string s =
10
10
try Js.Json. parseExn s
11
11
with exn ->
@@ -18,13 +18,13 @@ let of_string s =
18
18
(* msg really cannot be None in browser or any sane JS runtime *)
19
19
Option. value msg ~default: " JSON error"
20
20
in
21
- raise (Of_string_error msg)
21
+ raise (Json. Of_string_error msg)
22
22
23
- type error = Json.Decode .error =
23
+ type error = Json .of_json_error =
24
24
| Json_error of string
25
25
| Unexpected_variant of string
26
26
27
- exception Of_json_error = Json.Decode. DecodeError
27
+ exception Of_json_error = Json. Of_json_error
28
28
29
29
let of_json_error msg = raise (Of_json_error (Json_error msg))
30
30
@@ -60,75 +60,16 @@ module To_json = struct
60
60
end
61
61
62
62
module Of_json = struct
63
- let string_of_json (json : t ) : string =
64
- if Js. typeof json = " string" then (Obj. magic json : string )
65
- else of_json_error " expected a string"
66
-
67
- let bool_of_json (json : t ) : bool =
68
- if Js. typeof json = " boolean" then (Obj. magic json : bool )
69
- else of_json_error " expected a boolean"
70
-
71
- let is_int value =
72
- Js.Float. isFinite value && Js.Math. floor_float value == value
73
-
74
- let int_of_json (json : t ) : int =
75
- if Js. typeof json = " number" then
76
- let v = (Obj. magic json : float ) in
77
- if is_int v then (Obj. magic v : int )
78
- else of_json_error " expected an integer"
79
- else of_json_error " expected an integer"
80
-
81
- let int64_of_json (json : t ) : int64 =
82
- if Js. typeof json = " string" then
83
- let v = (Obj. magic json : string ) in
84
- match Int64. of_string_opt v with
85
- | Some v -> v
86
- | None -> of_json_error " expected int64 as string"
87
- else of_json_error " expected int64 as string"
88
-
89
- let float_of_json (json : t ) : float =
90
- if Js. typeof json = " number" then (Obj. magic json : float )
91
- else of_json_error " expected a float"
92
-
93
- let unit_of_json (json : t ) =
94
- if (Obj. magic json : 'a Js.null ) == Js. null then ()
95
- else of_json_error " expected null"
96
-
97
- let array_of_json v_of_json (json : t ) =
98
- if Js.Array. isArray json then
99
- let json = (Obj. magic json : Js.Json.t array ) in
100
- Js.Array. map ~f: v_of_json json
101
- else of_json_error " expected a JSON array"
102
-
103
- let list_of_json v_of_json (json : t ) =
104
- array_of_json v_of_json json |> Array. to_list
105
-
106
- let option_of_json v_of_json (json : t ) =
107
- if (Obj. magic json : 'a Js.null ) == Js. null then None
108
- else Some (v_of_json json)
109
-
110
- let result_of_json ok_of_json err_of_json (json : t ) =
111
- if Js.Array. isArray json then
112
- let array = (Obj. magic json : Js.Json.t array ) in
113
- let len = Js.Array. length array in
114
- if Stdlib. ( > ) len 0 then
115
- let tag = Js.Array. unsafe_get array 0 in
116
- if Stdlib. ( = ) (Js. typeof tag) " string" then
117
- let tag = (Obj. magic tag : string ) in
118
- if Stdlib. ( = ) tag " Ok" then (
119
- if Stdlib. ( <> ) len 2 then
120
- of_json_error " expected a JSON array of length 2" ;
121
- Ok (ok_of_json (Js.Array. unsafe_get array 1 )))
122
- else if Stdlib. ( = ) tag " Error" then (
123
- if Stdlib. ( <> ) len 2 then
124
- of_json_error " expected a JSON array of length 2" ;
125
- Error (err_of_json (Js.Array. unsafe_get array 1 )))
126
- else of_json_error " invalid JSON"
127
- else
128
- of_json_error
129
- " expected a non empty JSON array with element being a string"
130
- else of_json_error " expected a non empty JSON array"
131
- else of_json_error " expected a non empty JSON array"
63
+ let string_of_json = Json.Of_json. string
64
+ let bool_of_json = Json.Of_json. bool
65
+ let int_of_json = Json.Of_json. int
66
+ let int64_of_json = Json.Of_json. int64
67
+ let float_of_json = Json.Of_json. float
68
+ let unit_of_json = Json.Of_json. unit
69
+ let array_of_json = Json.Of_json. array
70
+ let list_of_json = Json.Of_json. list
71
+ let option_of_json = Json.Of_json. option
72
+ let result_of_json = Json.Of_json. result
132
73
end
133
74
134
75
module Primitives = struct
@@ -137,6 +78,10 @@ module Primitives = struct
137
78
end
138
79
139
80
module Classify = struct
81
+ (* This function is also defined in `Json` module, but not exposed on its mli *)
82
+ let is_int value =
83
+ Js.Float. isFinite value && Js.Math. floor_float value == value
84
+
140
85
let classify :
141
86
t ->
142
87
[ `Null
@@ -153,7 +98,7 @@ module Classify = struct
153
98
| "string" -> `String (Obj. magic json : string )
154
99
| "number" ->
155
100
let v = (Obj. magic json : float ) in
156
- if Of_json. is_int v then `Int (Obj. magic v : int ) else `Float v
101
+ if is_int v then `Int (Obj. magic v : int ) else `Float v
157
102
| "boolean" -> `Bool (Obj. magic json : bool )
158
103
| "object" ->
159
104
if Js.Array. isArray json then
0 commit comments