forked from informatikr/aeson-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
48 lines (41 loc) · 1.49 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE DeriveDataTypeable, RecordWildCards, OverloadedStrings #-}
module Main (main) where
import Prelude hiding (interact, concat, unlines, null)
import Data.Aeson (Value(..), json', encode)
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.Attoparsec.Lazy (Result(..), parse)
import Data.ByteString.Lazy.Char8 (ByteString, interact, unlines, null)
import Data.Version (showVersion)
import Paths_aeson_pretty (version)
import System.Console.CmdArgs
data Options = Opts { compact :: Bool } deriving (Data, Typeable)
opts :: Options
opts = Opts { compact = False &= help "Compact output." &= groupname "Flags"}
&= program prog
&= summary smry
&= details info
where
prog = "aeson-pretty"
smry = prog++" "++showVersion version++": Pretty JSON, the easy way."
info :: [String]
info =
[ "Read JSON from stdin and pretty-print to stdout. The complementary "
, "compact-mode removes whitespace from the input."
, ""
, "(c) Falko Peters 2011"
, ""
, "License: BSD3, for details see the source-repository at"
, "http://www.github.com/informatikr/aeson-pretty."
, ""
]
main :: IO ()
main = do
Opts{..} <- cmdArgs opts
let enc = if compact then encode else encodePretty
interact $ unlines . map enc . values
values :: ByteString -> [Value]
values s = case parse json' s of
Done rest v -> v : values rest
Fail rest _ _
| null rest -> []
| otherwise -> error "invalid json"