Skip to content

Commit cbaadd0

Browse files
committed
ppx: add int64 to runtime
1 parent 10ef727 commit cbaadd0

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

ppx/browser/ppx_deriving_json_runtime.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ module To_json = struct
2828
external string_to_json : string -> t = "%identity"
2929
external bool_to_json : bool -> t = "%identity"
3030
external int_to_json : int -> t = "%identity"
31+
32+
let int64_to_json : int64 -> t = fun v -> Obj.magic (Int64.to_string v)
33+
3134
external float_to_json : float -> t = "%identity"
3235

3336
let unit_to_json () : t = Obj.magic Js.null
@@ -68,6 +71,14 @@ module Of_json = struct
6871
else of_json_error "expected an integer"
6972
else of_json_error "expected an integer"
7073

74+
let int64_of_json (json : t) : int64 =
75+
if Js.typeof json = "string" then
76+
let v = (Obj.magic json : string) in
77+
match Int64.of_string_opt v with
78+
| Some v -> v
79+
| None -> of_json_error "expected an int64"
80+
else of_json_error "expected a string"
81+
7182
let float_of_json (json : t) : float =
7283
if Js.typeof json = "number" then (Obj.magic json : float)
7384
else of_json_error "expected a float"

ppx/native/ppx_deriving_json_runtime.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module To_json = struct
3232
let string_to_json v = `String v
3333
let bool_to_json v = `Bool v
3434
let int_to_json v = `Int v
35+
let int64_to_json v = `String (Int64.to_string v)
3536
let float_to_json v = `Float v
3637
let unit_to_json () = `Null
3738
let list_to_json v_to_json vs = `List (List.map v_to_json vs)
@@ -68,6 +69,13 @@ module Of_json = struct
6869
| `Int i -> i
6970
| json -> of_json_error_type_mismatch json "int"
7071

72+
let int64_of_json = function
73+
| `String i as json -> (
74+
match Int64.of_string_opt i with
75+
| Some v -> v
76+
| None -> of_json_error_type_mismatch json "string")
77+
| json -> of_json_error_type_mismatch json "string"
78+
7179
let float_of_json = function
7280
| `Float f -> f
7381
| `Int i -> float_of_int i

ppx/test/example.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type user = int [@@deriving json]
2+
type userid = int64 [@@deriving json]
23
type floaty = float [@@deriving json]
34
type 'a param = 'a [@@deriving json]
45
type opt = string option [@@deriving json]
@@ -25,6 +26,7 @@ type json = Ppx_deriving_json_runtime.t
2526
type of_json = C : string * (json -> 'a) * ('a -> json) * 'a -> of_json
2627
let of_json_cases = [
2728
C ({|1|}, user_of_json, user_to_json, 1);
29+
C ({|"9223372036854775807"|}, userid_of_json, userid_to_json, 9223372036854775807L);
2830
C ({|1.1|}, floaty_of_json, floaty_to_json, 1.1);
2931
C ({|1.0|}, floaty_of_json, floaty_to_json, 1.0);
3032
C ({|42|}, floaty_of_json, floaty_to_json, 42.0);

ppx/test/ppx_deriving_json_js.e2e.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*** json deriver tests ***
3030
JSON DATA: 1
3131
JSON REPRINT: 1
32+
JSON DATA: "9223372036854775807"
33+
JSON REPRINT: "9223372036854775807"
3234
JSON DATA: 1.1
3335
JSON REPRINT: 1.1
3436
JSON DATA: 1.0

ppx/test/ppx_deriving_json_native.e2e.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
$ dune exec ./main.exe
2020
JSON DATA: 1
2121
JSON REPRINT: 1
22+
JSON DATA: "9223372036854775807"
23+
JSON REPRINT: "9223372036854775807"
2224
JSON DATA: 1.1
2325
JSON REPRINT: 1.1
2426
JSON DATA: 1.0

0 commit comments

Comments
 (0)