Skip to content

Commit b145688

Browse files
authored
unify: fix tests (#46)
1 parent 708763c commit b145688

15 files changed

+194
-171
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Based on [@glennsl/bs-json](https://github.com/glennsl/bs-json).
88
The Decode module in particular provides a basic set of decoder functions to be
99
composed into more complex decoders. A decoder is a function that takes a
1010
`Js.Json.t` and either returns a value of the desired type if successful or
11-
raises a `DecodeError` exception if not. Other functions accept a decoder and
11+
raises an `Of_json_error` exception if not. Other functions accept a decoder and
1212
produce another decoder. Like `array`, which when given a decoder for type `t`
1313
will return a decoder that tries to produce a value of type `t array`. So to
1414
decode an `int array` you combine `Json.Decode.int` with `Json.Decode.array`

examples/complex.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[@@@alert "-deprecated"]
12
type line = { start : point; end_ : point; thickness : int option }
23
and point = { x : int; y : int }
34

examples/decode.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[@@@alert "-deprecated"]
2+
13
(* Decoding a fixed JSON data structure using Json.Decode *)
24
let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str =
35
let json = Json.parseOrRaise str in
@@ -26,5 +28,5 @@ let _ =
2628
let json = {|{ "y": 42 } |} |> Json.parseOrRaise in
2729
match Json.Decode.(field "x" int json) with
2830
| x -> Js.log x
29-
| exception Json.Decode.DecodeError err ->
30-
Js.log ("Error:" ^ Json.Decode.error_to_string err)
31+
| exception Json.Of_json_error err ->
32+
Js.log ("Error:" ^ Json.of_json_error_to_string err)

examples/dynamicDict_Ocaml.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[@@@alert "-deprecated"]
2+
13
(*
24
Handling an object with dynamic keys for sub-objects.
35
example:

examples/dynamicDict_Reason.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[@alert "-deprecated"];
2+
13
/*
24
Handling an object with dynamic keys for sub-objects.
35
example:

examples/encode.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[@@@alert "-deprecated"]
12
(* Encoding a JSON data structure using Json.Encode *)
23

34
(* prints ["foo", "bar"] *)

examples/parse.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[@@@alert "-deprecated"]
2+
13
(* Parsing a JSON string using Json.parseOrRaise *)
24

35
let arrayOfInts str =

examples/tree.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[@@@alert "-deprecated"]
12
(* Decode a JSON tree structure *)
23
type 'a tree = Node of 'a * 'a tree list | Leaf of 'a
34

ppx/browser/ppx_deriving_json_runtime.ml

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
[@@@alert "-deprecated"]
2+
13
type t = Js.Json.t
24

35
let to_json t = t
46
let of_json t = t
57
let to_string t = Js.Json.stringify t
68

7-
exception Of_string_error of string
8-
99
let of_string s =
1010
try Js.Json.parseExn s
1111
with exn ->
@@ -18,13 +18,13 @@ let of_string s =
1818
(* msg really cannot be None in browser or any sane JS runtime *)
1919
Option.value msg ~default:"JSON error"
2020
in
21-
raise (Of_string_error msg)
21+
raise (Json.Of_string_error msg)
2222

23-
type error = Json.Decode.error =
23+
type error = Json.of_json_error =
2424
| Json_error of string
2525
| Unexpected_variant of string
2626

27-
exception Of_json_error = Json.Decode.DecodeError
27+
exception Of_json_error = Json.Of_json_error
2828

2929
let of_json_error msg = raise (Of_json_error (Json_error msg))
3030

@@ -60,75 +60,16 @@ module To_json = struct
6060
end
6161

6262
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
13273
end
13374

13475
module Primitives = struct
@@ -137,6 +78,10 @@ module Primitives = struct
13778
end
13879

13980
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+
14085
let classify :
14186
t ->
14287
[ `Null
@@ -153,7 +98,7 @@ module Classify = struct
15398
| "string" -> `String (Obj.magic json : string)
15499
| "number" ->
155100
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
157102
| "boolean" -> `Bool (Obj.magic json : bool)
158103
| "object" ->
159104
if Js.Array.isArray json then

ppx/native/ppx_deriving_json_runtime.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ let show_json_type = function
2626
| `String _ -> "string"
2727

2828
let of_json_error_type_mismatch json expected =
29-
of_json_error
30-
("expected " ^ expected ^ " but got " ^ show_json_type json)
29+
of_json_error ("Expected " ^ expected ^ ", got " ^ show_json_type json)
3130

3231
module To_json = struct
3332
let string_to_json v = `String v

0 commit comments

Comments
 (0)