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
|
-- 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 (Integral a, ToJSON a) => ToJSON (DecimalRaw a) where
|
instance (Integral a, ToJSON a) => ToJSON (DecimalRaw a) where
|
||||||
toJSON d = object
|
toJSON = object . decimalKV
|
||||||
[ "decimalPlaces" .= toJSON (decimalPlaces d')
|
toEncoding = pairs . mconcat . decimalKV
|
||||||
, "decimalMantissa" .= toJSON (decimalMantissa d')
|
|
||||||
, "floatingPoint" .= toJSON (realToFrac d' :: Double)
|
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 Amount
|
||||||
instance ToJSON AmountStyle
|
instance ToJSON AmountStyle
|
||||||
@ -96,6 +99,9 @@ instance ToJSON AmountPrecision where
|
|||||||
toJSON = toJSON . \case
|
toJSON = toJSON . \case
|
||||||
Precision n -> Just n
|
Precision n -> Just n
|
||||||
NaturalPrecision -> Nothing
|
NaturalPrecision -> Nothing
|
||||||
|
toEncoding = toEncoding . \case
|
||||||
|
Precision n -> Just n
|
||||||
|
NaturalPrecision -> Nothing
|
||||||
|
|
||||||
instance ToJSON Side
|
instance ToJSON Side
|
||||||
instance ToJSON DigitGroupStyle
|
instance ToJSON DigitGroupStyle
|
||||||
@ -106,7 +112,11 @@ instance ToJSON MarketPrice
|
|||||||
instance ToJSON PostingType
|
instance ToJSON PostingType
|
||||||
|
|
||||||
instance ToJSON Posting where
|
instance ToJSON Posting where
|
||||||
toJSON Posting{..} = object
|
toJSON = object . postingKV
|
||||||
|
toEncoding = pairs . mconcat . postingKV
|
||||||
|
|
||||||
|
postingKV :: KeyValue kv => Posting -> [kv]
|
||||||
|
postingKV Posting{..} =
|
||||||
[ "pdate" .= pdate
|
[ "pdate" .= pdate
|
||||||
, "pdate2" .= pdate2
|
, "pdate2" .= pdate2
|
||||||
, "pstatus" .= pstatus
|
, "pstatus" .= pstatus
|
||||||
@ -141,7 +151,11 @@ instance ToJSON ClockTime
|
|||||||
instance ToJSON Journal
|
instance ToJSON Journal
|
||||||
|
|
||||||
instance ToJSON Account where
|
instance ToJSON Account where
|
||||||
toJSON a = object
|
toJSON = object . accountKV
|
||||||
|
toEncoding = pairs . mconcat . accountKV
|
||||||
|
|
||||||
|
accountKV :: KeyValue kv => Account -> [kv]
|
||||||
|
accountKV a =
|
||||||
[ "aname" .= aname a
|
[ "aname" .= aname a
|
||||||
, "aebalance" .= aebalance a
|
, "aebalance" .= aebalance a
|
||||||
, "aibalance" .= aibalance a
|
, "aibalance" .= aibalance a
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user