Skip to content

Commit

Permalink
ppx: remove string_to_json usage on js side (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri authored Nov 16, 2024
1 parent 1445379 commit ac0f795
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions ppx/browser/ppx_deriving_json_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
53 changes: 33 additions & 20 deletions ppx/test/ppx_deriving_json_js.t
Original file line number Diff line number Diff line change
Expand Up @@ -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);
|]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions ppx/test/ppx_deriving_json_js_variants.e2e.t
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ac0f795

Please sign in to comment.