gpt4 book ai didi

json - 如何在 Elm 中解码标记的联合类型?

转载 作者:行者123 更新时间:2023-12-04 10:06:56 24 4
gpt4 key购买 nike

如果我有某个标记的联合类型,例如 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.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

关于json - 如何在 Elm 中解码标记的联合类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com