aeson-pretty 0.6 → 0.6.1
raw patch · 3 files changed
+29/−14 lines, 3 filesdep ~bytestringPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: bytestring
API changes (from Hackage documentation)
+ Data.Aeson.Encode.Pretty: encodePretty' :: ToJSON a => Int -> a -> ByteString
Files
- Data/Aeson/Encode/Pretty.hs +15/−5
- Main.hs +10/−5
- aeson-pretty.cabal +4/−4
Data/Aeson/Encode/Pretty.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} -- |Aeson-compatible pretty-printing of JSON 'Value's.-module Data.Aeson.Encode.Pretty (encodePretty) where+module Data.Aeson.Encode.Pretty (encodePretty, encodePretty') where import Data.Aeson (Value(..), ToJSON(..)) import qualified Data.Aeson.Encode as Aeson@@ -15,13 +15,22 @@ import qualified Data.Vector as V (toList) -type Indent = Int+type Indent = (Int, Int) -- (spaces per lvl, lvl) -- |A drop-in replacement for aeson's 'Aeson.encode' function, producing -- JSON-ByteStrings for human readers.+--+-- Indents by four spaces per nesting-level. encodePretty :: ToJSON a => a -> ByteString-encodePretty = encodeUtf8 . toLazyText . fromValue 0 . toJSON+encodePretty = encodePretty' 4 -- default indentation is four spaces +-- |A variant of 'encodePretty' that takes an additional parameter: the number+-- of spaces to indent per nesting-level.+encodePretty' :: ToJSON a => Int -> a -> ByteString+encodePretty' spacesPerLvl = encodeUtf8 . toLazyText . fromValue ind . toJSON+ where+ ind = (spacesPerLvl, 0)+ fromValue :: Indent -> Value -> Builder fromValue ind = go where@@ -42,14 +51,15 @@ ] where items' = mconcat . intersperse ",\n" $- map (\item -> fromIndent (ind+1) <> fromItem (ind+1) item)+ map (\item -> fromIndent ind' <> fromItem ind' item) items+ ind' = let (spacesPerLvl, lvl) = ind in (spacesPerLvl, lvl + 1) fromPair :: Indent -> (Text, Value) -> Builder fromPair ind (k,v) = Aeson.fromValue (toJSON k) <> ": " <> fromValue ind v fromIndent :: Indent -> Builder-fromIndent ind = mconcat $ replicate (ind*4) " "+fromIndent (spacesPerLvl, lvl) = mconcat $ replicate (spacesPerLvl * lvl) " " (<>) :: Builder -> Builder -> Builder (<>) = mappend
Main.hs view
@@ -3,7 +3,7 @@ import Prelude hiding (interact, concat, unlines, null) import Data.Aeson (Value(..), json', encode)-import Data.Aeson.Encode.Pretty (encodePretty)+import Data.Aeson.Encode.Pretty import Data.Attoparsec.Lazy (Result(..), parse) import Data.ByteString.Lazy.Char8 (ByteString, interact, unlines, null) import Data.Version (showVersion)@@ -11,11 +11,16 @@ import System.Console.CmdArgs -data Options = Opts { compact :: Bool } deriving (Data, Typeable)+data Options = Opts { compact :: Bool+ , indent :: Int+ }+ deriving (Data, Typeable) opts :: Options-opts = Opts { compact = False &= help "Compact output." &= groupname "Flags"}- &= program prog+opts = Opts+ { compact = False &= help "Compact output."+ , indent = 4 &= help "Number of spaces per nesting-level (default 4)."+ } &= program prog &= summary smry &= details info where@@ -37,7 +42,7 @@ main :: IO () main = do Opts{..} <- cmdArgs opts- let enc = if compact then encode else encodePretty+ let enc = if compact then encode else encodePretty' indent interact $ unlines . map enc . values values :: ByteString -> [Value]
aeson-pretty.cabal view
@@ -1,5 +1,5 @@ name: aeson-pretty-version: 0.6+version: 0.6.1 license: BSD3 license-file: LICENSE category: Text, Web, JSON, Pretty Printer@@ -16,7 +16,7 @@ A JSON pretty-printing library compatible with aeson as well as a command-line tool to improve readabilty of streams of JSON data. .- The /library/ provides a single function "encodePretty". It is a drop-in+ The /library/ provides the function "encodePretty". It is a drop-in replacement for aeson's "encode" function, producing JSON-ByteStrings for human readers. .@@ -45,7 +45,7 @@ aeson == 0.6.*, attoparsec == 0.10.*, base == 4.*,- bytestring == 0.9.*,+ bytestring >= 0.9 && < 0.11, cmdargs == 0.7.*, containers, vector == 0.9.*,@@ -63,7 +63,7 @@ aeson == 0.6.*, attoparsec == 0.10.*, base == 4.*,- bytestring == 0.9.*,+ bytestring >= 0.9 && < 0.11, containers, vector == 0.9.*, text == 0.11.*,