1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
open Js
open! Import
class type json = object
method parse : 'a. js_string t -> 'a meth
method parse_ :
'a 'b 'c 'd. js_string t -> ('b t, js_string t -> 'c -> 'd) meth_callback -> 'a meth
method stringify : 'a. 'a -> js_string t meth
method stringify_ :
'a 'b 'c 'd. 'a -> ('b, js_string t -> 'c -> 'd) meth_callback -> js_string t meth
end
let json : json Js.t = Unsafe.global##._JSON
let input_reviver =
let reviver _this _key (value : Unsafe.any) : Obj.t =
if Js.equals (typeof value) (string "string")
then Obj.repr (to_bytestring (Unsafe.coerce value))
else if instanceof value Js.array_empty
&& (Unsafe.coerce value)##.length == 4
&& Unsafe.get value 0 == 255
then
Obj.repr
(Jsoo_runtime.Int64.create_int64_lo_mi_hi
(Unsafe.get value 1)
(Unsafe.get value 2)
(Unsafe.get value 3))
else Obj.repr value
in
wrap_meth_callback reviver
let unsafe_input s = json##parse_ s input_reviver
class type obj = object
method constructor : 'a. 'a constr Js.readonly_prop
end
let mlInt64_constr =
let dummy_int64 = 1L in
let dummy_obj : obj t = Obj.magic dummy_int64 in
dummy_obj##.constructor
let output_reviver _key (value : Unsafe.any) : Obj.t =
if Obj.tag (Obj.repr value) = Obj.string_tag
then Obj.repr (bytestring (Obj.magic value : string))
else if instanceof value mlInt64_constr
then
let value = Unsafe.coerce value in
Obj.repr (array [| 255; value##.lo; value##.mi; value##.hi |])
else Obj.repr value
let output obj = json##stringify_ obj (Js.wrap_callback output_reviver)