module Data.Yaml.Extra (decodeFileEither, encodeFile, module Data.Yaml) where
import Control.Exception
import Data.Yaml hiding (decodeFileEither, encodeFile)
import qualified Data.ByteString as B
import System.IO
decodeFileEither :: FromJSON a => FilePath -> IO (Either ParseException a)
decodeFileEither path =
handle (\(e :: IOException) -> return . Left . OtherParseException . SomeException $ e) $
withFile path ReadMode $
\hnd -> do
fileContent <- B.hGetContents hnd
return $ decodeEither' fileContent
encodeFile :: ToJSON a => FilePath -> a -> IO ()
encodeFile path v = withFile path WriteMode $
\hnd -> do
let fileContent = encode v
B.hPut hnd fileContent