1

Minimal test code (bs.hs):

import qualified Data.Binary as B
import qualified Data.ByteString.Lazy.Char8 as BSLC

main = do
    BSLC.putStr $ B.encode $ Pad $ BSLC.pack "xxx"

data Pad = Pad BSLC.ByteString
instance B.Binary Pad where
    put (Pad p) = do
        B.put p
    get = do
        p <- B.get
        return $ Pad p

And I get:

% runghc bs.hs | od -c
0000000   \0  \0  \0  \0  \0  \0  \0 003   x   x   x                    
0000013
% ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.10.2

I expect to get "xxx". I have no idea how the first 8 bytes (7 x \0 + 1 x 003) come from.

uebayasi
  • 46
  • 4
  • 3
    It's the length of the ByteString as a 64-bit (8 byte) number. If you want a specific format for your output, you should not be using the Binary class. – Derek Elkins left SE Feb 08 '16 at 01:59
  • After rereading the documentation, makes sense. I started looking at Data.Binary.Get and Data.Binary.Put but somehow ended up with using Data.Binary. – uebayasi Feb 08 '16 at 03:44
  • @DerekElkins since your comment answered the question, you should provide it as an answer... – sclv Mar 05 '16 at 04:20

1 Answers1

1

From my comment, the explanation of the output is that the format Binary currently uses for serializing ByteStrings is to output the length as a 64-bit integer followed by the contents of the ByteString.

Generally, though, the Binary class is of limited use. There's no guarantee of forward or backward compatibility, no mechanism to even allow for schema evolution, no standard or specification for the instances to allow cross-language interoperability. It's really only useful if you want an opaque binary blob that you will only store temporarily and/or communicate with nodes that you have a high level of control over. As a consumer, this is all you can expect of a Binary instance. While there is no reason not to make a Binary instance for a type for which you create serialization code, if you offer any guarantees beyond the above you should probably (also) present it separately from the Binary class. Ideally, as an instance of some class which communicates those extra guarantees, e.g. a class for serializing to a specific format such as Avro.

All this is only directed at the Binary class. The rest of the binary package is quite useful for its intended purpose.

Derek Elkins left SE
  • 2,079
  • 12
  • 14