From c2be054860a876f2fd3499231ba034d1334da8d0 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Wed, 21 Jul 2021 12:30:01 +1000 Subject: [PATCH] imp: json: The keys of JSON objects are now displayed in alphabetical order, making it stable across different systems and compilers. --- hledger-lib/Hledger/Data/Json.hs | 6 ++- hledger/test/json.test | 70 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/hledger-lib/Hledger/Data/Json.hs b/hledger-lib/Hledger/Data/Json.hs index e89a5b5b15f2..04d5c896366e 100644 --- a/hledger-lib/Hledger/Data/Json.hs +++ b/hledger-lib/Hledger/Data/Json.hs @@ -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) @@ -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 diff --git a/hledger/test/json.test b/hledger/test/json.test index 1a970981fb0d..75b77aa0c3c0 100644 --- a/hledger/test/json.test +++ b/hledger/test/json.test @@ -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 } } ] @@ -72,20 +72,20 @@ $ 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 } } ] @@ -93,20 +93,20 @@ $ hledger -f- bal --output-format=json ], [ { - "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 } } ]