1module Json.Decode.Ext exposing (listIgnore, optionalField)
2
3import Json.Decode as Decode exposing (Decoder)
4import Maybe.Extra as Maybe
5
6
7
8-- 🔱
9
10
11{-| A list decoder that always succeeds, throwing away the failures.
12-}
13listIgnore : Decoder a -> Decoder (List a)
14listIgnore decoder =
15 decoder
16 |> Decode.maybe
17 |> Decode.list
18 |> Decode.map Maybe.values
19
20
21{-| Provide a default value for a field that might not be there.
22-}
23optionalField : String -> Decoder a -> a -> Decoder a
24optionalField field decoder defaultValue =
25 decoder
26 |> Decode.field field
27 |> Decode.maybe
28 |> Decode.map (Maybe.withDefault defaultValue)