From 3d0d55ecf8e7525d9f2344c9660bfc315093edc6 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 18 Feb 2019 22:55:55 -0800 Subject: [PATCH] api: cleanups, simplify ToJSON instances --- hledger-api/hledger-api.hs | 70 ++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/hledger-api/hledger-api.hs b/hledger-api/hledger-api.hs index e1a6106e2..f2307943b 100644 --- a/hledger-api/hledger-api.hs +++ b/hledger-api/hledger-api.hs @@ -166,19 +166,59 @@ hledgerApiApp staticdir j = Servant.serve api server thisacctq = Acct $ accountNameToAccountRegex a -- includes subs return $ accountTransactionsReport ropts j q thisacctq -instance ToJSON Status where toJSON = genericToJSON defaultOptions -- avoiding https://github.com/bos/aeson/issues/290 -instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions -instance ToJSON Decimal where - toJSON = toJSON . show -instance ToJSON Amount where toJSON = genericToJSON defaultOptions -instance ToJSON AmountStyle where toJSON = genericToJSON defaultOptions -instance ToJSON Side where toJSON = genericToJSON defaultOptions -instance ToJSON DigitGroupStyle where toJSON = genericToJSON defaultOptions -instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions -instance ToJSON BalanceAssertion where toJSON = genericToJSON defaultOptions -instance ToJSON Price where toJSON = genericToJSON defaultOptions -instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions -instance ToJSON PostingType where toJSON = genericToJSON defaultOptions +-- avoiding https://github.com/bos/aeson/issues/290 - no longer needed ? +--instance ToJSON Status where toJSON = genericToJSON defaultOptions -- avoiding https://github.com/bos/aeson/issues/290 +--instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions +--instance ToJSON Decimal where toJSON = toJSON . show +--instance ToJSON Amount where toJSON = genericToJSON defaultOptions +--instance ToJSON AmountStyle where toJSON = genericToJSON defaultOptions +--instance ToJSON Side where toJSON = genericToJSON defaultOptions +--instance ToJSON DigitGroupStyle where toJSON = genericToJSON defaultOptions +--instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions +--instance ToJSON BalanceAssertion where toJSON = genericToJSON defaultOptions +--instance ToJSON Price where toJSON = genericToJSON defaultOptions +--instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions +--instance ToJSON PostingType where toJSON = genericToJSON defaultOptions +--instance ToJSON Posting where +-- toJSON Posting{..} = +-- object +-- ["pdate" .= toJSON pdate +-- ,"pdate2" .= toJSON pdate2 +-- ,"pstatus" .= toJSON pstatus +-- ,"paccount" .= toJSON paccount +-- ,"pamount" .= toJSON pamount +-- ,"pcomment" .= toJSON pcomment +-- ,"ptype" .= toJSON ptype +-- ,"ptags" .= toJSON ptags +-- ,"pbalanceassertion" .= toJSON pbalanceassertion +-- ,"ptransactionidx" .= toJSON (maybe "" (show.tindex) ptransaction) +-- ] +--instance ToJSON Transaction where toJSON = genericToJSON defaultOptions +--instance ToJSON Account where +-- toJSON a = +-- object +-- ["aname" .= toJSON (aname a) +-- ,"aebalance" .= toJSON (aebalance a) +-- ,"aibalance" .= toJSON (aibalance a) +-- ,"anumpostings" .= toJSON (anumpostings a) +-- ,"aboring" .= toJSON (aboring a) +-- ,"aparentname" .= toJSON (maybe "" aname $ aparent a) +-- ,"asubs" .= toJSON (map toJSON $ asubs a) +-- ] + +-- Convert things to JSON for serving to clients +instance ToJSON Status +instance ToJSON GenericSourcePos +instance ToJSON Decimal where toJSON = toJSON . show +instance ToJSON Amount +instance ToJSON AmountStyle +instance ToJSON Side +instance ToJSON DigitGroupStyle +instance ToJSON MixedAmount +instance ToJSON BalanceAssertion +instance ToJSON Price +instance ToJSON MarketPrice +instance ToJSON PostingType instance ToJSON Posting where toJSON Posting{..} = object @@ -193,7 +233,7 @@ instance ToJSON Posting where ,"pbalanceassertion" .= toJSON pbalanceassertion ,"ptransactionidx" .= toJSON (maybe "" (show.tindex) ptransaction) ] -instance ToJSON Transaction where toJSON = genericToJSON defaultOptions +instance ToJSON Transaction instance ToJSON Account where toJSON a = object @@ -205,6 +245,8 @@ instance ToJSON Account where ,"aparentname" .= toJSON (maybe "" aname $ aparent a) ,"asubs" .= toJSON (map toJSON $ asubs a) ] + +-- convert things to Schema for swagger API description instance ToSchema Status instance ToSchema GenericSourcePos instance ToSchema Decimal