lib: Add more efficient toEncoding for custom ToJSON declarations.
This commit is contained in:
parent
a529207ae7
commit
278153effa
@ -81,12 +81,15 @@ instance ToJSON GenericSourcePos
|
||||
-- remains manageable in practice. (I'm not sure how to limit the number
|
||||
-- of significant digits in a Decimal right now.)
|
||||
instance (Integral a, ToJSON a) => ToJSON (DecimalRaw a) where
|
||||
toJSON d = object
|
||||
[ "decimalPlaces" .= toJSON (decimalPlaces d')
|
||||
, "decimalMantissa" .= toJSON (decimalMantissa d')
|
||||
, "floatingPoint" .= toJSON (realToFrac d' :: Double)
|
||||
toJSON = object . decimalKV
|
||||
toEncoding = pairs . mconcat . decimalKV
|
||||
|
||||
decimalKV :: (KeyValue kv, Integral a, ToJSON a) => DecimalRaw a -> [kv]
|
||||
decimalKV d = let d' = if decimalPlaces d <= 10 then d else roundTo 10 d in
|
||||
[ "decimalPlaces" .= decimalPlaces d'
|
||||
, "decimalMantissa" .= decimalMantissa d'
|
||||
, "floatingPoint" .= (realToFrac d' :: Double)
|
||||
]
|
||||
where d' = if decimalPlaces d <= 10 then d else roundTo 10 d
|
||||
|
||||
instance ToJSON Amount
|
||||
instance ToJSON AmountStyle
|
||||
@ -96,6 +99,9 @@ instance ToJSON AmountPrecision where
|
||||
toJSON = toJSON . \case
|
||||
Precision n -> Just n
|
||||
NaturalPrecision -> Nothing
|
||||
toEncoding = toEncoding . \case
|
||||
Precision n -> Just n
|
||||
NaturalPrecision -> Nothing
|
||||
|
||||
instance ToJSON Side
|
||||
instance ToJSON DigitGroupStyle
|
||||
@ -106,7 +112,11 @@ instance ToJSON MarketPrice
|
||||
instance ToJSON PostingType
|
||||
|
||||
instance ToJSON Posting where
|
||||
toJSON Posting{..} = object
|
||||
toJSON = object . postingKV
|
||||
toEncoding = pairs . mconcat . postingKV
|
||||
|
||||
postingKV :: KeyValue kv => Posting -> [kv]
|
||||
postingKV Posting{..} =
|
||||
[ "pdate" .= pdate
|
||||
, "pdate2" .= pdate2
|
||||
, "pstatus" .= pstatus
|
||||
@ -141,7 +151,11 @@ instance ToJSON ClockTime
|
||||
instance ToJSON Journal
|
||||
|
||||
instance ToJSON Account where
|
||||
toJSON a = object
|
||||
toJSON = object . accountKV
|
||||
toEncoding = pairs . mconcat . accountKV
|
||||
|
||||
accountKV :: KeyValue kv => Account -> [kv]
|
||||
accountKV a =
|
||||
[ "aname" .= aname a
|
||||
, "aebalance" .= aebalance a
|
||||
, "aibalance" .= aibalance a
|
||||
|
||||
Loading…
Reference in New Issue
Block a user