haskell - Haskell 中的序列化

标签 haskell serialization types template-haskell

从鸟的角度来看,我的问题是:as-is 是否有通用机制? Haskell中的数据序列化?
介绍
问题的根源确实不在于 Haskell。有一次,我尝试序列化一个 python 字典,其中对象的散列函数非常重。我发现在python中,默认的字典序列化并没有保存字典的内部结构,只是转储了一个键值对列表。结果,反序列化过程非常耗时,没有办法与之抗衡。我确信在 Haskell 中有一种方法,因为在我看来,使用 BFS 或 DFS 将纯 Haskell 类型自动转换为字节流应该没有问题。令人惊讶的是,但事实并非如此。讨论了这个问题here (以下引用)

Currently, there is no way to make HashMap serializable without modifying the HashMap library itself. It is not possible to make Data.HashMap an instance of Generic (for use with cereal) using stand-alone deriving as described by @mergeconflict's answer, because Data.HashMap does not export all its constructors (this is a requirement for GHC). So, the only solution left to serialize the HashMap seems to be to use the toList/fromList interface.


当前问题
我对 Data.Trie 有同样的问题bytestring-trie package .为我的数据构建一个 trie 非常耗时,我需要一种机制来序列化和反序列化这个轮胎。但是,它看起来像以前的情况,我看不出如何制作 Data.Trie Generic 的一个实例(或者,我错了)?
所以问题是:
  • 是否有某种通用机制将纯 Haskell 类型投影到字节字符串?如果不是,是基本限制还是缺乏实现?
  • 如果没有,修改 bytestring-trie package 最轻松的方法是什么?使其成为 Generic 的实例并使用 Data.Store 进行序列化
  • 最佳答案

  • 有一种方法使用 compact regions ,但有一个很大的限制:

  • Our binary representation contains direct pointers to the info tables of objects in the region. This means that the info tables of the receiving process must be laid out in exactly the same way as from the original process; in practice, this means using static linking, using the exact same binary and turning off ASLR. This API does NOT do any safety checking and will probably segfault if you get it wrong. DO NOT run this on untrusted input.


    这也让我们深入了解目前不可能的通用序列化。数据结构包含非常具体的指针,如果您使用不同的二进制文件,这些指针可能会有所不同。将原始字节读入另一个二进制文件将导致指针无效。
    有一些讨论in this GitHub issue关于弱化这个要求。
  • 我认为正确的方法是在上游打开问题或拉取请求以导出 internal module 中的数据构造函数.这就是 HashMap 发生的事情。现在可以在 its internal module 中完全访问.

  • 更新:似乎已经有类似的open issue对这个。

    关于haskell - Haskell 中的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68215186/

    相关文章:

    c# - 将父类(super class)型数组拆分为子类型数组

    haskell - 如何在 Haskell 中触发类型族模式匹配错误?

    haskell - 使用 `ghc-pkg` 注册多个版本的软件包?

    Java 套接字 : Request-response pattern with object streams

    oracle - 当您尝试避免使用魔数(Magic Number)来表示字符串长度时,clob 类型是否在 PL/SQL 代码中使用了不好的做法?

    string - Scala String 与 java.lang.String - 类型推断

    haskell - 将字符串列表连接到一个字符串 haskell

    haskell - Haskell:如何检测 “lazy memory leaks”

    c# - StringContent 与 ObjectContent

    php - 将 PHP 数组数据保存到 mysql 表的最佳方法