write JSON to file as lazy ByteString directly from Aeson encode output without converting to String

487 Views Asked by At

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?

0

There are 0 best solutions below