【问题标题】:How to Create ToJSON / FromJSON Instances for [Text]如何为 [Text] 创建 ToJSON / FromJSON 实例
【发布时间】:2014-10-21 14:04:44
【问题描述】:

我有一个文本列表:

foo :: [Text]
foo = ["Foo", "Bar"]

我使用 scotty 的 json 函数来发送响应。

post "/generate" $ do
     json foo

我收到无实例错误:

 No instance for (ToJSON Text) arising from a use of ‘json’

当我添加时:

instance ToJSON Text  where
        toJSON = String
instance FromJSON Text where
        parseJSON = withText "Text" pure

我明白了:

Couldn't match type ‘text-1.1.1.3:Data.Text.Internal.Text’
              with ‘Text’
NB: ‘text-1.1.1.3:Data.Text.Internal.Text’
      is defined in ‘Data.Text.Internal’ in package ‘text-1.1.1.3’
    ‘Text’
      is defined in ‘Data.Text.Internal.Lazy’ in package ‘text-1.2.0.0’
Expected type: Text -> Value
  Actual type: text-1.1.1.3:Data.Text.Internal.Text -> Value
In the expression: String
In an equation for ‘toJSON’: toJSON = String

好像我安装了多个版本的 text-*,但我被卡住了。

【问题讨论】:

  • 你在使用阴谋集团沙箱吗?这看起来像基本问题是阴谋集团的地狱。
  • 您必须为Text 定义实例也很奇怪,因为这些实例肯定已经存在于Data.Aeson 中。
  • @bheklilr :我没有使用沙箱——我会尝试看看是否能解决这个问题。另外,我正在定义我的实例,因为它给了我No Instance Error,不知道为什么会这样。

标签: haskell


【解决方案1】:

您正在尝试在 text-1.2.0.0 的值(很可能是您上次安装的文本版本)上使用与 text-1.1.1.3 链接的 aeson。

您可能想要 cabal 重新安装 aeson,以及任何损坏的软件包。

另一种方法是使用 cabal 构建项目并使用 .cabal 文件中绑定的版本来强制使用 text-1.1.1.3,但这几乎可以肯定 不是你想要做的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多