imp: json: The keys of JSON objects are now displayed in alphabetical order, making it stable across different systems and compilers.

This commit is contained in:
Stephen Morgan 2021-07-21 12:30:01 +10:00 committed by Simon Michael
parent 50b915d12a
commit c606f874fb
2 changed files with 51 additions and 49 deletions

View File

@ -34,13 +34,13 @@ module Hledger.Data.Json (
) where
import Data.Aeson
import Data.Aeson.Encode.Pretty (encodePrettyToTextBuilder)
import Data.Aeson.Encode.Pretty (Config(..), Indent(..), NumberFormat(..),
encodePretty', encodePrettyToTextBuilder')
--import Data.Aeson.TH
import qualified Data.ByteString.Lazy as BL
import Data.Decimal (DecimalRaw(..), roundTo)
import Data.Maybe (fromMaybe)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
import qualified Data.Text.Lazy.Builder as TB
import GHC.Generics (Generic)
import System.Time (ClockTime)
@ -259,15 +259,18 @@ instance FromJSON (DecimalRaw Integer)
-- Utilities
-- | Config for pretty printing JSON output.
jsonConf :: Config
jsonConf = Config{confIndent=Spaces 4,confCompare=compare, confNumFormat=Generic, confTrailingNewline=True}
-- | Show a JSON-convertible haskell value as pretty-printed JSON text.
toJsonText :: ToJSON a => a -> TL.Text
toJsonText = TB.toLazyText . (<> TB.fromText "\n") . encodePrettyToTextBuilder
toJsonText = TB.toLazyText . encodePrettyToTextBuilder' jsonConf
-- | Write a JSON-convertible haskell value to a pretty-printed JSON file.
-- Eg: writeJsonFile "a.json" nulltransaction
writeJsonFile :: ToJSON a => FilePath -> a -> IO ()
writeJsonFile f = TL.writeFile f . toJsonText
-- we write with Text and read with ByteString, is that fine ?
writeJsonFile f = BL.writeFile f . encodePretty' jsonConf
-- | Read a JSON file and decode it to the target type, or raise an error if we can't.
-- Eg: readJsonFile "a.json" :: IO Transaction
@ -280,4 +283,3 @@ readJsonFile f = do
case fromJSON v :: FromJSON a => Result a of
Error e -> error e
Success t -> return t

View File

@ -10,52 +10,52 @@ $ hledger -f- reg --output-format=json
null,
"",
{
"pbalanceassertion": null,
"pstatus": "Unmarked",
"paccount": "a",
"pamount": [
{
"aprice": null,
"acommodity": "AAA",
"aquantity": {
"floatingPoint": 1,
"decimalPlaces": 1,
"decimalMantissa": 10
},
"aismultiplier": false,
"aprice": null,
"aquantity": {
"decimalMantissa": 10,
"decimalPlaces": 1,
"floatingPoint": 1
},
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
],
"ptransaction_": "1",
"paccount": "a",
"pdate": null,
"ptype": "VirtualPosting",
"pbalanceassertion": null,
"pcomment": "",
"pdate": null,
"pdate2": null,
"poriginal": null,
"pstatus": "Unmarked",
"ptags": [],
"poriginal": null
"ptransaction_": "1",
"ptype": "VirtualPosting"
},
[
{
"aprice": null,
"acommodity": "AAA",
"aquantity": {
"floatingPoint": 1,
"decimalPlaces": 1,
"decimalMantissa": 10
},
"aismultiplier": false,
"aprice": null,
"aquantity": {
"decimalMantissa": 10,
"decimalPlaces": 1,
"floatingPoint": 1
},
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
]
@ -72,20 +72,20 @@ $ hledger -f- bal --output-format=json
0,
[
{
"aprice": null,
"acommodity": "AAA",
"aquantity": {
"floatingPoint": 1,
"decimalPlaces": 1,
"decimalMantissa": 10
},
"aismultiplier": false,
"aprice": null,
"aquantity": {
"decimalMantissa": 10,
"decimalPlaces": 1,
"floatingPoint": 1
},
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
]
@ -93,20 +93,20 @@ $ hledger -f- bal --output-format=json
],
[
{
"aprice": null,
"acommodity": "AAA",
"aquantity": {
"floatingPoint": 1,
"decimalPlaces": 1,
"decimalMantissa": 10
},
"aismultiplier": false,
"aprice": null,
"aquantity": {
"decimalMantissa": 10,
"decimalPlaces": 1,
"floatingPoint": 1
},
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
]