1

Currently, I'm using something like this to write JSON content (my_json) to a file (my_output_filepath):

import           Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Lazy.UTF8 as U

writeFile my_output_filepath $ U.toString $ encodePretty my_json

This works, but I'm wondering whether it is necessary to convert the ByteString that is returned by (encodePretty) to a String before writing it to a file, or if it incurs a performance penalty.

I see there is a variant of writeFile that accepts a ByteString as input. However, when I try to use it, I get this error:

Couldn't match expected type ‘B.ByteString’
            with actual type ‘U.ByteString’
NB: ‘B.ByteString’ is defined in ‘Data.ByteString.Internal’
    ‘U.ByteString’ is defined in ‘Data.ByteString.Lazy.Internal’

Is there some way to get the Lazy variant of ByteString to interoperate with writeFile?

kostmo
  • 6,222
  • 4
  • 40
  • 51
  • 2
    Could you please post the part of your source code which causes the error? I can't see where the strict bytestring comes from, `encodePretty` returns lazy bytestring and `writeFile` from `Data.ByteString.Lazy` expects lazy bytestring as well. – zakyggaps Apr 17 '16 at 04:04
  • 2
    Could it be that you're importing the `writeFile` from the strict bytestring module `Data.ByteString`? Make sure you're definitely using the lazy one you linked to. – Will Sewell Apr 17 '16 at 10:28
  • that not enough? https://hackage.haskell.org/package/bytestring-0.10.6.0/docs/Data-ByteString-Lazy.html#v:hPut – d8d0d65b3f7cf42 Apr 17 '16 at 18:53
  • @WillSewell you are correct. D'oh. – kostmo Apr 17 '16 at 20:51

0 Answers0