@@ -15,6 +15,8 @@ module Error = {
1515
1616let custom = f => f
1717
18+ let id = (. json ) => json
19+
1820let float = (. json ) => {
1921 if Js .typeof (json ) != "number" {
2022 Error .expected ("float" , json )
@@ -73,7 +75,19 @@ let array = (decode, . json) => {
7375 target
7476}
7577
76- let pair = (decodeA , decodeB , . json ) => {
78+ let list = (decode , . json ) => array (decode )(. json )-> Array .to_list
79+
80+ let option = (decode , . json ) => {
81+ if Obj .magic (json ) == Js .null {
82+ None
83+ } else {
84+ Some (decode (. json ))
85+ }
86+ }
87+
88+ let date = (. json ) => string (. json )-> Js .Date .fromString
89+
90+ let tuple2 = (decodeA , decodeB , . json ) => {
7791 if ! Js .Array .isArray (json ) {
7892 Error .expected ("array" , json )
7993 }
@@ -91,12 +105,63 @@ let pair = (decodeA, decodeB, . json) => {
91105 | DecodeError (msg ) => raise (DecodeError (j ` ${msg}\n\t in pair` ))
92106 }
93107}
108+ let pair = tuple2
94109
95- let option = (decode , . json ) => {
96- if Obj .magic (json ) == Js .null {
97- None
98- } else {
99- Some (decode (. json ))
110+ let tuple3 = (decodeA , decodeB , decodeC , . json ) => {
111+ if ! Js .Array .isArray (json ) {
112+ Error .expected ("array" , json )
113+ }
114+
115+ let arr : array <Js .Json .t > = Obj .magic (json )
116+ if Array .length (arr ) != 3 {
117+ raise (
118+ DecodeError (
119+ ` Expected array of length 3, got array of length ${Array.length(arr)-> string_of_int}` ,
120+ ),
121+ )
122+ }
123+
124+ try (
125+ decodeA (. arr -> Array .unsafe_get (0 )),
126+ decodeB (. arr -> Array .unsafe_get (1 )),
127+ decodeC (. arr -> Array .unsafe_get (2 )),
128+ ) catch {
129+ | DecodeError (msg ) => raise (DecodeError (j ` ${msg}\n\t in pair` ))
130+ }
131+ }
132+
133+ let tuple4 = (decodeA , decodeB , decodeC , decodeD , . json ) => {
134+ if ! Js .Array .isArray (json ) {
135+ Error .expected ("array" , json )
136+ }
137+
138+ let arr : array <Js .Json .t > = Obj .magic (json )
139+ if Array .length (arr ) != 4 {
140+ raise (
141+ DecodeError (
142+ ` Expected array of length 4, got array of length ${Array.length(arr)-> string_of_int}` ,
143+ ),
144+ )
145+ }
146+
147+ try (
148+ decodeA (. arr -> Array .unsafe_get (0 )),
149+ decodeB (. arr -> Array .unsafe_get (1 )),
150+ decodeC (. arr -> Array .unsafe_get (2 )),
151+ decodeD (. arr -> Array .unsafe_get (3 )),
152+ ) catch {
153+ | DecodeError (msg ) => raise (DecodeError (j ` ${msg}\n\t in pair` ))
154+ }
155+ }
156+
157+ let dict = (decode , . json ) => {
158+ if Js .typeof (json ) != "object" || Js .Array .isArray (json ) || Obj .magic (json ) == Js .null {
159+ Error .expected ("object" , json )
160+ }
161+
162+ let source : Js .Dict .t <Js .Json .t > = Obj .magic (json )
163+ try Js .Dict .map (decode , source )-> Obj .magic catch {
164+ | DecodeError (msg ) => raise (DecodeError (` ${msg}\n\t in dict'` ))
100165 }
101166}
102167
0 commit comments