diff --git a/CHANGES.md b/CHANGES.md index 881f751..5ef2198 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,8 @@ ([#30](https://github.com/melange-community/melange-json/pull/30)) - PPX: add support for `int64` in the runtime ([#33](https://github.com/melange-community/melange-json/pull/33)) +- PPX: remove `string_to_json` usage on js side + ([#35](https://github.com/melange-community/melange-json/pull/35)) ## 1.3.0 (2024-08-28) diff --git a/ppx/browser/ppx_deriving_json_js.ml b/ppx/browser/ppx_deriving_json_js.ml index 3ebf157..5437daf 100644 --- a/ppx/browser/ppx_deriving_json_js.ml +++ b/ppx/browser/ppx_deriving_json_js.ml @@ -194,13 +194,13 @@ module To_json = struct | Vcs_record (n, r) -> let loc = n.loc in let n = Option.value ~default:n (vcs_attr_json_name r.rcd_ctx) in - let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in + let tag = [%expr (Obj.magic [%e estring ~loc:n.loc n.txt]: Js.Json.t)] in let es = [ derive_of_record derive r es ] in as_json ~loc (pexp_array ~loc (tag :: es)) | Vcs_tuple (n, t) -> let loc = n.loc in let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in - let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in + let tag = [%expr (Obj.magic [%e estring ~loc:n.loc n.txt]: Js.Json.t)] in let es = List.map2 t.tpl_types es ~f:derive in as_json ~loc (pexp_array ~loc (tag :: es)) diff --git a/ppx/test/ppx_deriving_json_js.t b/ppx/test/ppx_deriving_json_js.t index d7eab10..3745e1c 100644 --- a/ppx/test/ppx_deriving_json_js.t +++ b/ppx/test/ppx_deriving_json_js.t @@ -406,14 +406,14 @@ let rec sum_to_json = (fun x -> match x with - | A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t) + | A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t) | B x_0 -> - (Obj.magic [| string_to_json "B"; int_to_json x_0 |] + (Obj.magic [| (Obj.magic "B" : Js.Json.t); int_to_json x_0 |] : Js.Json.t) | C { name = x_name } -> (Obj.magic [| - string_to_json "C"; + (Obj.magic "C" : Js.Json.t); (Obj.magic [%mel.obj { name = string_to_json x_name }] : Js.Json.t); |] @@ -472,7 +472,9 @@ | S2 (x_0, x_1) -> (Obj.magic [| - string_to_json "S2"; int_to_json x_0; string_to_json x_1; + (Obj.magic "S2" : Js.Json.t); + int_to_json x_0; + string_to_json x_1; |] : Js.Json.t) : sum2 -> Js.Json.t) @@ -532,7 +534,7 @@ let rec other_to_json = (fun x -> match x with - | `C -> (Obj.magic [| string_to_json "C" |] : Js.Json.t) + | `C -> (Obj.magic [| (Obj.magic "C" : Js.Json.t) |] : Js.Json.t) : other -> Js.Json.t) let _ = other_to_json @@ -595,9 +597,9 @@ let rec poly_to_json = (fun x -> match x with - | `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t) + | `A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t) | `B x_0 -> - (Obj.magic [| string_to_json "B"; int_to_json x_0 |] + (Obj.magic [| (Obj.magic "B" : Js.Json.t); int_to_json x_0 |] : Js.Json.t) | #other as x -> other_to_json x : poly -> Js.Json.t) @@ -663,7 +665,9 @@ | `P2 (x_0, x_1) -> (Obj.magic [| - string_to_json "P2"; int_to_json x_0; string_to_json x_1; + (Obj.magic "P2" : Js.Json.t); + int_to_json x_0; + string_to_json x_1; |] : Js.Json.t) : poly2 -> Js.Json.t) @@ -721,7 +725,8 @@ fun x -> match x with | `C x_0 -> - (Obj.magic [| string_to_json "C"; a_to_json x_0 |] : Js.Json.t) + (Obj.magic [| (Obj.magic "C" : Js.Json.t); a_to_json x_0 |] + : Js.Json.t) let _ = c_to_json end [@@ocaml.doc "@inline"] [@@merlin.hide] @@ -775,9 +780,10 @@ let rec recur_to_json = (fun x -> match x with - | A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t) + | A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t) | Fix x_0 -> - (Obj.magic [| string_to_json "Fix"; recur_to_json x_0 |] + (Obj.magic + [| (Obj.magic "Fix" : Js.Json.t); recur_to_json x_0 |] : Js.Json.t) : recur -> Js.Json.t) @@ -842,9 +848,10 @@ let rec polyrecur_to_json = (fun x -> match x with - | `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t) + | `A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t) | `Fix x_0 -> - (Obj.magic [| string_to_json "Fix"; polyrecur_to_json x_0 |] + (Obj.magic + [| (Obj.magic "Fix" : Js.Json.t); polyrecur_to_json x_0 |] : Js.Json.t) : polyrecur -> Js.Json.t) @@ -900,8 +907,10 @@ let rec evar_to_json = (fun x -> match x with - | A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t) - | B -> (Obj.magic [| string_to_json "b_aliased" |] : Js.Json.t) + | A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t) + | B -> + (Obj.magic [| (Obj.magic "b_aliased" : Js.Json.t) |] + : Js.Json.t) : evar -> Js.Json.t) let _ = evar_to_json @@ -964,8 +973,10 @@ let rec epoly_to_json = (fun x -> match x with - | `a -> (Obj.magic [| string_to_json "A_aliased" |] : Js.Json.t) - | `b -> (Obj.magic [| string_to_json "b" |] : Js.Json.t) + | `a -> + (Obj.magic [| (Obj.magic "A_aliased" : Js.Json.t) |] + : Js.Json.t) + | `b -> (Obj.magic [| (Obj.magic "b" : Js.Json.t) |] : Js.Json.t) : epoly -> Js.Json.t) let _ = epoly_to_json @@ -1019,9 +1030,11 @@ fun x -> match x with | A x_0 -> - (Obj.magic [| string_to_json "A"; a_to_json x_0 |] : Js.Json.t) + (Obj.magic [| (Obj.magic "A" : Js.Json.t); a_to_json x_0 |] + : Js.Json.t) | B x_0 -> - (Obj.magic [| string_to_json "B"; b_to_json x_0 |] : Js.Json.t) + (Obj.magic [| (Obj.magic "B" : Js.Json.t); b_to_json x_0 |] + : Js.Json.t) let _ = p2_to_json end [@@ocaml.doc "@inline"] [@@merlin.hide] @@ -1147,7 +1160,7 @@ | A { a = x_a } -> (Obj.magic [| - string_to_json "A"; + (Obj.magic "A" : Js.Json.t); (Obj.magic [%mel.obj { a = int_to_json x_a }] : Js.Json.t); |] : Js.Json.t) diff --git a/ppx/test/ppx_deriving_json_js_variants.e2e.t b/ppx/test/ppx_deriving_json_js_variants.e2e.t new file mode 100644 index 0000000..27fa139 --- /dev/null +++ b/ppx/test/ppx_deriving_json_js_variants.e2e.t @@ -0,0 +1,26 @@ + + $ echo '(lang dune 3.11) + > (using melange 0.1)' > dune-project + + $ echo ' + > (library + > (name lib) + > (modes melange) + > (modules main) + > (flags :standard -w -37-69) + > (preprocess (pps melange.ppx melange-json.ppx))) + > (melange.emit + > (alias js) + > (target output) + > (modules) + > (libraries lib) + > (module_systems commonjs))' > dune + + $ echo ' + > type sum = A [@@deriving json] + > let json = sum_to_json A + > ' >> main.ml + +Can build without having to open Ppx_deriving_json_runtime.Primitives + + $ dune build @js