aeson-pretty 0.7.1 → 0.7.2
raw patch · 2 files changed
+23/−14 lines, 2 filesdep ~aesondep ~base
Dependency ranges changed: aeson, base
Files
- Data/Aeson/Encode/Pretty.hs +20/−11
- aeson-pretty.cabal +3/−3
Data/Aeson/Encode/Pretty.hs view
@@ -3,10 +3,10 @@ -- |Aeson-compatible pretty-printing of JSON 'Value's. module Data.Aeson.Encode.Pretty ( -- * Simple Pretty-Printing- encodePretty,+ encodePretty, encodePrettyToTextBuilder, -- * Pretty-Printing with Configuration Options- encodePretty',+ encodePretty', encodePrettyToTextBuilder', Config (..), defConfig, -- ** Sorting Keys in Objects -- |With the Aeson library, the order of keys in objects is undefined due@@ -60,7 +60,7 @@ import qualified Data.HashMap.Strict as H (toList) import Data.List (intersperse, sortBy, elemIndex) import Data.Maybe (fromMaybe)-import Data.Monoid (mappend, mconcat, mempty)+import Data.Monoid ((<>), mconcat, mempty) import Data.Ord import Data.Text (Text) import Data.Text.Lazy.Builder (Builder, toLazyText)@@ -92,7 +92,7 @@ -- |The default configuration: indent by four spaces per level of nesting, do -- not sort objects by key. ----- > defConfig = Config { confIndent = 4, confSort = mempty }+-- > defConfig = Config { confIndent = 4, confCompare = mempty } defConfig :: Config defConfig = Config { confIndent = 4, confCompare = mempty } @@ -106,17 +106,30 @@ -- |A variant of 'encodePretty' that takes an additional configuration -- parameter. encodePretty' :: ToJSON a => Config -> a -> ByteString-encodePretty' Config{..} = encodeUtf8 . toLazyText . fromValue st . toJSON+encodePretty' conf = encodeUtf8 . toLazyText . encodePrettyToTextBuilder' conf++-- |A drop-in replacement for aeson's 'Aeson.encodeToTextBuilder' function,+-- producing JSON-ByteStrings for human readers.+--+-- Follows the default configuration in 'defConfig'.+encodePrettyToTextBuilder :: ToJSON a => a -> Builder+encodePrettyToTextBuilder = encodePrettyToTextBuilder' defConfig++-- |A variant of 'encodeToTextBuilder' that takes an additional configuration+-- parameter.+encodePrettyToTextBuilder' :: ToJSON a => Config -> a -> Builder+encodePrettyToTextBuilder' Config{..} = fromValue st . toJSON where st = PState confIndent 0 condSort condSort = sortBy (confCompare `on` fst) + fromValue :: PState -> Value -> Builder fromValue st@PState{..} = go where go (Array v) = fromCompound st ("[","]") fromValue (V.toList v) go (Object m) = fromCompound st ("{","}") fromPair (pstSort (H.toList m))- go v = Aeson.fromValue v+ go v = Aeson.encodeToTextBuilder v fromCompound :: PState -> (Builder, Builder)@@ -136,11 +149,7 @@ st' = st { pstLevel = pstLevel + 1 } fromPair :: PState -> (Text, Value) -> Builder-fromPair st (k,v) = Aeson.fromValue (toJSON k) <> ": " <> fromValue st v+fromPair st (k,v) = Aeson.encodeToTextBuilder (toJSON k) <> ": " <> fromValue st v fromIndent :: PState -> Builder fromIndent PState{..} = mconcat $ replicate (pstIndent * pstLevel) " "--(<>) :: Builder -> Builder -> Builder-(<>) = mappend-infixr 6 <>
aeson-pretty.cabal view
@@ -1,5 +1,5 @@ name: aeson-pretty-version: 0.7.1+version: 0.7.2 license: BSD3 license-file: LICENSE category: Text, Web, JSON, Pretty Printer@@ -40,8 +40,8 @@ Data.Aeson.Encode.Pretty build-depends:- aeson >= 0.6,- base == 4.*,+ aeson >= 0.7,+ base >= 4.5, bytestring >= 0.9, vector >= 0.9, text >= 0.11,