Search code examples
haskellserializationbytestring

how to read String from file and convert to Data.ByteString for use in Data.Serialize.decode


I need to analyze a log file that contains lines like:

2018-07-11T17:25:14.07565;  [ZMQ] info; Sending message to; ROne (Addr {_unAddr = "tcp://127.0.0.1:10002"}); ## MSG ##; "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\t127.0.0.1\NUL\NUL\NUL\NUL\NUL\NUL'\DC3\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NAKtcp://127.0.0.1:10003\NUL\NUL\NUL\NUL\NUL\NUL\NUL@h_\214\169\213n\RS\212o\tu|\191\"\207GvP\224\167\222V*n6\140\236q\von0\148\240\nV\157\206\225\251A\240\DC4\US\228\140\253\255L\242\163\&0\134\\\241'\r\229W{WD\218\b\143\213go\194\161o\STXM\154X\195R\128\134eN=\144\153\129\242\133f\149\\:A\213\158C\DLE\b\NUL\NUL\NUL\NUL\NUL\NUL\NULG\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\t127.0.0.1\NUL\NUL\NUL\NUL\NUL\NUL'\DLE\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NAKtcp://127.0.0.1:10000\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL;\154\202\NUL"

The parts with \NUL were written using

Prelude.putStr (show msg)

where msg is a Data.ByteString created by an instance of Data.Serialize

I need to

  1. read the entire String line
  2. select the ByteString portion
  3. turn that portion into a ByteString
  4. then give it to Data.Serialize.decode

It is not clear to me how to do step 3 : turn a String representation of a ByteString into a real ByteString.

My initial try was to give #2 to Data.ByteString.Char8.pack. But, when trying to decode

decode (BSC8.pack l) :: Either String SignedRPC

I get:

Left "too few bytes\nFrom:\tdemandInput\n\n"

Solution

  • Perhaps:

    Data.ByteString Text.Read> readMaybe "\"\\NUL\"" :: Maybe ByteString
    Just "\NUL"