|
| 1 | +We can use the `Ptyp_open` AST node: |
| 2 | + $ echo ' |
| 3 | + > module X = struct |
| 4 | + > type x = (int * int) [@@deriving json] |
| 5 | + > end |
| 6 | + > type u = X.(x) [@@deriving json] |
| 7 | + > let () = print_endline (Ppx_deriving_json_runtime.to_string (u_to_json (1, 2))) |
| 8 | + > let () = assert (u_of_json (Ppx_deriving_json_runtime.of_string {|[1, 2]|}) = (1, 2)) |
| 9 | + > ' | ./run.sh |
| 10 | + === ppx output:native === |
| 11 | + module X = |
| 12 | + struct |
| 13 | + type x = (int * int)[@@deriving json] |
| 14 | + include |
| 15 | + struct |
| 16 | + let _ = fun (_ : x) -> () |
| 17 | + [@@@ocaml.warning "-39-11-27"] |
| 18 | + let rec x_of_json = |
| 19 | + (fun x -> |
| 20 | + match x with |
| 21 | + | `List (x_0::x_1::[]) -> ((int_of_json x_0), (int_of_json x_1)) |
| 22 | + | _ -> |
| 23 | + Ppx_deriving_json_runtime.of_json_error ~json:x |
| 24 | + "expected a JSON array of length 2" : Yojson.Basic.t -> x) |
| 25 | + let _ = x_of_json |
| 26 | + [@@@ocaml.warning "-39-11-27"] |
| 27 | + let rec x_to_json = |
| 28 | + (fun x -> |
| 29 | + match x with |
| 30 | + | (x_0, x_1) -> `List [int_to_json x_0; int_to_json x_1] : |
| 31 | + x -> Yojson.Basic.t) |
| 32 | + let _ = x_to_json |
| 33 | + end[@@ocaml.doc "@inline"][@@merlin.hide ] |
| 34 | + end |
| 35 | + type u = X.(x)[@@deriving json] |
| 36 | + include |
| 37 | + struct |
| 38 | + let _ = fun (_ : u) -> () |
| 39 | + [@@@ocaml.warning "-39-11-27"] |
| 40 | + let rec u_of_json = (fun x -> X.x_of_json x : Yojson.Basic.t -> u) |
| 41 | + let _ = u_of_json |
| 42 | + [@@@ocaml.warning "-39-11-27"] |
| 43 | + let rec u_to_json = (fun x -> X.x_to_json x : u -> Yojson.Basic.t) |
| 44 | + let _ = u_to_json |
| 45 | + end[@@ocaml.doc "@inline"][@@merlin.hide ] |
| 46 | + let () = |
| 47 | + print_endline (Ppx_deriving_json_runtime.to_string (u_to_json (1, 2))) |
| 48 | + let () = |
| 49 | + assert |
| 50 | + ((u_of_json (Ppx_deriving_json_runtime.of_string {|[1, 2]|})) = (1, 2)) |
| 51 | + === ppx output:browser === |
| 52 | + module X = |
| 53 | + struct |
| 54 | + type x = (int * int)[@@deriving json] |
| 55 | + include |
| 56 | + struct |
| 57 | + let _ = fun (_ : x) -> () |
| 58 | + [@@@ocaml.warning "-39-11-27"] |
| 59 | + let rec x_of_json = |
| 60 | + (fun x -> |
| 61 | + if |
| 62 | + Stdlib.(&&) (Js.Array.isArray x) |
| 63 | + (Stdlib.(=) |
| 64 | + (Js.Array.length (Obj.magic x : Js.Json.t array)) 2) |
| 65 | + then |
| 66 | + let es = (Obj.magic x : Js.Json.t array) in |
| 67 | + ((int_of_json (Js.Array.unsafe_get es 0)), |
| 68 | + (int_of_json (Js.Array.unsafe_get es 1))) |
| 69 | + else |
| 70 | + Ppx_deriving_json_runtime.of_json_error ~json:x |
| 71 | + "expected a JSON array of length 2" : Js.Json.t -> x) |
| 72 | + let _ = x_of_json |
| 73 | + [@@@ocaml.warning "-39-11-27"] |
| 74 | + let rec x_to_json = |
| 75 | + (fun x -> |
| 76 | + match x with |
| 77 | + | (x_0, x_1) -> |
| 78 | + (Obj.magic [|(int_to_json x_0);(int_to_json x_1)|] : |
| 79 | + Js.Json.t) : x -> Js.Json.t) |
| 80 | + let _ = x_to_json |
| 81 | + end[@@ocaml.doc "@inline"][@@merlin.hide ] |
| 82 | + end |
| 83 | + type u = X.(x)[@@deriving json] |
| 84 | + include |
| 85 | + struct |
| 86 | + let _ = fun (_ : u) -> () |
| 87 | + [@@@ocaml.warning "-39-11-27"] |
| 88 | + let rec u_of_json = (fun x -> X.x_of_json x : Js.Json.t -> u) |
| 89 | + let _ = u_of_json |
| 90 | + [@@@ocaml.warning "-39-11-27"] |
| 91 | + let rec u_to_json = (fun x -> X.x_to_json x : u -> Js.Json.t) |
| 92 | + let _ = u_to_json |
| 93 | + end[@@ocaml.doc "@inline"][@@merlin.hide ] |
| 94 | + let () = |
| 95 | + print_endline (Ppx_deriving_json_runtime.to_string (u_to_json (1, 2))) |
| 96 | + let () = |
| 97 | + assert |
| 98 | + ((u_of_json (Ppx_deriving_json_runtime.of_string {|[1, 2]|})) = (1, 2)) |
| 99 | + === stdout:native === |
| 100 | + [1,2] |
| 101 | + === stdout:js === |
| 102 | + [1,2] |
0 commit comments