1
1
@new external _unsafeCreateUninitializedArray : int => array <'a > = "Array"
2
2
3
- @ val external _stringify : Js . Json . t => string = "JSON.stringify"
3
+ let _isInteger = value => Float . isFinite ( value ) && Math . floor ( value ) === value
4
4
5
- let _isInteger = value => Js .Float .isFinite (value ) && Js .Math .floor_float (value ) === value
6
-
7
- type decoder <'a > = Js .Json .t => 'a
5
+ type decoder <'a > = JSON .t => 'a
8
6
9
7
exception DecodeError (string )
10
8
11
9
let id = json => json
12
10
13
11
let bool = json =>
14
- if Js . typeof (json ) == " boolean" {
15
- (Obj .magic ((json : Js . Json .t )): bool )
12
+ if typeof (json ) == # boolean {
13
+ (Obj .magic ((json : JSON .t )): bool )
16
14
} else {
17
- \"@@" (raise , DecodeError ("Expected boolean, got " ++ _stringify (json )))
15
+ \"@@" (raise , DecodeError ("Expected boolean, got " ++ JSON . stringify (json )))
18
16
}
19
17
20
18
let float = json =>
21
- if Js . typeof (json ) == " number" {
22
- (Obj .magic ((json : Js . Json .t )): float )
19
+ if typeof (json ) == # number {
20
+ (Obj .magic ((json : JSON .t )): float )
23
21
} else {
24
- \"@@" (raise , DecodeError ("Expected number, got " ++ _stringify (json )))
22
+ \"@@" (raise , DecodeError ("Expected number, got " ++ JSON . stringify (json )))
25
23
}
26
24
27
25
let int = json => {
28
26
let f = float (json )
29
27
if _isInteger (f ) {
30
28
(Obj .magic ((f : float )): int )
31
29
} else {
32
- \"@@" (raise , DecodeError ("Expected integer, got " ++ _stringify (json )))
30
+ \"@@" (raise , DecodeError ("Expected integer, got " ++ JSON . stringify (json )))
33
31
}
34
32
}
35
33
36
34
let string = json =>
37
- if Js . typeof (json ) == " string" {
38
- (Obj .magic ((json : Js . Json .t )): string )
35
+ if typeof (json ) == # string {
36
+ (Obj .magic ((json : JSON .t )): string )
39
37
} else {
40
- \"@@" (raise , DecodeError ("Expected string, got " ++ _stringify (json )))
38
+ \"@@" (raise , DecodeError ("Expected string, got " ++ JSON . stringify (json )))
41
39
}
42
40
43
41
let char = json => {
44
42
let s = string (json )
45
43
if String .length (s ) == 1 {
46
44
OCamlCompat .String .get (s , 0 )
47
45
} else {
48
- \"@@" (raise , DecodeError ("Expected single-character string, got " ++ _stringify (json )))
46
+ \"@@" (raise , DecodeError ("Expected single-character string, got " ++ JSON . stringify (json )))
49
47
}
50
48
}
51
49
52
- let date = json => Js . Date .fromString (string (json ))
50
+ let date = json => Date .fromString (string (json ))
53
51
54
52
let nullable = (decode , json ) =>
55
- if (Obj .magic (json ): Js . null <'a >) === Js .null {
56
- Js .null
53
+ if (Obj .magic (json ): Null . t <'a >) === Null .null {
54
+ Null .null
57
55
} else {
58
- Js . Null .return (decode (json ))
56
+ Null .make (decode (json ))
59
57
}
60
58
61
59
/* TODO: remove this? */
62
60
let nullAs = (value , json ) =>
63
- if (Obj .magic (json ): Js . null <'a >) === Js .null {
61
+ if (Obj .magic (json ): Null . t <'a >) === Null .null {
64
62
value
65
63
} else {
66
- \"@@" (raise , DecodeError ("Expected null, got " ++ _stringify (json )))
64
+ \"@@" (raise , DecodeError ("Expected null, got " ++ JSON . stringify (json )))
67
65
}
68
66
69
67
let array = (decode , json ) =>
70
- if Js . Array .isArray (json ) {
71
- let source : array <Js . Json . t > = Obj .magic ((json : Js . Json .t ))
72
- let length = Js . Array .length (source )
68
+ if Array .isArray (json ) {
69
+ let source : array <JSON . t > = Obj .magic ((json : JSON .t ))
70
+ let length = Array .length (source )
73
71
let target = _unsafeCreateUninitializedArray (length )
74
72
for i in 0 to length - 1 {
75
73
let value = try decode (Array .getUnsafe (source , i )) catch {
76
74
| DecodeError (msg ) =>
77
- \"@@" (raise , DecodeError (msg ++ ("\n\t in array at index " ++ string_of_int (i ))))
75
+ \"@@" (raise , DecodeError (msg ++ ("\n\t in array at index " ++ Int . toString (i ))))
78
76
}
79
77
80
78
Array .setUnsafe (target , i , value )
81
79
}
82
80
target
83
81
} else {
84
- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
82
+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
85
83
}
86
84
87
85
let list = (decode , json ) => array (decode , json )-> List .fromArray
88
86
89
87
let pair = (decodeA , decodeB , json ) =>
90
- if Js . Array .isArray (json ) {
91
- let source : array <Js . Json . t > = Obj .magic ((json : Js . Json .t ))
92
- let length = Js . Array .length (source )
88
+ if Array .isArray (json ) {
89
+ let source : array <JSON . t > = Obj .magic ((json : JSON .t ))
90
+ let length = Array .length (source )
93
91
if length == 2 {
94
92
try (decodeA (Array .getUnsafe (source , 0 )), decodeB (Array .getUnsafe (source , 1 ))) catch {
95
93
| DecodeError (msg ) => \"@@" (raise , DecodeError (msg ++ "\n\t in pair/tuple2" ))
96
94
}
97
95
} else {
98
96
\"@@" (
99
97
raise ,
100
- DecodeError (` Expected array of length 2, got array of length ${length-> string_of_int }` ),
98
+ DecodeError (` Expected array of length 2, got array of length ${length-> Int.toString }` ),
101
99
)
102
100
}
103
101
} else {
104
- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
102
+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
105
103
}
106
104
107
105
let tuple2 = pair
108
106
109
107
let tuple3 = (decodeA , decodeB , decodeC , json ) =>
110
- if Js . Array .isArray (json ) {
111
- let source : array <Js . Json . t > = Obj .magic ((json : Js . Json .t ))
112
- let length = Js . Array .length (source )
108
+ if Array .isArray (json ) {
109
+ let source : array <JSON . t > = Obj .magic ((json : JSON .t ))
110
+ let length = Array .length (source )
113
111
if length == 3 {
114
112
try (
115
113
decodeA (Array .getUnsafe (source , 0 )),
@@ -121,17 +119,17 @@ let tuple3 = (decodeA, decodeB, decodeC, json) =>
121
119
} else {
122
120
\"@@" (
123
121
raise ,
124
- DecodeError (` Expected array of length 3, got array of length ${length-> string_of_int }` ),
122
+ DecodeError (` Expected array of length 3, got array of length ${length-> Int.toString }` ),
125
123
)
126
124
}
127
125
} else {
128
- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
126
+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
129
127
}
130
128
131
129
let tuple4 = (decodeA , decodeB , decodeC , decodeD , json ) =>
132
- if Js . Array .isArray (json ) {
133
- let source : array <Js . Json . t > = Obj .magic ((json : Js . Json .t ))
134
- let length = Js . Array .length (source )
130
+ if Array .isArray (json ) {
131
+ let source : array <JSON . t > = Obj .magic ((json : JSON .t ))
132
+ let length = Array .length (source )
135
133
if length == 4 {
136
134
try (
137
135
decodeA (Array .getUnsafe (source , 0 )),
@@ -144,52 +142,52 @@ let tuple4 = (decodeA, decodeB, decodeC, decodeD, json) =>
144
142
} else {
145
143
\"@@" (
146
144
raise ,
147
- DecodeError (` Expected array of length 4, got array of length ${length-> string_of_int }` ),
145
+ DecodeError (` Expected array of length 4, got array of length ${length-> Int.toString }` ),
148
146
)
149
147
}
150
148
} else {
151
- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
149
+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
152
150
}
153
151
154
152
let dict = (decode , json ) =>
155
153
if (
156
- Js . typeof (json ) == " object" &&
157
- (! Js . Array .isArray (json ) &&
158
- ! ((Obj .magic (json ): Js . null <'a >) === Js .null ))
154
+ typeof (json ) == # object &&
155
+ (! Array .isArray (json ) &&
156
+ ! ((Obj .magic (json ): Null . t <'a >) === Null .null ))
159
157
) {
160
- let source : Js . Dict .t <Js . Json . t > = Obj .magic ((json : Js . Json .t ))
161
- let keys = Js . Dict .keys (source )
162
- let l = Js . Array .length (keys )
163
- let target = Js . Dict .empty ()
158
+ let source : Dict .t <JSON . t > = Obj .magic ((json : JSON .t ))
159
+ let keys = Dict .keysToArray (source )
160
+ let l = Array .length (keys )
161
+ let target = Dict .make ()
164
162
for i in 0 to l - 1 {
165
163
let key = Array .getUnsafe (keys , i )
166
- let value = try decode (Js . Dict .unsafeGet (source , key )) catch {
164
+ let value = try decode (Dict .getUnsafe (source , key )) catch {
167
165
| DecodeError (msg ) => \"@@" (raise , DecodeError (msg ++ "\n\t in dict" ))
168
166
}
169
167
170
- Js . Dict .set (target , key , value )
168
+ Dict .set (target , key , value )
171
169
}
172
170
target
173
171
} else {
174
- \"@@" (raise , DecodeError ("Expected object, got " ++ _stringify (json )))
172
+ \"@@" (raise , DecodeError ("Expected object, got " ++ JSON . stringify (json )))
175
173
}
176
174
177
175
let field = (key , decode , json ) =>
178
176
if (
179
- Js . typeof (json ) == " object" &&
180
- (! Js . Array .isArray (json ) &&
181
- ! ((Obj .magic (json ): Js . null <'a >) === Js .null ))
177
+ typeof (json ) == # object &&
178
+ (! Array .isArray (json ) &&
179
+ ! ((Obj .magic (json ): Null . t <'a >) === Null .null ))
182
180
) {
183
- let dict : Js . Dict .t <Js . Json . t > = Obj .magic ((json : Js . Json .t ))
184
- switch Js . Dict .get (dict , key ) {
181
+ let dict : Dict .t <JSON . t > = Obj .magic ((json : JSON .t ))
182
+ switch Dict .get (dict , key ) {
185
183
| Some (value ) =>
186
184
try decode (value ) catch {
187
185
| DecodeError (msg ) => \"@@" (raise , DecodeError (msg ++ ("\n\t at field '" ++ (key ++ "'" ))))
188
186
}
189
187
| None => \"@@" (raise , DecodeError (` Expected field '${key}'` ))
190
188
}
191
189
} else {
192
- \"@@" (raise , DecodeError ("Expected object, got " ++ _stringify (json )))
190
+ \"@@" (raise , DecodeError ("Expected object, got " ++ JSON . stringify (json )))
193
191
}
194
192
195
193
let rec at = (key_path , decoder , json ) =>
@@ -208,12 +206,12 @@ let oneOf = (decoders, json) => {
208
206
let rec inner = (decoders , errors ) =>
209
207
switch decoders {
210
208
| list {} =>
211
- let formattedErrors = "\n - " ++ Js . Array .joinWith ( " \n - " , List .toArray (List .reverse (errors )))
209
+ let formattedErrors = "\n - " ++ Array .join ( List .toArray (List .reverse (errors )), " \n - " )
212
210
\"@@" (
213
211
raise ,
214
212
DecodeError (
215
213
` All decoders given to oneOf failed. Here are all the errors: ${formattedErrors}\\ nAnd the JSON being decoded: ` ++
216
- _stringify (json ),
214
+ JSON . stringify (json ),
217
215
),
218
216
)
219
217
| list {decode , ... rest } =>
0 commit comments