Elm JSON解码器,用于带有数据的联合类型

时间:2018-12-13 09:15:43

标签: json functional-programming decode elm

我的json看起来像这样:

{"name": "providerWithVal", "value": "example"}

或类似这样:

{"name": "provider2"}

{"name": "provider3"}

我的Elm联合类型定义如下:

type Provider
    = ProviderWithVal String
    | Provider2
    | Provider3

我可以为联合类型编写一个解码器,而无需附加数据。但是ProviderWithVal需要一个字符串,我不确定如何使它全部正常工作。

这是我到目前为止所拥有的:

import Json.Decode as D

providerDecoder : D.Decoder Provider
providerDecoder =
    D.field "name" D.string |> D.andThen providerNameDecoder

providerNameDecoder : String -> D.Decoder Provider
providerNameDecoder string =
    case string of
        "providerWithVal" -> D.succeed ProviderWithVal
        "provider2" -> D.succeed Provider2
        "provider3" -> D.succeed Provider3
        _ -> D.fail <| "Invalid provider: " ++ string

1 个答案:

答案 0 :(得分:4)

您的问题的快速解决方案是将D.succeed ProviderWithVal替换为D.map ProviderWithVal (D.field "value" Decode.string

但是我会创建一个帮助程序来匹配目标字符串,然后按以下方式使用它:

decoder =
    Decode.oneOf [ decodeWithVal, decodeP2, decodeP3 ]


decodeWithVal =
    exactMatch (Decode.field "name" Decode.string)
        "providerWithVal"
        (Decode.map ProviderWithVal <| Decode.field "value" Decode.string)


decodeP2 =
    exactMatch (Decode.field "name" Decode.string) "provider2" (Decode.succeed Provider2)


decodeP3 =
    exactMatch (Decode.field "name" Decode.string) "provider3" (Decode.succeed Provider3)


exactMatch : Decoder String -> String -> Decoder a -> Decoder a
exactMatch matchDecoder match dec =
    matchDecoder
        |> Decode.andThen
            (\str ->
                if str == match then
                    dec

                else
                    Decode.fail <| "[exactMatch] tgt: " ++ match ++ " /= " ++ str
            )