Skip to content

Commit

Permalink
Bring back Aeson number formatting as the default.
Browse files Browse the repository at this point in the history
This default is actually quite sensible: not only does it reasonably switch
between fixed-decimal and scientific notation, it also discerns fractional
from integral numbers.

The new `Generic` option would simply print _all_ numbers in floating-point
format, which is not really acceptable, seeing as JSON is a dynamic format
and uses `Number` for numbers of any type.
E.g. 17.0 is not really an integer, is it?

Now `Generic` again prints integers as integers.
  • Loading branch information
leftaroundabout committed Sep 8, 2016
1 parent 375afe8 commit d7a6c6e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Data/Aeson/Encode/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ data PState = PState { pLevel :: Int
data Indent = Spaces Int | Tab

data NumberFormat
-- | Use decimal notation for values between 0.1 and 9,999,999, and scientific
-- | The standard behaviour of the 'Aeson.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.
= Generic
-- | Scientific notation (e.g. 2.3e123).
Expand Down Expand Up @@ -195,7 +197,7 @@ fromIndent PState{..} = mconcat (replicate pLevel pIndent)

fromNumber :: PState -> S.Scientific -> Builder
fromNumber st x = case pNumFormat st of
Generic -> formatScientificBuilder S.Generic Nothing x
Generic -> Aeson.encodeToTextBuilder $ Number x
Scientific -> formatScientificBuilder S.Exponent Nothing x
Decimal -> formatScientificBuilder S.Fixed Nothing x
Custom f -> f x

0 comments on commit d7a6c6e

Please sign in to comment.