How to decode JSON into a custom type (union type/adt)?

This package has some examples in the readme on how to write decoders for custom types using a do-notation style.

https://package.elm-lang.org/packages/webbhuset/elm-json-decode/latest/

Something like this should probably work in your case:

statusDecoder : Decoder Status
statusDecoder =
    oneOf
        [ dataDecoder
            |> map Authenticated
        , maybe errorDecoder
            |> map Unauthenticated
        ]
1 Like