Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit a5b2c50a74c4bd0942e8c258fc84b54b634aecb4 @informatikr committed Jun 11, 2011
Showing with 218 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +62 −0 Data/Aeson/Encode/Pretty.hs
  3. BIN Data/Aeson/Pretty.o
  4. +30 −0 LICENSE
  5. +46 −0 Main.hs
  6. +7 −0 README.markdown
  7. +2 −0 Setup.hs
  8. +68 −0 aeson-pretty.cabal
3 .gitignore
@@ -0,0 +1,3 @@
+dist
+cabal-dev
+*.json
62 Data/Aeson/Encode/Pretty.hs
@@ -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 ' '
BIN Data/Aeson/Pretty.o
Binary file not shown.
30 LICENSE
@@ -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.
46 Main.hs
@@ -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"
7 README.markdown
@@ -0,0 +1,7 @@
+# aeson-pretty
+
+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.
2 Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
68 aeson-pretty.cabal
@@ -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 -O2
+ 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 -O2
+
+source-repository head
+ type: git
+ location: http://github.com/informatikr/aeson-pretty

0 comments on commit a5b2c50

Please sign in to comment.
Something went wrong with that request. Please try again.