aeson-pretty (empty) → 0.1
raw patch · 6 files changed
+231/−0 lines, 6 filesdep +aesondep +attoparsecdep +basesetup-changed
Dependencies added: aeson, attoparsec, base, blaze-builder, bytestring, cmdargs, containers, text, vector
Files
- Data/Aeson/Encode/Pretty.hs +62/−0
- LICENSE +30/−0
- Main.hs +46/−0
- README.markdown +23/−0
- Setup.hs +2/−0
- aeson-pretty.cabal +68/−0
+ Data/Aeson/Encode/Pretty.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE OverloadedStrings, RecordWildCards #-}++-- |Aeson-compatible pretty-printing of JSON 'Value's.+module Data.Aeson.Encode.Pretty (encodePretty) where++import Blaze.ByteString.Builder (Builder, toLazyByteString, fromByteString)+import Blaze.ByteString.Builder.Char.Utf8 (fromChar)+import Data.Aeson (Value(..), ToJSON(..))+import qualified Data.Aeson.Encode as Aeson+import Data.ByteString.Lazy (ByteString)+import Data.List (intersperse)+import Data.Map (assocs)+import Data.Monoid (mappend, mconcat, mempty)+import Data.Text (Text)+import Data.Vector (toList)+++type Indent = Int++-- |A drop-in replacement for aeson's 'Aeson.encode' function, producing +-- JSON-ByteStrings for human readers.+encodePretty :: Value -> ByteString+encodePretty = toLazyByteString . fromValue 0++fromValue :: Indent -> Value -> Builder+fromValue lvl = go+ where+ go (Array v) = renderCompound lvl ('[',']') renderListItem (toList v)+ go (Object v) = renderCompound lvl ('{','}') renderPair (assocs v)+ go v = Aeson.fromValue v++renderCompound :: Indent+ -> (Char, Char)+ -> (Indent -> a -> Builder)+ -> [a]+ -> Builder+renderCompound lvl (delimL,delimR) render content = + fromChar delimL `mappend` content' `mappend` fromChar delimR+ where+ content' = if null content then mempty+ else mconcat+ [ newLine+ , mconcat . intersperse (fromChar ',' `mappend` newLine) $+ map (render $ lvl+1) content+ , newLine+ , indent lvl+ ]+ newLine = fromChar '\n'++renderListItem :: Indent -> Value -> Builder+renderListItem lvl v = indent lvl `mappend` fromValue lvl v++renderPair :: Indent -> (Text, Value) -> Builder+renderPair lvl (k,v) =+ mconcat [ indent lvl+ , Aeson.fromValue (toJSON k)+ , fromByteString ": "+ , fromValue lvl v+ ]++indent :: Indent -> Builder+indent lvl = mconcat $ replicate (lvl*4) $ fromChar ' '
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Falko Peters++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Falko Peters nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Main.hs view
@@ -0,0 +1,46 @@+{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}+module Main (main) where++import Prelude hiding (interact, concat)+import Data.Aeson (Value(..), json, encode)+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 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."+ &= details info++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 (concat . map ((`append` pack "\n") . enc) . values)++values :: ByteString -> [Value]+values s = case parse json s of+ Done rest v -> v : values rest+ Fail rest _ _+ | L.null rest -> []+ | otherwise -> error "invalid json"
+ README.markdown view
@@ -0,0 +1,23 @@+# Welcome to aeson-pretty++This is a JSON pretty-printing Haskell library compatible with [aeson](http://hackage.haskell.org/package/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 replacement for aeson's `encode` function, producing JSON-ByteStrings for human readers.++The **command-line tool** reads JSON from stdin and writes prettified JSON to stdout. It also offers a complementary "compact"-mode, essentially the opposite of pretty-printing.+++# Join in!++We are happy to receive bug reports, fixes, documentation enhancements, and other improvements.++Please report bugs via the+[github issue tracker](http://github.com/informatikr/aeson-pretty/issues).++Master [git repository](http://github.com/informatikr/aeson-pretty):++* `git clone git://github.com/informatikr/aeson-pretty.git`++# Authors++This library is written and maintained by Falko Peters, <falko.peters@gmail.com>.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ aeson-pretty.cabal view
@@ -0,0 +1,68 @@+name: aeson-pretty+version: 0.1+license: BSD3+license-file: LICENSE+category: Text, Web, JSON+copyright: Copyright 2011 Falko Peters+author: Falko Peters <falko.peters@gmail.com>+maintainer: Falko Peters <falko.peters@gmail.com>+stability: experimental+tested-with: GHC == 6.12.3+cabal-version: >= 1.8+homepage: http://github.com/informatikr/aeson-pretty+bug-reports: http://github.com/informatikr/aeson-pretty/issues+build-type: Simple+synopsis: JSON pretty-printing library and command-line tool.+description:+ 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+ replacement for aeson's "encode" function, producing JSON-ByteStrings for + human readers.+ .+ The command-line tool reads JSON from stdin and writes prettified JSON+ to stdout. It also offers a complementary "compact"-mode, essentially the+ opposite of pretty-printing.++extra-source-files:+ README.markdown++executable aeson-pretty+ main-is: Main.hs++ build-depends:+ aeson == 0.3.2.*,+ attoparsec >= 0.8.6.1,+ base == 4.*,+ blaze-builder >= 0.2.1.4,+ bytestring == 0.9.*,+ cmdargs == 0.7.*,+ containers,+ vector >= 0.7,+ text++ ghc-options: -Wall+ ghc-prof-options: -auto-all+ +library+ exposed-modules:+ Data.Aeson.Encode.Pretty++ other-modules:++ build-depends:+ aeson == 0.3.2.*,+ attoparsec >= 0.8.6.1,+ base == 4.*,+ blaze-builder >= 0.2.1.4,+ bytestring == 0.9.*,+ containers,+ vector >= 0.7,+ text++ ghc-options: -Wall++source-repository head+ type: git+ location: http://github.com/informatikr/aeson-pretty