从不带模式匹配的JSON值字符串文本中提取文本 [英] Extract the Text from a JSON value String Text without pattern matching

查看:99
本文介绍了从不带模式匹配的JSON值字符串文本中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Json值的定义:

Here is the definition for a Json Value :

-- | A JSON value represented as a Haskell value.
data Value = Object !Object
           | Array !Array
           | String !Text
           | Number !Scientific
           | Bool !Bool
           | Null
             deriving (Eq, Show)

let value = String "myValue"
looking for => fromString value == "myValue" ??
fromString :: Value -> Text

我正在寻找一个像我可以从字符串中获取文本而无需进行某些模式匹配的函数,显然,该函数将是不安全的... fromString,例如Data中的fromJust..Lens.Aeson吗?

I'm looking a function like where I could get the Text from String without to do some pattern matching, obviously this function will be unsafe... a fromString like fromJust in Data.Maybe for example... Something in Data.Lens.Aeson ?

推荐答案

正如Thomas M. DuBuisson在上述评论中所暗示的那样,这听起来像是

As Thomas M. DuBuisson implies in the above comment, this sounds like an XY Problem. Still, I'll do my best to nonetheless address the specifics.

技术上,您可以用 Value->类型简单地编写一个函数.文本,尽管它需要模式匹配.我意识到OP请求的功能没有模式匹配,但请继续阅读:

Technically, you can trivially write a function with the type Value -> Text, although it requires pattern matching. I realise that the OP requests a function without pattern matching, but please read on:

-- Warning: UNSAFE!
fromString :: Value -> Text
fromString (String s) = s

这样的函数可以编译,但是不安全!

Such a function compiles, but is unsafe!

*Q53961314 Q53961314> fromString $ String "myValue"
"myValue"
*Q53961314 Q53961314> fromString $ Number 42
"*** Exception: Non-exhaustive patterns in function fromString

虽然它适用于 String 值,但对于任何其他类型的值都会崩溃.尽管在技术上可以像上面那样编写和编译不安全的函数,但AFAICT并不认为它是惯用的Haskell.

While it works for String values, it crashes for any other type of value. While it's technically possible to write and compile unsafe functions like the above, AFAICT it's not considered idiomatic Haskell.

更好的选择是返回 Maybe Text 的安全函数.使用模式匹配,这仍然很容易编写:

A better alternative is a safe function that returns Maybe Text. This is still easy to write using pattern matching:

fromStringSafe :: Value -> Maybe Text
fromStringSafe (String s) = Just s
fromStringSafe _ = Nothing

此功能总计:

*Q53961314 Q53961314> fromStringSafe $ String "myValue"
Just "myValue"
*Q53961314 Q53961314> fromStringSafe $ Number 42
Nothing

如果您不想自己编写此类功能,而是希望使用 lens-aeson ,您可以使用 _String 棱镜:

If you don't wish to write such a function yourself, but prefer to use lens-aeson, you can use the _String prism:

Prelude Data.Aeson Data.Aeson.Lens Control.Lens> String "myValue" ^? _String
Just "myValue"

如您所知,这也是安全的,因为 ^?_String 返回也许是文本:

As you can tell, this is also safe, in that ^? _String returns Maybe Text:

Prelude Data.Aeson Data.Aeson.Lens Control.Lens> Bool True ^? _String
Nothing
Prelude Data.Aeson Data.Aeson.Lens Control.Lens> Number 42 ^? _String
Nothing

如果您真的想要一个不安全的功能,可以使用 ^ ?!_String :

If you really, really want an unsafe function, you can use ^?! _String:

Prelude Data.Aeson Data.Aeson.Lens Control.Lens> String "myValue" ^?! _String
"myValue"

这并不奇怪,不安全:

Prelude Data.Aeson Data.Aeson.Lens Control.Lens> Number 42 ^?! _String
"*** Exception: (^?!): empty Fold
CallStack (from HasCallStack):
  error, called at src\\Control\\Lens\\Fold.hs:1285:28 in lens-4.17-7m3etWEBj0P3172qv7LEG9:Control.Lens.Fold
  ^?!, called at <interactive>:9:1 in interactive:Ghci3

我想知道为什么您要问这种功能.您要使用Aeson解决的特定问题是否可以解决?

I wonder why you're asking about this sort of functionality, though. Is there a specific problem you're trying to solve with Aeson that we can help with?

这篇关于从不带模式匹配的JSON值字符串文本中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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