on de41665 help sign in
typeable/yaml/exe/yaml2json.hs
28 lines 829 B history raw
1import Prelude hiding (putStr, getContents)
3import Data.Aeson (encode, Value)
4import Data.ByteString (getContents)
5import Data.ByteString.Lazy (putStr)
6import System.Environment (getArgs)
7import System.Exit
9import Data.Yaml (decodeFileEither, decodeEither')
11helpMessage :: IO ()
12helpMessage = putStrLn "yaml2json FILE\n use - as FILE to indicate stdin" >> exitFailure
14showJSON :: Show a => Either a Value -> IO b
15showJSON ejson =
16 case ejson of
17 Left err -> print err >> exitFailure
18 Right res -> putStr (encode (res :: Value)) >> exitSuccess
20main :: IO ()
21main = do
22 args <- getArgs
23 case args of
24 -- strict getContents will read in all of stdin at once
25 (["-"]) -> getContents >>= showJSON . decodeEither'
26 ([f]) -> decodeFileEither f >>= showJSON
27 _ -> helpMessage