11@new external _unsafeCreateUninitializedArray : int => array <'a > = "Array"
22
3- @ val external _stringify : Js . Json . t => string = "JSON.stringify"
3+ let _isInteger = value => Float . isFinite ( value ) && Math . floor ( value ) === value
44
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
86
97exception DecodeError (string )
108
119let id = json => json
1210
1311let 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 )
1614 } else {
17- \"@@" (raise , DecodeError ("Expected boolean, got " ++ _stringify (json )))
15+ \"@@" (raise , DecodeError ("Expected boolean, got " ++ JSON . stringify (json )))
1816 }
1917
2018let 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 )
2321 } else {
24- \"@@" (raise , DecodeError ("Expected number, got " ++ _stringify (json )))
22+ \"@@" (raise , DecodeError ("Expected number, got " ++ JSON . stringify (json )))
2523 }
2624
2725let int = json => {
2826 let f = float (json )
2927 if _isInteger (f ) {
3028 (Obj .magic ((f : float )): int )
3129 } else {
32- \"@@" (raise , DecodeError ("Expected integer, got " ++ _stringify (json )))
30+ \"@@" (raise , DecodeError ("Expected integer, got " ++ JSON . stringify (json )))
3331 }
3432}
3533
3634let 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 )
3937 } else {
40- \"@@" (raise , DecodeError ("Expected string, got " ++ _stringify (json )))
38+ \"@@" (raise , DecodeError ("Expected string, got " ++ JSON . stringify (json )))
4139 }
4240
4341let char = json => {
4442 let s = string (json )
4543 if String .length (s ) == 1 {
4644 OCamlCompat .String .get (s , 0 )
4745 } else {
48- \"@@" (raise , DecodeError ("Expected single-character string, got " ++ _stringify (json )))
46+ \"@@" (raise , DecodeError ("Expected single-character string, got " ++ JSON . stringify (json )))
4947 }
5048}
5149
52- let date = json => Js . Date .fromString (string (json ))
50+ let date = json => Date .fromString (string (json ))
5351
5452let 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
5755 } else {
58- Js . Null .return (decode (json ))
56+ Null .make (decode (json ))
5957 }
6058
6159/* TODO: remove this? */
6260let nullAs = (value , json ) =>
63- if (Obj .magic (json ): Js . null <'a >) === Js .null {
61+ if (Obj .magic (json ): Null . t <'a >) === Null .null {
6462 value
6563 } else {
66- \"@@" (raise , DecodeError ("Expected null, got " ++ _stringify (json )))
64+ \"@@" (raise , DecodeError ("Expected null, got " ++ JSON . stringify (json )))
6765 }
6866
6967let 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 )
7371 let target = _unsafeCreateUninitializedArray (length )
7472 for i in 0 to length - 1 {
7573 let value = try decode (Array .getUnsafe (source , i )) catch {
7674 | 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 ))))
7876 }
7977
8078 Array .setUnsafe (target , i , value )
8179 }
8280 target
8381 } else {
84- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
82+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
8583 }
8684
8785let list = (decode , json ) => array (decode , json )-> List .fromArray
8886
8987let 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 )
9391 if length == 2 {
9492 try (decodeA (Array .getUnsafe (source , 0 )), decodeB (Array .getUnsafe (source , 1 ))) catch {
9593 | DecodeError (msg ) => \"@@" (raise , DecodeError (msg ++ "\n\t in pair/tuple2" ))
9694 }
9795 } else {
9896 \"@@" (
9997 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 }` ),
10199 )
102100 }
103101 } else {
104- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
102+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
105103 }
106104
107105let tuple2 = pair
108106
109107let 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 )
113111 if length == 3 {
114112 try (
115113 decodeA (Array .getUnsafe (source , 0 )),
@@ -121,17 +119,17 @@ let tuple3 = (decodeA, decodeB, decodeC, json) =>
121119 } else {
122120 \"@@" (
123121 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 }` ),
125123 )
126124 }
127125 } else {
128- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
126+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
129127 }
130128
131129let 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 )
135133 if length == 4 {
136134 try (
137135 decodeA (Array .getUnsafe (source , 0 )),
@@ -144,52 +142,52 @@ let tuple4 = (decodeA, decodeB, decodeC, decodeD, json) =>
144142 } else {
145143 \"@@" (
146144 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 }` ),
148146 )
149147 }
150148 } else {
151- \"@@" (raise , DecodeError ("Expected array, got " ++ _stringify (json )))
149+ \"@@" (raise , DecodeError ("Expected array, got " ++ JSON . stringify (json )))
152150 }
153151
154152let dict = (decode , json ) =>
155153 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 ))
159157 ) {
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 ()
164162 for i in 0 to l - 1 {
165163 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 {
167165 | DecodeError (msg ) => \"@@" (raise , DecodeError (msg ++ "\n\t in dict" ))
168166 }
169167
170- Js . Dict .set (target , key , value )
168+ Dict .set (target , key , value )
171169 }
172170 target
173171 } else {
174- \"@@" (raise , DecodeError ("Expected object, got " ++ _stringify (json )))
172+ \"@@" (raise , DecodeError ("Expected object, got " ++ JSON . stringify (json )))
175173 }
176174
177175let field = (key , decode , json ) =>
178176 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 ))
182180 ) {
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 ) {
185183 | Some (value ) =>
186184 try decode (value ) catch {
187185 | DecodeError (msg ) => \"@@" (raise , DecodeError (msg ++ ("\n\t at field '" ++ (key ++ "'" ))))
188186 }
189187 | None => \"@@" (raise , DecodeError (` Expected field '${key}'` ))
190188 }
191189 } else {
192- \"@@" (raise , DecodeError ("Expected object, got " ++ _stringify (json )))
190+ \"@@" (raise , DecodeError ("Expected object, got " ++ JSON . stringify (json )))
193191 }
194192
195193let rec at = (key_path , decoder , json ) =>
@@ -208,12 +206,12 @@ let oneOf = (decoders, json) => {
208206 let rec inner = (decoders , errors ) =>
209207 switch decoders {
210208 | 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 - " )
212210 \"@@" (
213211 raise ,
214212 DecodeError (
215213 ` All decoders given to oneOf failed. Here are all the errors: ${formattedErrors}\\ nAnd the JSON being decoded: ` ++
216- _stringify (json ),
214+ JSON . stringify (json ),
217215 ),
218216 )
219217 | list {decode , ... rest } =>
0 commit comments