xml - 如何使 HaXml 和 DrIFT 与 GHC 7.0.3 一起工作?

标签 xml serialization haskell persistence

我正在寻找一种在 Haskell 和 Java/Scala/C# 代码之间交换数据的解决方案。目前,我正在考虑使用 XML。理想情况下,我希望从我的 Haskell 数据类型生成 XML 模式。我的第一次尝试是 HaXml 1.22.2、DrIFT 2.2.2。全部在 GHC 7.0.3 上。有以下片段:

import Data.List (isPrefixOf)
import Text.XML.HaXml.XmlContent
import Text.XML.HaXml.Types
import Text.XML.HaXml.Pretty (document)

data MyType = A | B String deriving (Eq, Show)
      {-! derive : XmlContent !-}   -- this line is for DrIFT

从这个文件中,DrIFT 产生:

{- Generated by DrIFT (Automatic class derivations for Haskell) -}
{-# LINE 1 "ts.hs" #-}
import Data.List (isPrefixOf)
import Text.XML.HaXml.XmlContent
import Text.XML.HaXml.Types
import Text.XML.HaXml.Pretty (document)


data MyType = A | B String deriving (Eq, Show)
      {-! derive : XmlContent !-}   -- this line is for DrIFT
{-* Generated by DrIFT : Look, but Don't Touch. *-}
instance HTypeable MyType where
    toHType v =
    Defined "MyType" [] [Constr "A" [] [],Constr "B" [] [toHType aa]]
      where
    (B aa) = v
instance XmlContent MyType where
    parseContents = do
    { e@(Elem t _ _) <- elementWith (flip isPrefixOf) ["B","A"]
    ; case t of
      _ | "B" `isPrefixOf` t -> interior e $ fmap B parseContents
        | "A" `isPrefixOf` t -> interior e $ return A
    }
    toContents v@A =
    [mkElemC (showConstr 0 (toHType v)) []]
    toContents v@(B aa) =
    [mkElemC (showConstr 1 (toHType v)) (toContents aa)]

--  Imported from other files :-

使用 GHC 编译此代码会产生错误消息:

19:32:
    Couldn't match expected type `[Char]' with actual type `QName'
    In the second argument of `isPrefixOf', namely `t'
    In the expression: "B" `isPrefixOf` t
    In a stmt of a pattern guard for
                 a case alternative:
          "B" `isPrefixOf` t

是工具的问题还是我做错了什么?如何解决这个问题?

最佳答案

深入研究旧版本 HaXml 的 Hackage 文档表明,在 1.20.2 及更早版本中,Elem 数据构造函数 used to take a Name ,这只是 String 的类型同义词。然而,在那个版本和 1.22.3 之间的某个时候,它被更改为采用 QName,即 a custom data type .

因此,在元素名称上使用 isPrefixOf 对旧版本有效,但对新版本无效。

从这些版本的上传日期来看,这发生在去年的某个时候,而 DrIFT 自 2009 年以来似乎没有更新过。

您可能应该将此通知 DrIFT 维护者。同时,您可以使用旧版本的 HaXml 或自己编写实例来解决这个问题。您应该能够使用不正确生成的实例作为起点。

关于xml - 如何使 HaXml 和 DrIFT 与 GHC 7.0.3 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501037/

相关文章:

objective-c - 如何将 JSON 对象映射到 Objective C 类?

javascript - HTML 表单 : Which Elements are Sent?

haskell - 如何组成 "Maybe"镜头?

haskell - 在 Haskell 中使用断点进行第二次调试?

algorithm - 在 Haskell 中获取元组列表的第一个元组

xml - 无法替换PowerShell中的字符串

c# - 构造函数未出现在我的 WCF 客户端中,序列化问题?

android - 我如何以编程方式引用/values/strings.xml 文件中的内容?

android - 在 Android Studio 中设置默认单选按钮

android - 不允许 NativeScript Angular 明文 HTTP 流量到本地主机