lib: Do not round Decimal before applying JSON representation if
existing representation is small enough. Previously the JSON representation of Decimal was rounded to 10 points of precision before serialising. This sometimes results in an unnecessary increase of precision.
This commit is contained in:
		
							parent
							
								
									55772cbd9b
								
							
						
					
					
						commit
						a529207ae7
					
				| @ -41,8 +41,8 @@ import           Data.Aeson | |||||||
| import           Data.Aeson.Encode.Pretty (encodePrettyToTextBuilder) | import           Data.Aeson.Encode.Pretty (encodePrettyToTextBuilder) | ||||||
| --import           Data.Aeson.TH | --import           Data.Aeson.TH | ||||||
| import qualified Data.ByteString.Lazy as BL | import qualified Data.ByteString.Lazy as BL | ||||||
| import           Data.Decimal | import           Data.Decimal (DecimalRaw(..), roundTo) | ||||||
| import           Data.Maybe | import           Data.Maybe (fromMaybe) | ||||||
| import qualified Data.Text.Lazy    as TL | import qualified Data.Text.Lazy    as TL | ||||||
| import qualified Data.Text.Lazy.IO as TL | import qualified Data.Text.Lazy.IO as TL | ||||||
| import qualified Data.Text.Lazy.Builder as TB | import qualified Data.Text.Lazy.Builder as TB | ||||||
| @ -80,13 +80,13 @@ instance ToJSON GenericSourcePos | |||||||
| -- and that the overall number of significant digits in the floating point | -- and that the overall number of significant digits in the floating point | ||||||
| -- remains manageable in practice. (I'm not sure how to limit the number | -- remains manageable in practice. (I'm not sure how to limit the number | ||||||
| -- of significant digits in a Decimal right now.) | -- of significant digits in a Decimal right now.) | ||||||
| instance ToJSON Decimal where | instance (Integral a, ToJSON a) => ToJSON (DecimalRaw a) where | ||||||
|   toJSON d = object |   toJSON d = object | ||||||
|     ["decimalPlaces"   .= toJSON decimalPlaces |       [ "decimalPlaces"   .= toJSON (decimalPlaces d') | ||||||
|     ,"decimalMantissa" .= toJSON decimalMantissa |       , "decimalMantissa" .= toJSON (decimalMantissa d') | ||||||
|     ,"floatingPoint"   .= toJSON (fromRational $ toRational d' :: Double) |       , "floatingPoint"   .= toJSON (realToFrac d' :: Double) | ||||||
|     ] |       ] | ||||||
|     where d'@Decimal{..} = roundTo 10 d |     where d' = if decimalPlaces d <= 10 then d else roundTo 10 d | ||||||
| 
 | 
 | ||||||
| instance ToJSON Amount | instance ToJSON Amount | ||||||
| instance ToJSON AmountStyle | instance ToJSON AmountStyle | ||||||
|  | |||||||
| @ -18,8 +18,8 @@ $ hledger -f- reg --output-format=json | |||||||
|                     "acommodity": "AAA", |                     "acommodity": "AAA", | ||||||
|                     "aquantity": { |                     "aquantity": { | ||||||
|                         "floatingPoint": 1, |                         "floatingPoint": 1, | ||||||
|                         "decimalPlaces": 10, |                         "decimalPlaces": 1, | ||||||
|                         "decimalMantissa": 10000000000 |                         "decimalMantissa": 10 | ||||||
|                     }, |                     }, | ||||||
|                     "aismultiplier": false, |                     "aismultiplier": false, | ||||||
|                     "astyle": { |                     "astyle": { | ||||||
| @ -46,8 +46,8 @@ $ hledger -f- reg --output-format=json | |||||||
|                 "acommodity": "AAA", |                 "acommodity": "AAA", | ||||||
|                 "aquantity": { |                 "aquantity": { | ||||||
|                     "floatingPoint": 1, |                     "floatingPoint": 1, | ||||||
|                     "decimalPlaces": 10, |                     "decimalPlaces": 1, | ||||||
|                     "decimalMantissa": 10000000000 |                     "decimalMantissa": 10 | ||||||
|                 }, |                 }, | ||||||
|                 "aismultiplier": false, |                 "aismultiplier": false, | ||||||
|                 "astyle": { |                 "astyle": { | ||||||
| @ -76,8 +76,8 @@ $ hledger -f- bal --output-format=json | |||||||
|                     "acommodity": "AAA", |                     "acommodity": "AAA", | ||||||
|                     "aquantity": { |                     "aquantity": { | ||||||
|                         "floatingPoint": 1, |                         "floatingPoint": 1, | ||||||
|                         "decimalPlaces": 10, |                         "decimalPlaces": 1, | ||||||
|                         "decimalMantissa": 10000000000 |                         "decimalMantissa": 10 | ||||||
|                     }, |                     }, | ||||||
|                     "aismultiplier": false, |                     "aismultiplier": false, | ||||||
|                     "astyle": { |                     "astyle": { | ||||||
| @ -97,8 +97,8 @@ $ hledger -f- bal --output-format=json | |||||||
|             "acommodity": "AAA", |             "acommodity": "AAA", | ||||||
|             "aquantity": { |             "aquantity": { | ||||||
|                 "floatingPoint": 1, |                 "floatingPoint": 1, | ||||||
|                 "decimalPlaces": 10, |                 "decimalPlaces": 1, | ||||||
|                 "decimalMantissa": 10000000000 |                 "decimalMantissa": 10 | ||||||
|             }, |             }, | ||||||
|             "aismultiplier": false, |             "aismultiplier": false, | ||||||
|             "astyle": { |             "astyle": { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user