on de41665 help sign in
typeable/yaml/exe/json2yaml.hs
22 lines 679 B history raw
1import Control.Monad (when)
2import qualified Data.Aeson as J
3import qualified Data.ByteString as S
4import qualified Data.ByteString.Lazy as L
5import System.Environment (getArgs)
7import qualified Data.Yaml as Y
9main :: IO ()
10main = do
11 args <- getArgs
12 when (length args > 2) $ error "Usage: json2yaml [in] [out]"
13 let (input:output:_) = args ++ repeat "-"
14 mval <- fmap J.decode $
15 case input of
16 "-" -> L.getContents
17 _ -> L.readFile input
18 case mval of
19 Nothing -> error "Invalid input JSON"
20 Just val -> case output of
21 "-" -> S.putStr $ Y.encode (val :: Y.Value)
22 _ -> Y.encodeFile output val