Skip to content

Commit f341eb4

Browse files
committed
feat: more decoders
1 parent 7eaf820 commit f341eb4

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

src/Json_Decode.res

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Error = {
1515

1616
let custom = f => f
1717

18+
let id = (. json) => json
19+
1820
let 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\tin 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\tin 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\tin 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\tin dict'`))
100165
}
101166
}
102167

src/Json_Decode.resi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@ module Error: {
1313

1414
let custom: ((. Js.Json.t) => 'a) => t<'a>
1515

16+
let id: t<Js.Json.t>
17+
1618
let float: t<float>
1719
let int: t<int>
1820
let bool: t<bool>
1921
let string: t<string>
22+
2023
let array: t<'a> => t<array<'a>>
21-
let pair: (t<'a>, t<'b>) => t<('a, 'b)>
24+
let list: t<'a> => t<list<'a>>
2225
let option: t<'a> => t<option<'a>>
26+
27+
let date: t<Js.Date.t>
28+
let pair: (t<'a>, t<'b>) => t<('a, 'b)>
29+
let tuple2: (t<'a>, t<'b>) => t<('a, 'b)>
30+
let tuple3: (t<'a>, t<'b>, t<'c>) => t<('a, 'b, 'c)>
31+
let tuple4: (t<'a>, t<'b>, t<'c>, t<'d>) => t<('a, 'b, 'c, 'd)>
32+
let dict: t<'a> => t<Js.Dict.t<'a>>
2333
let field: (string, t<'a>) => t<'a>
2434
let object: (fieldDecoders => 'a) => t<'a>
2535

0 commit comments

Comments
 (0)