Elm 应为自定义错误
我刚接触elm,正在尝试解码一些Json。但我一直在犯这样的错误,因为我的期望值是不可能实现的 下面是一个代码示例。我不知道它在期待什么Elm 应为自定义错误,elm,Elm,我刚接触elm,正在尝试解码一些Json。但我一直在犯这样的错误,因为我的期望值是不可能实现的 下面是一个代码示例。我不知道它在期待什么 module JsonTest exposing (..) import Json.Decode import Json.Decode.Pipeline --Model type alias Model = { msg : String , tlps : List Tlp } decodeTlps : Json.Decode.De
module JsonTest exposing (..)
import Json.Decode
import Json.Decode.Pipeline
--Model
type alias Model =
{ msg : String
, tlps : List Tlp
}
decodeTlps : Json.Decode.Decoder Model
decodeTlps =
Json.Decode.Pipeline.decode Model
|> Json.Decode.Pipeline.hardcoded "Got Data"
|> Json.Decode.Pipeline.required "tlps" (Json.Decode.list decodeTlp)
type alias Tlp =
{ title : String
, trendingTopic : Bool
, synonym : Bool
, summaryDate : String
, lastFeatureAdded : String
, modifiedDate : String
}
decodeTlp : Json.Decode.Decoder Tlp
decodeTlp =
Json.Decode.Pipeline.decode Tlp
|> Json.Decode.Pipeline.required "title" (Json.Decode.string)
|> Json.Decode.Pipeline.optional "trendingTopic" (Json.Decode.bool) False
|> Json.Decode.Pipeline.optional "synonym" (Json.Decode.bool) False
|> Json.Decode.Pipeline.optional "summaryDate" (Json.Decode.string) "1/1/2016"
|> Json.Decode.Pipeline.optional "lastFeatureAdded" (Json.Decode.string) "1/1/2016"
|> Json.Decode.Pipeline.optional "modifiedDate" (Json.Decode.string) "1/1/2016"
json =
"""
{
"tlps": [
{
"title": "Topic 1",
"trendingTopic": false,
"synonym": false,
"summaryDate": null,
"lastFeatureAdded": null,
"modifiedDate": null
},
{
"title": "Topic 2",
"trendingTopic": false,
"synonym": false,
"summaryDate": null,
"lastFeatureAdded": null,
"modifiedDate": null
},
{
"title": "Topic 3",
"trendingTopic": false,
"synonym": false,
"summaryDate": null,
"lastFeatureAdded": null,
"modifiedDate": null
},
{
"title": "Topic 4",
"trendingTopic": false,
"synonym": false,
"summaryDate": null,
"lastFeatureAdded": null,
"modifiedDate": null
}
]
}
"""
如果我在repl中运行以下命令
> import JsonTest
> import Json.Decode
> Json.Decode.decodeString JsonTest.decodeTlps JsonTest.json
Err "Expecting something custom at _.tlps[3] but instead got: {\"title\":\"Topic 4\",\"trendingTopic\":false,\"synonym\":false,\"summaryDate\":null,\"lastFeatureAdded\":null,\"modifiedDate\":null}"
: Result.Result String TLPDashboard.Model
>
更新 包括此问题的修复程序,因此
null
由optional
作为Nothing
处理
版本1.1.2的原始答案
的当前版本没有像您预期的那样处理显式空值
使用optional
时,如果json中没有字段,则解码器会成功,但如果该值指定为null(例如“summaryDate”:null
),则解码器会因您发布的消息而失败
有几种方法可以解决这个问题:
可能
(例如摘要日期:可能字符串
),然后相应地更改解码器,在此过程中,删除硬编码日期以支持无
:|>Json.Decode.Pipeline.optional“summaryDate”(Json.Decode.maybe Json.Decode.string)无
Json.Decode.Pipeline
代码,使用原生Elm功能定义解码器decodeTlp:Json.Decode.Decoder-Tlp
解码TLP=
让
可选解码器默认值=Json.Decode.oneOf[解码器,Json.Decode.succeed默认值]
在里面
Json.Decode.object6 Tlp
(“title”:=Json.Decode.string)
(可选(“trendingTopic”:=Json.Decode.bool)False)
(可选(“同义词”:=Json.Decode.bool)False)
(可选(“summaryDate”:=Json.Decode.string)“1/1/2016”)
(可选(“lastFeatureAdded”:=Json.Decode.string)“1/1/2016”)
(可选(“modifiedDate”:=Json.Decode.string)“1/1/2016”)
更新 包括此问题的修复程序,因此
null
由optional
作为Nothing
处理
版本1.1.2的原始答案
的当前版本没有像您预期的那样处理显式空值
使用optional
时,如果json中没有字段,则解码器会成功,但如果该值指定为null(例如“summaryDate”:null
),则解码器会因您发布的消息而失败
有几种方法可以解决这个问题:
可能
(例如摘要日期:可能字符串
),然后相应地更改解码器,在此过程中,删除硬编码日期以支持无
:|>Json.Decode.Pipeline.optional“summaryDate”(Json.Decode.maybe Json.Decode.string)无
Json.Decode.Pipeline
代码,使用原生Elm功能定义解码器decodeTlp:Json.Decode.Decoder-Tlp
解码TLP=
让
可选解码器默认值=Json.Decode.oneOf[解码器,Json.Decode.succeed默认值]
在里面
Json.Decode.object6 Tlp
(“title”:=Json.Decode.string)
(可选(“trendingTopic”:=Json.Decode.bool)False)
(可选(“同义词”:=Json.Decode.bool)False)
(可选(“summaryDate”:=Json.Decode.string)“1/1/2016”)
(可选(“lastFeatureAdded”:=Json.Decode.string)“1/1/2016”)
(可选(“modifiedDate”:=Json.Decode.string)“1/1/2016”)
非常感谢您的帮助。我看到您回答了很多Elm问题,感谢您帮助我们大家!我只使用了一个多星期,我仍然习惯于函数的组成。非常感谢您的帮助,效果非常好。我看到您回答了很多Elm问题,感谢您帮助我们大家!我只在唱了一个多星期,我仍然习惯于函数的组合。