如果我有某个标记的联合类型,例如 Shape
在这里,我将如何在 Elm 中为它构建一个 JSON 解码器?
type alias Rectangle = { width : Int, height : Int }
type alias Circle = { radius: Int }
type Shape
= ShapeRectangle Rectangle
| ShapeCircle Circle
最佳答案
Michel Thoma 的回答在这里大放异彩。
您可以使用 Json.Decode.map
标记解码的值或 andThen
像这样:
`andThen` \x -> decode (MyTag x)
使用这里是使用
andThen
的解决方案和 Json.Decode.Pipelineimport 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
关于json - 如何在 Elm 中解码标记的联合类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193642/