packages feed

pretty-simple 1.1.0.0 → 1.1.0.1

raw patch · 6 files changed

+165/−6 lines, 6 filesdep +aesondep +bytestringdep ~basedep ~textnew-component:exe:pretty-simple-json-examplePVP ok

version bump matches the API change (PVP)

Dependencies added: aeson, bytestring

Dependency ranges changed: base, text

API changes (from Hackage documentation)

Files

README.md view
@@ -29,7 +29,7 @@  ```haskell > print bar-Bar {bar1 = 10, bar2 = [Foo {foo1 = 3, foo2 = ["hello","goodbye"], foo3 = 3.3},Foo {foo1 = 3, foo2 = ["hello","goodbye"], foo3 = 3.3}], bar3 = 10.55}+Bar {bar1 = 10.55, bar2 = [Foo {foo1 = 3, foo2 = ["hello","goodbye"]},Foo {foo1 = 3, foo2 = ["hello","goodbye"]}]} ```  This is pretty hard to read.  Imagine if there were more fields or it were even@@ -99,6 +99,61 @@ - Requires every data type to be an instance of some special typeclass (instead   of just `Show`). - Requires all `Show` instances to output valid Haskell code.++## Other Uses++`pretty-simple` can be used to pretty-print any `String` that is similar to+Haskell data types.  The only requirement is that the `String` must correctly+use brackets, parenthese, and braces to indicate nesting.++For example, the+[`pString`](https://hackage.haskell.org/package/pretty-simple/docs/Text-Pretty-Simple.html#v:pString)+function can be used to pretty-print JSON.++Recall our example from before.++```haskell+data Foo = Foo { foo1 :: Integer , foo2 :: [String] } deriving Show++foo :: Foo+foo = Foo 3 ["hello", "goodbye"]++data Bar = Bar { bar1 :: Double , bar2 :: [Foo] } deriving Show++bar :: Bar+bar = Bar 10.55 [foo, foo]+```++You can use [`aeson`](https://hackage.haskell.org/package/aeson) to turn these+data types into JSON.  First, you must derive+[`ToJSON`](https://hackage.haskell.org/package/aeson/docs/Data-Aeson.html#t:ToJSON)+instances for the data types.  It is easiest to do this with Template Haskell:++```haskell+{-# LANGUAGE TemplateHaskell #-}++$(deriveJSON defaultOptions ''Foo)+$(deriveJSON defaultOptions ''Bar)+```++If you run this in `ghci` and type `encode bar`, you'll get output like this:++```haskell+> import Data.Aeson (encode)+> putLazyByteStringLn $ encode bar+{"bar1":10.55,"bar2":[{"foo1":3,"foo2":["hello","goodbye"]},{"foo1":3,"foo2":["hello","goodbye"]}]}+```++Just like Haskell's normal `print` output, this is pretty hard to read.++`pretty-simple` can be used to pretty-print the JSON-encoded `bar` in an+easy-to-read format:++![json example screenshot](/img/pretty-simple-json-example-screenshot.png?raw=true "json example screenshot")++(You can find the `lazyByteStringToString`, `putLazyByteStringLn`,+and `putLazyTextLn` in the [`ExampleJSON.hs`](example/ExampleJSON.hs)+file.)  ## Contributions 
example/Example.hs view
@@ -1,4 +1,14 @@ +{- |+Copyright   :  Dennis Gosnell 2017+License     :  BSD3+Maintainer  :  Dennis Gosnell (cdep.illabout@gmail.com)+Stability   :  experimental+Portability :  unknown++This is an short example of using 'pPrint' from "Text.Pretty.Simple" to+pretty-print a Haskell data type.+-} module Main where  import Text.Pretty.Simple (pPrint)
example/Example/Data.hs view
@@ -1,5 +1,23 @@ {-# LANGUAGE DeriveDataTypeable #-} +{- |+Module      :  Example.Data+Copyright   :  Dennis Gosnell 2017+License     :  BSD3+Maintainer  :  Dennis Gosnell (cdep.illabout@gmail.com)+Stability   :  experimental+Portability :  unknown++This module contains some data types and values that users can use to play+around with pretty-simple.++These data types are also use in the two example programs, as well as the+benchmark for pretty-simple.++Most users should use 'foo' or 'bar'.  'baz' is an extremely large data type,+only used in the benchmark.+-}+ module Example.Data where  import Data.Data (Data)
+ example/ExampleJSON.hs view
@@ -0,0 +1,53 @@+{-# LANGUAGE TemplateHaskell #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++{- |+Copyright   :  Dennis Gosnell 2017+License     :  BSD3+Maintainer  :  Dennis Gosnell (cdep.illabout@gmail.com)+Stability   :  experimental+Portability :  unknown++This is an short example of using 'pString' from "Text.Pretty.Simple" to+pretty-print JSON.+-}++module Main where++import Data.Aeson (encode)+import Data.Aeson.TH (defaultOptions, deriveJSON)+import qualified Data.ByteString.Lazy as LByteString (ByteString, toStrict)+import Data.Text as Text (Text, unpack)+import Data.Text.Encoding (decodeUtf8)+import qualified Data.Text.IO as TextIO (putStrLn)+import qualified Data.Text.Lazy as LText (Text)+import qualified Data.Text.Lazy.IO as LTextIO (putStrLn)+import Text.Pretty.Simple (pString)++import Example.Data (Foo, Bar, bar)++$(deriveJSON defaultOptions ''Foo)+$(deriveJSON defaultOptions ''Bar)++main :: IO ()+main = do+  putStrLn "\nThe following normal \"Data.Aeson.encode\" output:\n"+  putLazyByteStringLn $ encode bar+  putStrLn "\ngets turned into this (using \"Text.Pretty.Simple.pString\"):\n"+  LTextIO.putStrLn . pString . lazyByteStringToString $ encode bar++-- | Convert a 'LByteString.ByteString' to a 'Text.Text' by utf8-encoding it.+lazyByteStringToText :: LByteString.ByteString -> Text.Text+lazyByteStringToText = decodeUtf8 . LByteString.toStrict++-- | Convert a 'LByteString.ByteString' to a 'String' by utf8-encoding it.+lazyByteStringToString :: LByteString.ByteString -> String+lazyByteStringToString = unpack . lazyByteStringToText++-- | Print a 'LByteString.ByteString' to the screen.  Similar to 'putStrLn'.+putLazyByteStringLn :: LByteString.ByteString -> IO ()+putLazyByteStringLn = TextIO.putStrLn . lazyByteStringToText++-- | Print a 'LText.Text' to the screen.  Similar to 'putStrLn'.+putLazyTextLn :: LText.Text -> IO ()+putLazyTextLn = LTextIO.putStrLn
pretty-simple.cabal view
@@ -1,5 +1,5 @@ name:                pretty-simple-version:             1.1.0.0+version:             1.1.0.1 synopsis:            pretty printer for data types with a 'Show' instance. description:         Please see README.md homepage:            https://github.com/cdepillabout/pretty-simple@@ -47,6 +47,23 @@   hs-source-dirs:      example   build-depends:       base                      , pretty-simple+  default-language:    Haskell2010+  ghc-options:         -Wall -threaded -rtsopts -with-rtsopts=-N++  if flag(buildexample)+    buildable:       True+  else+    buildable:       False++executable pretty-simple-json-example+  main-is:             ExampleJSON.hs+  other-modules:       Example.Data+  hs-source-dirs:      example+  build-depends:       base+                     , aeson+                     , bytestring+                     , pretty-simple+                     , text   default-language:    Haskell2010   ghc-options:         -Wall -threaded -rtsopts -with-rtsopts=-N 
src/Text/Pretty/Simple.hs view
@@ -14,12 +14,13 @@ Stability   : experimental Portability : POSIX -This module contains the functions 'pPrint', 'pShow', and 'pString', for-pretty-printing any Haskell data type with a show instance.+This module contains the functions 'pPrint', 'pShow', and 'pString' for+pretty-printing any Haskell data type with a 'Show' instance. -'pPrint' should be the main go-to function when debugging in GHCi.+'pPrint' is the main go-to function when debugging Haskell code.  'pShow' and+'pString' are slight variations on 'pPrint'. -There are other variations of 'pPrint', 'pShow', and 'pString' for printing+The other variations of 'pPrint', 'pShow', and 'pString' are for printing without color and changing the indentation amount.  Most users can ignore these.  See the Examples section at the end of this module for examples of acutally@@ -84,6 +85,11 @@  -- | Similar to 'pShow', but the first argument is a 'String' representing a -- data type that has already been 'show'ed.+--+-- This will work on any 'String' that is similar to a Haskell data type.  The+-- only requirement is that the strings are quoted, and braces, parentheses, and+-- brackets are correctly used to represent indentation.  For example,+-- 'pString' will correctly pretty-print JSON. pString :: String -> Text pString = pStringOpt defaultOutputOptions