@@ -1,4 +1,6 @@
1-- | Prettier YAML encoding.
1-- | Prettier YAML encoding.
2module Data.Yaml.Pretty
4module Data.Yaml.Pretty
3 ( encodePretty
5 ( encodePretty
@@ -18,17 +20,25 @@ import Data.List (sortBy)
18import Data.ByteString (ByteString)
20import Data.ByteString (ByteString)
19import Prelude hiding (null)
21import Prelude hiding (null)
21data Config = Config
25data Config = Config
22 { confCompare :: Text -> Text -> Ordering -- ^ Function used to sort keys in objects
26 { confCompare :: Text -> Text -> Ordering -- ^ Function used to sort keys in objects
25-- | The default configuration: do not sort objects.
29-- | The default configuration: do not sort objects.
26defConfig :: Config
32defConfig :: Config
27defConfig = Config mempty
33defConfig = Config mempty
29getConfCompare :: Config -> Text -> Text -> Ordering
37getConfCompare :: Config -> Text -> Text -> Ordering
30getConfCompare = confCompare
38getConfCompare = confCompare
32setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
42setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
33setConfCompare cmp c = c { confCompare = cmp }
43setConfCompare cmp c = c { confCompare = cmp }
@@ -42,5 +52,7 @@ pretty cfg = go
42 go (Bool b) = bool b
52 go (Bool b) = bool b
44-- | Configurable 'encode'.
54-- | Configurable 'encode'.
45encodePretty :: ToJSON a => Config -> a -> ByteString
57encodePretty :: ToJSON a => Config -> a -> ByteString
46encodePretty cfg = toByteString . pretty cfg . toJSON
58encodePretty cfg = toByteString . pretty cfg . toJSON