简体   繁体   English

在ELM 0.19解码中超过8个地图

[英]More than 8 map in ELM 0.19 decode

I have flags decoder with default values. 我有标志解码器默认值。

flagsDecoder : Decode.Decoder Params
flagsDecoder =
    Decode.map8 Params
        (Decode.field "field1" (Decode.string) |> (Decode.withDefault) "1")
        (Decode.field "field2" (Decode.string)   |> (Decode.withDefault) "2")
        (Decode.field "field3" (Decode.string)   |> (Decode.withDefault) "3")
        (Decode.field "field4" (Decode.string) |> (Decode.withDefault) "4)
        (Decode.field "field5" (Decode.string)  |> (Decode.withDefault) "5")
        (Decode.field "field6" (Decode.int) |> (Decode.withDefault) 6)
        (Decode.field "field7" (Decode.string)    |> (Decode.withDefault) "7")
        (Decode.field "field8" (Decode.string)   |> (Decode.withDefault) "8")

How can i add more fields? 我该如何添加更多字段? I have a JSON object with 10 fields. 我有一个包含10个字段的JSON对象。

I would recommend using andMap from elm-community/json-extra : 我建议使用elm-community/json-extra andMap

flagsDecoder : Decode.Decoder Params
flagsDecoder =
    Decode.succeed Params
        |> Decode.andMap (Decode.field "field1" (Decode.string) |> (Decode.withDefault) "1")
        |> Decode.andMap (Decode.field "field2" (Decode.string)   |> (Decode.withDefault) "2")
        |> Decode.andMap (Decode.field "field3" (Decode.string)   |> (Decode.withDefault) "3")
        |> Decode.andMap (Decode.field "field4" (Decode.string) |> (Decode.withDefault) "4")
        |> Decode.andMap (Decode.field "field5" (Decode.string)  |> (Decode.withDefault) "5")
        |> Decode.andMap (Decode.field "field6" (Decode.int) |> (Decode.withDefault) "6")
        |> Decode.andMap (Decode.field "field7" (Decode.string)    |> (Decode.withDefault) "7")
        |> Decode.andMap (Decode.field "field8" (Decode.string)   |> (Decode.withDefault) "8")

But there are also other options like elm-json-decode-pipeline and a new experimental API . 但也有其他选项,如elm-json-decode-pipeline一个新的实验API andMap is really simple and straight-forward though, to use along with and to switch to from mapN if needed. andMap非常简单直接,如果需要的话可以和mapN一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM