ミシェル・トーマの答えはここに偉大な光を照らしました。
あなたはこのようJson.Decode.map
またはandThen
を使用して復号化された値にタグを付けることができます:ここで使用
`andThen` \x -> decode (MyTag x)
はandThen
とJson.Decode.Pipeline
import Json.Decode exposing (Decoder, decodeString, int, andThen, oneOf)
import Json.Decode.Pipeline exposing (decode, required)
import Html
main =
let
decoded = decodeString decodeShape "{ \"radius\": 2 }"
in
case decoded of
Ok shape ->
Html.text <| toString shape
Err error ->
Html.text error
type alias Rectangle = { width : Int, height : Int }
type alias Circle = { radius: Int }
type Shape
= ShapeRectangle Rectangle
| ShapeCircle Circle
decodeShape : Decoder Shape
decodeShape =
oneOf
[ decodeRectangle `andThen` \x -> decode (ShapeRectangle x)
, decodeCircle `andThen` \x -> decode (ShapeCircle x)
]
decodeRectangle : Decoder Rectangle
decodeRectangle =
decode Rectangle
|> required "width" int
|> required "height" int
decodeCircle : Decoder Circle
decodeCircle =
decode Circle
|> required "radius" int
を使用したソリューションであります