HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Moduleaeson-pretty-0.8.11Haskell2010

Data.Aeson.Encode.Pretty

Aeson-compatible pretty-printing of JSON Values.

Contents
  1. Simple Pretty-Printing
  2. Pretty-Printing with Configuration Options
  3. Sorting Keys in Objects
Synopsis

Simple Pretty-Printing

Pretty-Printing with Configuration Options

The default configuration: indent by four spaces per level of nesting, do not sort objects by key, do not add trailing newline.

defConfig = Config { confIndent = Spaces 4, confCompare = mempty, confNumFormat = Generic, confTrailingNewline = False }
datadata NumberFormat
#

Constructors

  • Generic

    For numbers with absolute value less than 1e19, this follows the standard behaviour of the encode function. Uses integer literals for integers (1, 2, 3...), simple decimals for fractional values between 0.1 and 9,999,999, and scientific notation otherwise. For numbers with an absolute value larger than 1e19, always uses scientific notation.

  • Scientific

    Scientific notation (e.g. 2.3e123).

  • Decimal

    Standard decimal notation

  • Custom (Scientific -> Builder)

    Custom formatting function

Sorting Keys in Objects

With the Aeson library, the order of keys in objects is undefined due to objects being implemented as HashMaps. To allow user-specified key orders in the pretty-printed JSON, encodePretty' can be configured with a comparison function. These comparison functions can be composed using the Monoid interface. Some other useful helper functions to keep in mind are comparing and on.

Consider the following deliberately convoluted example, demonstrating the use of comparison functions:

An object might pretty-print as follows

{
  "baz": ...,
  "bar": ...,
  "foo": ...,
  "quux": ...,
}

which is clearly a confusing order of keys. By using a comparison function such as

comp :: Text -> Text -> Ordering
comp = keyOrder ["foo","bar"] `mappend` comparing length

we can achieve the desired neat result:

{
  "foo": ...,
  "bar": ...,
  "baz": ...,
  "quux": ...,
}
methodmempty :: a

Identity of mappend

Examples
Example1 expression
"Hello world" <> mempty"Hello world"
Example1 expression
mempty <> [1, 2, 3][1,2,3]

Serves as an order-preserving (non-)sort function. Re-exported from Data.Monoid.

Sort keys in their natural order, i.e. by comparing character codes. Re-exported from the Prelude and Data.Ord

valuekeyOrder :: [Text] -> Text -> Text -> Ordering

Sort keys by their order of appearance in the argument list.

Keys that are not present in the argument list are considered to be greater than any key in the list and equal to all keys not in the list. I.e. keys not in the argument list are moved to the end, while their order is preserved.