Skip to content

Commit a077b9e

Browse files
committed
ppx: namespace usage of string_to_json on js side
1 parent ead8d43 commit a077b9e

File tree

3 files changed

+114
-27
lines changed

3 files changed

+114
-27
lines changed

ppx/browser/ppx_deriving_json_js.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,21 @@ 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 =
198+
[%expr
199+
Ppx_deriving_json_runtime.Primitives.string_to_json
200+
[%e estring ~loc:n.loc n.txt]]
201+
in
198202
let es = [ derive_of_record derive r es ] in
199203
as_json ~loc (pexp_array ~loc (tag :: es))
200204
| Vcs_tuple (n, t) ->
201205
let loc = n.loc in
202206
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
207+
let tag =
208+
[%expr
209+
Ppx_deriving_json_runtime.Primitives.string_to_json
210+
[%e estring ~loc:n.loc n.txt]]
211+
in
204212
let es = List.map2 t.tpl_types es ~f:derive in
205213
as_json ~loc (pexp_array ~loc (tag :: es))
206214

ppx/test/ppx_deriving_json_js.t

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,23 @@
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 ->
410+
(Obj.magic
411+
[|
412+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
413+
|]
414+
: Js.Json.t)
410415
| B x_0 ->
411-
(Obj.magic [| string_to_json "B"; int_to_json x_0 |]
416+
(Obj.magic
417+
[|
418+
Ppx_deriving_json_runtime.Primitives.string_to_json "B";
419+
int_to_json x_0;
420+
|]
412421
: Js.Json.t)
413422
| C { name = x_name } ->
414423
(Obj.magic
415424
[|
416-
string_to_json "C";
425+
Ppx_deriving_json_runtime.Primitives.string_to_json "C";
417426
(Obj.magic [%mel.obj { name = string_to_json x_name }]
418427
: Js.Json.t);
419428
|]
@@ -472,7 +481,9 @@
472481
| S2 (x_0, x_1) ->
473482
(Obj.magic
474483
[|
475-
string_to_json "S2"; int_to_json x_0; string_to_json x_1;
484+
Ppx_deriving_json_runtime.Primitives.string_to_json "S2";
485+
int_to_json x_0;
486+
string_to_json x_1;
476487
|]
477488
: Js.Json.t)
478489
: sum2 -> Js.Json.t)
@@ -532,7 +543,12 @@
532543
let rec other_to_json =
533544
(fun x ->
534545
match x with
535-
| `C -> (Obj.magic [| string_to_json "C" |] : Js.Json.t)
546+
| `C ->
547+
(Obj.magic
548+
[|
549+
Ppx_deriving_json_runtime.Primitives.string_to_json "C";
550+
|]
551+
: Js.Json.t)
536552
: other -> Js.Json.t)
537553
538554
let _ = other_to_json
@@ -595,9 +611,18 @@
595611
let rec poly_to_json =
596612
(fun x ->
597613
match x with
598-
| `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
614+
| `A ->
615+
(Obj.magic
616+
[|
617+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
618+
|]
619+
: Js.Json.t)
599620
| `B x_0 ->
600-
(Obj.magic [| string_to_json "B"; int_to_json x_0 |]
621+
(Obj.magic
622+
[|
623+
Ppx_deriving_json_runtime.Primitives.string_to_json "B";
624+
int_to_json x_0;
625+
|]
601626
: Js.Json.t)
602627
| #other as x -> other_to_json x
603628
: poly -> Js.Json.t)
@@ -663,7 +688,9 @@
663688
| `P2 (x_0, x_1) ->
664689
(Obj.magic
665690
[|
666-
string_to_json "P2"; int_to_json x_0; string_to_json x_1;
691+
Ppx_deriving_json_runtime.Primitives.string_to_json "P2";
692+
int_to_json x_0;
693+
string_to_json x_1;
667694
|]
668695
: Js.Json.t)
669696
: poly2 -> Js.Json.t)
@@ -721,7 +748,12 @@
721748
fun x ->
722749
match x with
723750
| `C x_0 ->
724-
(Obj.magic [| string_to_json "C"; a_to_json x_0 |] : Js.Json.t)
751+
(Obj.magic
752+
[|
753+
Ppx_deriving_json_runtime.Primitives.string_to_json "C";
754+
a_to_json x_0;
755+
|]
756+
: Js.Json.t)
725757
726758
let _ = c_to_json
727759
end [@@ocaml.doc "@inline"] [@@merlin.hide]
@@ -775,9 +807,18 @@
775807
let rec recur_to_json =
776808
(fun x ->
777809
match x with
778-
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
810+
| A ->
811+
(Obj.magic
812+
[|
813+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
814+
|]
815+
: Js.Json.t)
779816
| Fix x_0 ->
780-
(Obj.magic [| string_to_json "Fix"; recur_to_json x_0 |]
817+
(Obj.magic
818+
[|
819+
Ppx_deriving_json_runtime.Primitives.string_to_json "Fix";
820+
recur_to_json x_0;
821+
|]
781822
: Js.Json.t)
782823
: recur -> Js.Json.t)
783824
@@ -842,9 +883,18 @@
842883
let rec polyrecur_to_json =
843884
(fun x ->
844885
match x with
845-
| `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
886+
| `A ->
887+
(Obj.magic
888+
[|
889+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
890+
|]
891+
: Js.Json.t)
846892
| `Fix x_0 ->
847-
(Obj.magic [| string_to_json "Fix"; polyrecur_to_json x_0 |]
893+
(Obj.magic
894+
[|
895+
Ppx_deriving_json_runtime.Primitives.string_to_json "Fix";
896+
polyrecur_to_json x_0;
897+
|]
848898
: Js.Json.t)
849899
: polyrecur -> Js.Json.t)
850900
@@ -900,8 +950,19 @@
900950
let rec evar_to_json =
901951
(fun x ->
902952
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)
953+
| A ->
954+
(Obj.magic
955+
[|
956+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
957+
|]
958+
: Js.Json.t)
959+
| B ->
960+
(Obj.magic
961+
[|
962+
Ppx_deriving_json_runtime.Primitives.string_to_json
963+
"b_aliased";
964+
|]
965+
: Js.Json.t)
905966
: evar -> Js.Json.t)
906967
907968
let _ = evar_to_json
@@ -964,8 +1025,19 @@
9641025
let rec epoly_to_json =
9651026
(fun x ->
9661027
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)
1028+
| `a ->
1029+
(Obj.magic
1030+
[|
1031+
Ppx_deriving_json_runtime.Primitives.string_to_json
1032+
"A_aliased";
1033+
|]
1034+
: Js.Json.t)
1035+
| `b ->
1036+
(Obj.magic
1037+
[|
1038+
Ppx_deriving_json_runtime.Primitives.string_to_json "b";
1039+
|]
1040+
: Js.Json.t)
9691041
: epoly -> Js.Json.t)
9701042
9711043
let _ = epoly_to_json
@@ -1019,9 +1091,19 @@
10191091
fun x ->
10201092
match x with
10211093
| A x_0 ->
1022-
(Obj.magic [| string_to_json "A"; a_to_json x_0 |] : Js.Json.t)
1094+
(Obj.magic
1095+
[|
1096+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
1097+
a_to_json x_0;
1098+
|]
1099+
: Js.Json.t)
10231100
| B x_0 ->
1024-
(Obj.magic [| string_to_json "B"; b_to_json x_0 |] : Js.Json.t)
1101+
(Obj.magic
1102+
[|
1103+
Ppx_deriving_json_runtime.Primitives.string_to_json "B";
1104+
b_to_json x_0;
1105+
|]
1106+
: Js.Json.t)
10251107
10261108
let _ = p2_to_json
10271109
end [@@ocaml.doc "@inline"] [@@merlin.hide]
@@ -1147,7 +1229,7 @@
11471229
| A { a = x_a } ->
11481230
(Obj.magic
11491231
[|
1150-
string_to_json "A";
1232+
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
11511233
(Obj.magic [%mel.obj { a = int_to_json x_a }] : Js.Json.t);
11521234
|]
11531235
: Js.Json.t)

ppx/test/ppx_deriving_json_js_variants.e2e.t

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
> let json = sum_to_json A
2222
> ' >> main.ml
2323

24+
Can build without having to open Ppx_deriving_json_runtime.Primitives
25+
2426
$ dune build @js
25-
File "main.ml", line 2, characters 11-12:
26-
2 | type sum = A [@@deriving json]
27-
^
28-
Error: Unbound value string_to_json
29-
[1]

0 commit comments

Comments
 (0)