Skip to content

Commit ac0f795

Browse files
authored
ppx: remove string_to_json usage on js side (#35)
1 parent 1445379 commit ac0f795

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
([#30](https://github.com/melange-community/melange-json/pull/30))
2020
- PPX: add support for `int64` in the runtime
2121
([#33](https://github.com/melange-community/melange-json/pull/33))
22+
- PPX: remove `string_to_json` usage on js side
23+
([#35](https://github.com/melange-community/melange-json/pull/35))
2224

2325
## 1.3.0 (2024-08-28)
2426

ppx/browser/ppx_deriving_json_js.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ module To_json = struct
194194
| Vcs_record (n, r) ->
195195
let loc = n.loc in
196196
let n = Option.value ~default:n (vcs_attr_json_name r.rcd_ctx) in
197-
let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in
197+
let tag = [%expr (Obj.magic [%e estring ~loc:n.loc n.txt]: Js.Json.t)] in
198198
let es = [ derive_of_record derive r es ] in
199199
as_json ~loc (pexp_array ~loc (tag :: es))
200200
| Vcs_tuple (n, t) ->
201201
let loc = n.loc in
202202
let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in
203-
let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in
203+
let tag = [%expr (Obj.magic [%e estring ~loc:n.loc n.txt]: Js.Json.t)] in
204204
let es = List.map2 t.tpl_types es ~f:derive in
205205
as_json ~loc (pexp_array ~loc (tag :: es))
206206

ppx/test/ppx_deriving_json_js.t

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@
406406
let rec sum_to_json =
407407
(fun x ->
408408
match x with
409-
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
409+
| A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t)
410410
| B x_0 ->
411-
(Obj.magic [| string_to_json "B"; int_to_json x_0 |]
411+
(Obj.magic [| (Obj.magic "B" : Js.Json.t); int_to_json x_0 |]
412412
: Js.Json.t)
413413
| C { name = x_name } ->
414414
(Obj.magic
415415
[|
416-
string_to_json "C";
416+
(Obj.magic "C" : Js.Json.t);
417417
(Obj.magic [%mel.obj { name = string_to_json x_name }]
418418
: Js.Json.t);
419419
|]
@@ -472,7 +472,9 @@
472472
| S2 (x_0, x_1) ->
473473
(Obj.magic
474474
[|
475-
string_to_json "S2"; int_to_json x_0; string_to_json x_1;
475+
(Obj.magic "S2" : Js.Json.t);
476+
int_to_json x_0;
477+
string_to_json x_1;
476478
|]
477479
: Js.Json.t)
478480
: sum2 -> Js.Json.t)
@@ -532,7 +534,7 @@
532534
let rec other_to_json =
533535
(fun x ->
534536
match x with
535-
| `C -> (Obj.magic [| string_to_json "C" |] : Js.Json.t)
537+
| `C -> (Obj.magic [| (Obj.magic "C" : Js.Json.t) |] : Js.Json.t)
536538
: other -> Js.Json.t)
537539
538540
let _ = other_to_json
@@ -595,9 +597,9 @@
595597
let rec poly_to_json =
596598
(fun x ->
597599
match x with
598-
| `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
600+
| `A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t)
599601
| `B x_0 ->
600-
(Obj.magic [| string_to_json "B"; int_to_json x_0 |]
602+
(Obj.magic [| (Obj.magic "B" : Js.Json.t); int_to_json x_0 |]
601603
: Js.Json.t)
602604
| #other as x -> other_to_json x
603605
: poly -> Js.Json.t)
@@ -663,7 +665,9 @@
663665
| `P2 (x_0, x_1) ->
664666
(Obj.magic
665667
[|
666-
string_to_json "P2"; int_to_json x_0; string_to_json x_1;
668+
(Obj.magic "P2" : Js.Json.t);
669+
int_to_json x_0;
670+
string_to_json x_1;
667671
|]
668672
: Js.Json.t)
669673
: poly2 -> Js.Json.t)
@@ -721,7 +725,8 @@
721725
fun x ->
722726
match x with
723727
| `C x_0 ->
724-
(Obj.magic [| string_to_json "C"; a_to_json x_0 |] : Js.Json.t)
728+
(Obj.magic [| (Obj.magic "C" : Js.Json.t); a_to_json x_0 |]
729+
: Js.Json.t)
725730
726731
let _ = c_to_json
727732
end [@@ocaml.doc "@inline"] [@@merlin.hide]
@@ -775,9 +780,10 @@
775780
let rec recur_to_json =
776781
(fun x ->
777782
match x with
778-
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
783+
| A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t)
779784
| Fix x_0 ->
780-
(Obj.magic [| string_to_json "Fix"; recur_to_json x_0 |]
785+
(Obj.magic
786+
[| (Obj.magic "Fix" : Js.Json.t); recur_to_json x_0 |]
781787
: Js.Json.t)
782788
: recur -> Js.Json.t)
783789
@@ -842,9 +848,10 @@
842848
let rec polyrecur_to_json =
843849
(fun x ->
844850
match x with
845-
| `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
851+
| `A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t)
846852
| `Fix x_0 ->
847-
(Obj.magic [| string_to_json "Fix"; polyrecur_to_json x_0 |]
853+
(Obj.magic
854+
[| (Obj.magic "Fix" : Js.Json.t); polyrecur_to_json x_0 |]
848855
: Js.Json.t)
849856
: polyrecur -> Js.Json.t)
850857
@@ -900,8 +907,10 @@
900907
let rec evar_to_json =
901908
(fun x ->
902909
match x with
903-
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
904-
| B -> (Obj.magic [| string_to_json "b_aliased" |] : Js.Json.t)
910+
| A -> (Obj.magic [| (Obj.magic "A" : Js.Json.t) |] : Js.Json.t)
911+
| B ->
912+
(Obj.magic [| (Obj.magic "b_aliased" : Js.Json.t) |]
913+
: Js.Json.t)
905914
: evar -> Js.Json.t)
906915
907916
let _ = evar_to_json
@@ -964,8 +973,10 @@
964973
let rec epoly_to_json =
965974
(fun x ->
966975
match x with
967-
| `a -> (Obj.magic [| string_to_json "A_aliased" |] : Js.Json.t)
968-
| `b -> (Obj.magic [| string_to_json "b" |] : Js.Json.t)
976+
| `a ->
977+
(Obj.magic [| (Obj.magic "A_aliased" : Js.Json.t) |]
978+
: Js.Json.t)
979+
| `b -> (Obj.magic [| (Obj.magic "b" : Js.Json.t) |] : Js.Json.t)
969980
: epoly -> Js.Json.t)
970981
971982
let _ = epoly_to_json
@@ -1019,9 +1030,11 @@
10191030
fun x ->
10201031
match x with
10211032
| A x_0 ->
1022-
(Obj.magic [| string_to_json "A"; a_to_json x_0 |] : Js.Json.t)
1033+
(Obj.magic [| (Obj.magic "A" : Js.Json.t); a_to_json x_0 |]
1034+
: Js.Json.t)
10231035
| B x_0 ->
1024-
(Obj.magic [| string_to_json "B"; b_to_json x_0 |] : Js.Json.t)
1036+
(Obj.magic [| (Obj.magic "B" : Js.Json.t); b_to_json x_0 |]
1037+
: Js.Json.t)
10251038
10261039
let _ = p2_to_json
10271040
end [@@ocaml.doc "@inline"] [@@merlin.hide]
@@ -1147,7 +1160,7 @@
11471160
| A { a = x_a } ->
11481161
(Obj.magic
11491162
[|
1150-
string_to_json "A";
1163+
(Obj.magic "A" : Js.Json.t);
11511164
(Obj.magic [%mel.obj { a = int_to_json x_a }] : Js.Json.t);
11521165
|]
11531166
: Js.Json.t)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
$ echo '(lang dune 3.11)
3+
> (using melange 0.1)' > dune-project
4+
5+
$ echo '
6+
> (library
7+
> (name lib)
8+
> (modes melange)
9+
> (modules main)
10+
> (flags :standard -w -37-69)
11+
> (preprocess (pps melange.ppx melange-json.ppx)))
12+
> (melange.emit
13+
> (alias js)
14+
> (target output)
15+
> (modules)
16+
> (libraries lib)
17+
> (module_systems commonjs))' > dune
18+
19+
$ echo '
20+
> type sum = A [@@deriving json]
21+
> let json = sum_to_json A
22+
> ' >> main.ml
23+
24+
Can build without having to open Ppx_deriving_json_runtime.Primitives
25+
26+
$ dune build @js

0 commit comments

Comments
 (0)