Therefore, if you are looking for a solution with zero dependency that does not require Json.Decode.Pipeline .
import Json.Decode as Decode exposing (Decoder) modelDecoder : Decoder Model modelDecoder = Decode.map3 Model (Decode.field "id" Decode.int) (Decode.field "name" Decode.string) (Decode.maybe (Decode.field "desc" Decode.string))
If you want to do this, use the Model constructor as an applicative functor (because you need more than 8 elements).
import Json.Decode as Decode exposing (Decoder) import Json.Decode.Extra as Decode modelDecoder : Decoder Model modelDecoder = Decode.succeed Model |> Decode.andMap (Decode.field "id" Decode.int) |> Decode.andMap (Decode.field "name" Decode.string) |> Decode.andMap (Decode.maybe (Decode.field "desc" Decode.string))
Both can be used with List with Decode.list modelDecoder . I would like the applicative functions to be in the standard library, but you will need to go to all * -extra libraries to get these functions. Knowing how applicative functors work will help you better understand this, so I would suggest reading about them. Decode Pipeline's solution abstracts this simple concept, but when you Result.andMap with the need for Result.andMap or any other of andMap because there is no mapN for your module or DSL, you will know how to get to your solution.
Due to the applicability of the decoders, all fields must be processed asynchronously and in parallel with a small performance gain, and not synchronously, like andThen .
Under the hood, JSON.Decode.Pipeline uses Json.Decode.map2 (that is, andMap ), so there is no performance difference, but it uses DSL, which is slightly more "friendly".
source share