By default, Aeson encode or encodePretty do not append a trailing newline to the ByteString. I.e., when viewing the resulting file in vim I see the [noeol] indicator.
What is a performant/idiomatic way to append a newline to the file?
Here's an SSCCE for Main.hs:
{-# LANGUAGE DeriveGeneric #-}
import Data.Aeson
import Data.Aeson.Encode.Pretty
import GHC.Generics
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Internal as BS
data ExampleRecord =
ExampleRecord {
field1 :: String
, field2 :: String
} deriving (Show, Generic)
instance ToJSON ExampleRecord
main = L.writeFile "foo.json" $ encodePretty (ExampleRecord "a" "b")
with accompanying myproj.cabal file:
name: myproj
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
executable emit-json
main-is: Main.hs
build-depends:
base >=4.8 && <4.9
, aeson
, aeson-pretty
, bytestring
default-language: Haskell2010
I am able to append a newline to foo.json by using the snoc function from ByteString, changing the last line in Main.hs to this:
main = L.writeFile "foo.json" $ encodePretty (ExampleRecord "a" "b") `L.snoc` BS.c2w '\n'
However, the snoc documentation describes this as an O(n) operation. Should there be a way to append this newline in constant time?