aeson-pretty 0.2.1 → 0.3
raw patch · 3 files changed
+36/−35 lines, 3 files
Files
- Data/Aeson/Encode/Pretty.hs +23/−25
- Main.hs +11/−8
- aeson-pretty.cabal +2/−2
Data/Aeson/Encode/Pretty.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE OverloadedStrings, RecordWildCards #-}+{-# LANGUAGE OverloadedStrings #-} -- |Aeson-compatible pretty-printing of JSON 'Value's. module Data.Aeson.Encode.Pretty (encodePretty) where@@ -19,44 +19,42 @@ -- |A drop-in replacement for aeson's 'Aeson.encode' function, producing -- JSON-ByteStrings for human readers.-encodePretty :: Value -> ByteString-encodePretty = toLazyByteString . fromValue 0+encodePretty :: ToJSON a => a -> ByteString+encodePretty = toLazyByteString . fromValue 0 . toJSON fromValue :: Indent -> Value -> Builder-fromValue lvl = go+fromValue ind = go where- go (Array v) = fromCompound lvl ('[',']') fromListItem (toList v)- go (Object v) = fromCompound lvl ('{','}') fromPair (assocs v)+ go (Array v) = fromCompound ind ('[',']') fromValue (toList v)+ go (Object v) = fromCompound ind ('{','}') fromPair (assocs v) go v = Aeson.fromValue v fromCompound :: Indent- -> (Char, Char)- -> (Indent -> a -> Builder)- -> [a]- -> Builder-fromCompound lvl (delimL,delimR) render content = - fromChar delimL `mappend` content' `mappend` fromChar delimR+ -> (Char, Char)+ -> (Indent -> a -> Builder)+ -> [a]+ -> Builder+fromCompound ind (delimL,delimR) fromItem items = + fromChar delimL `mappend` items' `mappend` fromChar delimR where- content' = if null content then mempty+ newLine = fromChar '\n'+ items' = if null items then mempty else mconcat [ newLine , mconcat . intersperse (fromChar ',' `mappend` newLine) $- map (render $ lvl+1) content+ map (\i -> fromIndent (ind+1) `mappend`+ fromItem (ind+1) i)+ items , newLine- , indent lvl+ , fromIndent ind ]- newLine = fromChar '\n' -fromListItem :: Indent -> Value -> Builder-fromListItem lvl v = indent lvl `mappend` fromValue lvl v- fromPair :: Indent -> (Text, Value) -> Builder-fromPair lvl (k,v) =- mconcat [ indent lvl- , Aeson.fromValue (toJSON k)+fromPair ind (k,v) =+ mconcat [ Aeson.fromValue (toJSON k) , fromByteString ": "- , fromValue lvl v+ , fromValue ind v ] -indent :: Indent -> Builder-indent lvl = mconcat $ replicate (lvl*4) $ fromChar ' '+fromIndent :: Indent -> Builder+fromIndent ind = mconcat $ replicate (ind*4) $ fromChar ' '
Main.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}+{-# LANGUAGE DeriveDataTypeable, RecordWildCards, OverloadedStrings #-} module Main (main) where import Prelude hiding (interact, concat)@@ -6,19 +6,22 @@ import Data.Aeson.Encode.Pretty (encodePretty) import Data.Attoparsec.Lazy (Result(..), parse) import Data.ByteString.Lazy (ByteString, interact, concat, append)-import Data.ByteString.Lazy.Char8 (pack) import qualified Data.ByteString.Lazy as L (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. Ignores the -i flag." - &= groupname "Flags"}- &= program "aeson-pretty"- &= summary "aeson-pretty 0.1: Pretty JSON, the easy way."+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 =@@ -28,7 +31,7 @@ , "(c) Falko Peters 2011" , "" , "License: BSD3, for details see the source-repository at"- , "http://www.github.com/informatikr/aeson-pretty"+ , "http://www.github.com/informatikr/aeson-pretty." , "" ] @@ -36,7 +39,7 @@ main = do Opts{..} <- cmdArgs opts let enc = if compact then encode else encodePretty- interact (concat . map ((`append` pack "\n") . enc) . values)+ interact $ concat . map ((`append` "\n") . enc) . values values :: ByteString -> [Value] values s = case parse json s of
aeson-pretty.cabal view
@@ -1,8 +1,8 @@ name: aeson-pretty-version: 0.2.1+version: 0.3 license: BSD3 license-file: LICENSE-category: Text, Web, JSON+category: Text, Web, JSON, Pretty Printer copyright: Copyright 2011 Falko Peters author: Falko Peters <falko.peters@gmail.com> maintainer: Falko Peters <falko.peters@gmail.com>