print,reg,balcmds: fix JSON output, which wasn't JSON (#689)

This commit is contained in:
Simon Michael 2020-05-23 13:30:22 -07:00
parent 40c296a446
commit 5f1a864c20
4 changed files with 12 additions and 6 deletions

View File

@ -254,6 +254,7 @@ module Hledger.Cli.Commands.Balance (
) where
import Data.Aeson (toJSON)
import Data.Aeson.Text (encodeToLazyText)
import Data.List
import Data.Maybe
--import qualified Data.Map as Map
@ -319,7 +320,7 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do
assrt = not $ ignore_assertions_ $ inputopts_ opts
render = case fmt of
"txt" -> budgetReportAsText ropts
"json" -> (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk
"json" -> (++"\n") . T.unpack . TL.toStrict . encodeToLazyText . toJSON
_ -> const $ error' $ unsupportedOutputFormatError fmt
writeOutput opts $ render budgetreport
@ -330,7 +331,7 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do
"txt" -> multiBalanceReportAsText ropts
"csv" -> (++"\n") . printCSV . multiBalanceReportAsCsv ropts
"html" -> (++"\n") . TL.unpack . L.renderText . multiBalanceReportAsHtml ropts
"json" -> (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk
"json" -> (++"\n") . T.unpack . TL.toStrict . encodeToLazyText . toJSON
_ -> const $ error' $ unsupportedOutputFormatError fmt
writeOutput opts $ render report
@ -345,7 +346,7 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do
render = case fmt of
"txt" -> balanceReportAsText
"csv" -> \ropts r -> (++ "\n") $ printCSV $ balanceReportAsCsv ropts r
"json" -> const $ (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk
"json" -> const $ (++"\n") . T.unpack . TL.toStrict . encodeToLazyText . toJSON
_ -> const $ error' $ unsupportedOutputFormatError fmt
writeOutput opts $ render ropts report

View File

@ -16,9 +16,11 @@ module Hledger.Cli.Commands.Print (
where
import Data.Aeson (toJSON)
import Data.Aeson.Text (encodeToLazyText)
import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as T (toStrict)
import System.Console.CmdArgs.Explicit
import Hledger.Read.CsvReader (CSV, printCSV)
@ -57,7 +59,7 @@ printEntries opts@CliOpts{reportopts_=ropts} j = do
render = case fmt of
"txt" -> entriesReportAsText opts
"csv" -> (++"\n") . printCSV . entriesReportAsCsv
"json" -> (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk
"json" -> (++"\n") . T.unpack . T.toStrict . encodeToLazyText . toJSON
_ -> const $ error' $ unsupportedOutputFormatError fmt
writeOutput opts $ render $ entriesReport ropts q j

View File

@ -19,10 +19,12 @@ module Hledger.Cli.Commands.Register (
) where
import Data.Aeson (toJSON)
import Data.Aeson.Text (encodeToLazyText)
import Data.List
import Data.Maybe
-- import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import System.Console.CmdArgs.Explicit
import Hledger.Read.CsvReader (CSV, CsvRecord, printCSV)
@ -61,7 +63,7 @@ register opts@CliOpts{reportopts_=ropts} j = do
let fmt = outputFormatFromOpts opts
render | fmt=="txt" = postingsReportAsText
| fmt=="csv" = const ((++"\n") . printCSV . postingsReportAsCsv)
| fmt=="json" = const ((++"\n") . pshow . toJSON) -- XXX pshow for pretty output, but it may generate some junk
| fmt=="json" = const ((++"\n") . T.unpack . TL.toStrict . encodeToLazyText . toJSON)
| otherwise = const $ error' $ unsupportedOutputFormatError fmt
writeOutput opts $ render opts $ postingsReport ropts (queryFromOpts d ropts) j

View File

@ -14,6 +14,7 @@ module Hledger.Cli.CompoundBalanceCommand (
) where
import Data.Aeson (toJSON)
import Data.Aeson.Text (encodeToLazyText)
import Data.List (foldl')
import Data.Maybe
import qualified Data.Text as TS
@ -240,7 +241,7 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r
"txt" -> compoundBalanceReportAsText ropts' cbr
"csv" -> printCSV (compoundBalanceReportAsCsv ropts cbr) ++ "\n"
"html" -> (++"\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr
"json" -> (++"\n") $ pshow $ toJSON cbr -- XXX pshow for pretty output, but it may generate some junk
"json" -> (++"\n") $ TS.unpack $ TL.toStrict $ encodeToLazyText $ toJSON cbr
_ -> error' $ unsupportedOutputFormatError fmt
-- | Summarise one or more (inclusive) end dates, in a way that's