为什么 Aeson 不是模式匹配文本?
Why is Aeson not pattern-matching Text?
我正在尝试使用字符串构造函数将文本与 aeson 值进行模式匹配,但 运行 出现编译错误。以下示例程序说明了我 运行 遇到的问题。
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text
import qualified Data.Aeson
main = print $
case ("here" :: Data.Text.Text) of
(Data.Aeson.String x) -> "match"
_ -> "no match"
如图所示,我正在尝试让文本 "here" 与 Aeson 的 'String' 值构造函数进行模式匹配。我认为这个程序应该将文本与 Aeson 字符串进行模式匹配并打印 "match"。但是程序不编译。相反,我收到以下错误:
$ ghc scratch.hs
[1 of 1] Compiling Main ( scratch.hs, scratch.o )
scratch.hs:9:4: error:
• Couldn't match expected type ‘Data.Text.Text’
with actual type ‘Data.Aeson.Value’
• In the pattern: Data.Aeson.String x
In a case alternative: (Data.Aeson.String x) -> "match"
In the second argument of ‘($)’, namely
‘case ("here" :: Data.Text.Text) of {
(Data.Aeson.String x) -> "match"
_ -> "no match" }’
它说模式需要文本,而实际类型是 aeson 值。但它显然是类型注释标记的文本 ("here" :: Data.Text.Text)。我不明白为什么会收到此错误。通过字符串构造函数将文本与 aeson 值进行模式匹配似乎已在 this aeson tutorial 中完成,我不明白为什么它在我的示例中也不起作用。
It says the pattern expected Text whereas the actual type was aeson Value. But it is clearly Text as marked by the type annotation
匹配的值确实是Text
类型。但是您要匹配的模式不是 Text
类型的模式;相反,它是 Value
类型的模式。出于显而易见的原因,您不能将一种类型的值与另一种类型的模式相匹配。
有许多可能的修复方法,但很难选择一个作为建议,因为您对您正在尝试做的事情说得很少。 可能 您打算将 Text
解析为 Value
并检查它是哪种类型的值:
case decode (fromStrict (encodeUtf8 "\"match\"")) of
Just (String x) -> "match"
Just _ -> "no match"
Nothing -> "invalid JSON"
但是,我怀疑您实际上只是误解了如何使用 aeson,应该编写一个 FromJSON
实例(或使用现有实例),而不是试图弄乱 Text
值直接。例如,要将 JSON 值解析为 Text
,您可以重用 Text
的现有 FromJSON
实例来编写:
case decode "\"match\"" of
Just v -> Data.Text.putStrLn v
Nothing -> putStrLn "invalid JSON"
同样,这两个建议都是纯粹的猜测,因为这个问题在更大的目标上有多轻。
我正在尝试使用字符串构造函数将文本与 aeson 值进行模式匹配,但 运行 出现编译错误。以下示例程序说明了我 运行 遇到的问题。
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text
import qualified Data.Aeson
main = print $
case ("here" :: Data.Text.Text) of
(Data.Aeson.String x) -> "match"
_ -> "no match"
如图所示,我正在尝试让文本 "here" 与 Aeson 的 'String' 值构造函数进行模式匹配。我认为这个程序应该将文本与 Aeson 字符串进行模式匹配并打印 "match"。但是程序不编译。相反,我收到以下错误:
$ ghc scratch.hs
[1 of 1] Compiling Main ( scratch.hs, scratch.o )
scratch.hs:9:4: error:
• Couldn't match expected type ‘Data.Text.Text’
with actual type ‘Data.Aeson.Value’
• In the pattern: Data.Aeson.String x
In a case alternative: (Data.Aeson.String x) -> "match"
In the second argument of ‘($)’, namely
‘case ("here" :: Data.Text.Text) of {
(Data.Aeson.String x) -> "match"
_ -> "no match" }’
它说模式需要文本,而实际类型是 aeson 值。但它显然是类型注释标记的文本 ("here" :: Data.Text.Text)。我不明白为什么会收到此错误。通过字符串构造函数将文本与 aeson 值进行模式匹配似乎已在 this aeson tutorial 中完成,我不明白为什么它在我的示例中也不起作用。
It says the pattern expected Text whereas the actual type was aeson Value. But it is clearly Text as marked by the type annotation
匹配的值确实是Text
类型。但是您要匹配的模式不是 Text
类型的模式;相反,它是 Value
类型的模式。出于显而易见的原因,您不能将一种类型的值与另一种类型的模式相匹配。
有许多可能的修复方法,但很难选择一个作为建议,因为您对您正在尝试做的事情说得很少。 可能 您打算将 Text
解析为 Value
并检查它是哪种类型的值:
case decode (fromStrict (encodeUtf8 "\"match\"")) of
Just (String x) -> "match"
Just _ -> "no match"
Nothing -> "invalid JSON"
但是,我怀疑您实际上只是误解了如何使用 aeson,应该编写一个 FromJSON
实例(或使用现有实例),而不是试图弄乱 Text
值直接。例如,要将 JSON 值解析为 Text
,您可以重用 Text
的现有 FromJSON
实例来编写:
case decode "\"match\"" of
Just v -> Data.Text.putStrLn v
Nothing -> putStrLn "invalid JSON"
同样,这两个建议都是纯粹的猜测,因为这个问题在更大的目标上有多轻。