我正在尝试使用Data.Binary序列化 Map 但出现错误:没有足够的字节数。然后,我尝试使用整数列表制作一个更简单的示例,在这里,这也不起作用。什么可能是错的?我的代码中是否有错误,我误解了什么或者我的安装可能有问题,在这种情况下我该如何解决?
下面是我的测试代码...
import Data.Binary
worldfile = "binarysimple.world"
main = do
ser <- decodeFileOrFail worldfile
case ser of
Right w -> showWorld $ show (w :: [Int])
Left (_,s) -> putStrLn ("the error:"++s) >> newworld
newworld = do
let world = [1,2,3] :: [Int]
showWorld $ show world
encodeFile worldfile $ encode world
showWorld = putStrLn
...以及运行时的输出:
ghci binarysimple.hs
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( binarysimple.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package binary-0.7.1.0 ... linking ... done.
the error:demandInput: not enough bytes
[1,2,3]
*Main> newworld
[1,2,3]
*Main> main
the error:demandInput: not enough bytes
[1,2,3]
*Main> newworld
[1,2,3]
*Main>
Leaving GHCi.
$ ls -l binarysimple.world
-rw-r--r-- 1 btobias staff 40 8 Sep 21:15 binarysimple.world
我不知道确切的格式,但这可能是合理的输出:
$ hexdump -C binarysimple.world
00000000 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 03 |....... ........|
00000010 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 02 |................|
00000020 00 00 00 00 00 00 00 03 |........|
00000028