Elm:JSON解码器,将String转换为Bool [英] Elm: JSON decoder, convert String to Bool

查看:110
本文介绍了Elm:JSON解码器,将String转换为Bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的JSON看起来像这样:

I'm receiving a JSON that looks like this:

{ name: "NAME1", value: "true" }

我想创建一个json解码器,该解码器将创建如下记录:

I would like to create a json decoder that would create a record like this:

{ name: "NAME1", value: True }

我正在尝试制作一个将 true转换为True的解码器。到目前为止,我做到了:

I'm trying to make a decoder that transforms "true" into True. I did this so far:

userProperties : Json.Decode.Decoder Props
userProperties =
  Json.Decode.object2 (,)
    ("name" := Json.Decode.string)
    ("value" := Json.Decode.string)
      `andThen` \val ->
        let
          newVal = -- Do something here?
        in
          Json.Decode.succeed <| newVal


推荐答案

您的示例中有一些问题,所以让我们

There are a few problems in your example so let's step through each of them.

您尚未显示道具的定义,所以我假设在您的示例中,就是这样的:

You haven't shown the definition of Props so I'm assuming, based on your example, that it is this:

type alias Props = { name : String, value : Bool }

您正在传递( ,)作为 object2 的第一个参数,它指示您将返回一个元组类型的Decoder。可能应该是:

You are passing (,) as the first argument to object2 which indicates that you will be returning a Decoder of type tuple. That should probably be:

Json.Decode.object2 Props

现在,您使用 andthen 的方式由于其顺序而无法编译优先。如果您要用圆括号括起来,它看起来像这样:

Now, the way you are using andThen won't compile because of its order of precedence. If you were to parenthesize the whole thing, it would look like this:

userProperties =
  (Json.Decode.object2 Props
    ("name" := Json.Decode.string)
    ("value" := Json.Decode.string))
      `andThen` \val ->
        let
          newVal = -- Do something here?
        in
          Json.Decode.succeed <| newVal

由于您要的事情,这将是不正确的然后字段中的字符串 true 。为此,我建议创建一个提供该布尔解码器的解码器:

That isn't going to be correct since the thing you want to andThen is the string "true" in the "value" field. To do that, I would recommend creating a decoder which provides that boolean decoder:

stringBoolDecoder : Json.Decode.Decoder Bool
stringBoolDecoder =
  string `andThen` \val ->
    case val of
      "true" -> succeed True
      "false" -> succeed False
      _ -> fail <| "Expecting \"true\" or \"false\" but found " ++ val

我只是在猜测的处理和所有下划线。

I'm only guessing at the handling of "false" and the catch-all underscore. Change their implementation as suits your business case.

在构建复杂的解码器时,通常最好将解码器定义分解为尽可能小的块,如上所示。

When building complex decoders, it is often best to break up decoder definitions into the smallest chunk possible like in the above.

最后,我们现在可以重新定义您的 userProperties 解码器,以使用 stringBoolDecoder 在适当的位置:

Lastly, we can now redefine your userProperties decoder to use the stringBoolDecoder in the appropriate place:

userProperties : Json.Decode.Decoder Props
userProperties =
  Json.Decode.object2 Props
    ("name" := Json.Decode.string)
    ("value" := stringBoolDecoder)

这篇关于Elm:JSON解码器,将String转换为Bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆