Skip to content

Commit

Permalink
imp: json: The keys of JSON objects are now displayed in alphabetical…
Browse files Browse the repository at this point in the history
… order, making it stable across different systems and compilers.
  • Loading branch information
Xitian9 committed Jul 21, 2021
1 parent 8653510 commit c2be054
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
6 changes: 4 additions & 2 deletions hledger-lib/Hledger/Data/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Hledger.Data.Json (
) where

import Data.Aeson
import Data.Aeson.Encode.Pretty (encodePrettyToTextBuilder)
import Data.Aeson.Encode.Pretty (Config(..), encodePrettyToTextBuilder', defConfig)
--import Data.Aeson.TH
import qualified Data.ByteString.Lazy as BL
import Data.Decimal (DecimalRaw(..), roundTo)
Expand Down Expand Up @@ -261,7 +261,9 @@ instance FromJSON (DecimalRaw Integer)

-- | 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' conf
where
conf = defConfig{confCompare=compare, confTrailingNewline=True}

-- | Write a JSON-convertible haskell value to a pretty-printed JSON file.
-- Eg: writeJsonFile "a.json" nulltransaction
Expand Down
70 changes: 35 additions & 35 deletions hledger/test/json.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@ $ hledger -f- reg --output-format=json
null,
"",
{
"pbalanceassertion": null,
"pstatus": "Unmarked",
"paccount": "a",
"pamount": [
{
"aprice": null,
"acommodity": "AAA",
"aismultiplier": false,
"aprice": null,
"aquantity": {
"floatingPoint": 1,
"decimalMantissa": 10,
"decimalPlaces": 1,
"decimalMantissa": 10
"floatingPoint": 1
},
"aismultiplier": false,
"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",
"aismultiplier": false,
"aprice": null,
"aquantity": {
"floatingPoint": 1,
"decimalMantissa": 10,
"decimalPlaces": 1,
"decimalMantissa": 10
"floatingPoint": 1
},
"aismultiplier": false,
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
]
Expand All @@ -72,41 +72,41 @@ $ hledger -f- bal --output-format=json
0,
[
{
"aprice": null,
"acommodity": "AAA",
"aismultiplier": false,
"aprice": null,
"aquantity": {
"floatingPoint": 1,
"decimalMantissa": 10,
"decimalPlaces": 1,
"decimalMantissa": 10
"floatingPoint": 1
},
"aismultiplier": false,
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
]
]
],
[
{
"aprice": null,
"acommodity": "AAA",
"aismultiplier": false,
"aprice": null,
"aquantity": {
"floatingPoint": 1,
"decimalMantissa": 10,
"decimalPlaces": 1,
"decimalMantissa": 10
"floatingPoint": 1
},
"aismultiplier": false,
"astyle": {
"ascommodityside": "R",
"asdigitgroups": null,
"ascommodityspaced": true,
"asprecision": 1,
"asdecimalpoint": "."
"asdecimalpoint": ".",
"asdigitgroups": null,
"asprecision": 1
}
}
]
Expand Down

0 comments on commit c2be054

Please sign in to comment.