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:
parent
50b915d12a
commit
c606f874fb
@ -34,13 +34,13 @@ module Hledger.Data.Json (
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Aeson.Encode.Pretty (encodePrettyToTextBuilder)
|
import Data.Aeson.Encode.Pretty (Config(..), Indent(..), NumberFormat(..),
|
||||||
|
encodePretty', encodePrettyToTextBuilder')
|
||||||
--import Data.Aeson.TH
|
--import Data.Aeson.TH
|
||||||
import qualified Data.ByteString.Lazy as BL
|
import qualified Data.ByteString.Lazy as BL
|
||||||
import Data.Decimal (DecimalRaw(..), roundTo)
|
import Data.Decimal (DecimalRaw(..), roundTo)
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import qualified Data.Text.Lazy as TL
|
import qualified Data.Text.Lazy as TL
|
||||||
import qualified Data.Text.Lazy.IO as TL
|
|
||||||
import qualified Data.Text.Lazy.Builder as TB
|
import qualified Data.Text.Lazy.Builder as TB
|
||||||
import GHC.Generics (Generic)
|
import GHC.Generics (Generic)
|
||||||
import System.Time (ClockTime)
|
import System.Time (ClockTime)
|
||||||
@ -259,15 +259,18 @@ instance FromJSON (DecimalRaw Integer)
|
|||||||
|
|
||||||
-- Utilities
|
-- 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.
|
-- | Show a JSON-convertible haskell value as pretty-printed JSON text.
|
||||||
toJsonText :: ToJSON a => a -> TL.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.
|
-- | Write a JSON-convertible haskell value to a pretty-printed JSON file.
|
||||||
-- Eg: writeJsonFile "a.json" nulltransaction
|
-- Eg: writeJsonFile "a.json" nulltransaction
|
||||||
writeJsonFile :: ToJSON a => FilePath -> a -> IO ()
|
writeJsonFile :: ToJSON a => FilePath -> a -> IO ()
|
||||||
writeJsonFile f = TL.writeFile f . toJsonText
|
writeJsonFile f = BL.writeFile f . encodePretty' jsonConf
|
||||||
-- we write with Text and read with ByteString, is that fine ?
|
|
||||||
|
|
||||||
-- | Read a JSON file and decode it to the target type, or raise an error if we can't.
|
-- | 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
|
-- Eg: readJsonFile "a.json" :: IO Transaction
|
||||||
@ -280,4 +283,3 @@ readJsonFile f = do
|
|||||||
case fromJSON v :: FromJSON a => Result a of
|
case fromJSON v :: FromJSON a => Result a of
|
||||||
Error e -> error e
|
Error e -> error e
|
||||||
Success t -> return t
|
Success t -> return t
|
||||||
|
|
||||||
|
|||||||
@ -10,52 +10,52 @@ $ hledger -f- reg --output-format=json
|
|||||||
null,
|
null,
|
||||||
"",
|
"",
|
||||||
{
|
{
|
||||||
"pbalanceassertion": null,
|
"paccount": "a",
|
||||||
"pstatus": "Unmarked",
|
|
||||||
"pamount": [
|
"pamount": [
|
||||||
{
|
{
|
||||||
"aprice": null,
|
|
||||||
"acommodity": "AAA",
|
"acommodity": "AAA",
|
||||||
"aquantity": {
|
|
||||||
"floatingPoint": 1,
|
|
||||||
"decimalPlaces": 1,
|
|
||||||
"decimalMantissa": 10
|
|
||||||
},
|
|
||||||
"aismultiplier": false,
|
"aismultiplier": false,
|
||||||
|
"aprice": null,
|
||||||
|
"aquantity": {
|
||||||
|
"decimalMantissa": 10,
|
||||||
|
"decimalPlaces": 1,
|
||||||
|
"floatingPoint": 1
|
||||||
|
},
|
||||||
"astyle": {
|
"astyle": {
|
||||||
"ascommodityside": "R",
|
"ascommodityside": "R",
|
||||||
"asdigitgroups": null,
|
|
||||||
"ascommodityspaced": true,
|
"ascommodityspaced": true,
|
||||||
"asprecision": 1,
|
"asdecimalpoint": ".",
|
||||||
"asdecimalpoint": "."
|
"asdigitgroups": null,
|
||||||
|
"asprecision": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"ptransaction_": "1",
|
"pbalanceassertion": null,
|
||||||
"paccount": "a",
|
|
||||||
"pdate": null,
|
|
||||||
"ptype": "VirtualPosting",
|
|
||||||
"pcomment": "",
|
"pcomment": "",
|
||||||
|
"pdate": null,
|
||||||
"pdate2": null,
|
"pdate2": null,
|
||||||
|
"poriginal": null,
|
||||||
|
"pstatus": "Unmarked",
|
||||||
"ptags": [],
|
"ptags": [],
|
||||||
"poriginal": null
|
"ptransaction_": "1",
|
||||||
|
"ptype": "VirtualPosting"
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"aprice": null,
|
|
||||||
"acommodity": "AAA",
|
"acommodity": "AAA",
|
||||||
"aquantity": {
|
|
||||||
"floatingPoint": 1,
|
|
||||||
"decimalPlaces": 1,
|
|
||||||
"decimalMantissa": 10
|
|
||||||
},
|
|
||||||
"aismultiplier": false,
|
"aismultiplier": false,
|
||||||
|
"aprice": null,
|
||||||
|
"aquantity": {
|
||||||
|
"decimalMantissa": 10,
|
||||||
|
"decimalPlaces": 1,
|
||||||
|
"floatingPoint": 1
|
||||||
|
},
|
||||||
"astyle": {
|
"astyle": {
|
||||||
"ascommodityside": "R",
|
"ascommodityside": "R",
|
||||||
"asdigitgroups": null,
|
|
||||||
"ascommodityspaced": true,
|
"ascommodityspaced": true,
|
||||||
"asprecision": 1,
|
"asdecimalpoint": ".",
|
||||||
"asdecimalpoint": "."
|
"asdigitgroups": null,
|
||||||
|
"asprecision": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -72,20 +72,20 @@ $ hledger -f- bal --output-format=json
|
|||||||
0,
|
0,
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"aprice": null,
|
|
||||||
"acommodity": "AAA",
|
"acommodity": "AAA",
|
||||||
"aquantity": {
|
|
||||||
"floatingPoint": 1,
|
|
||||||
"decimalPlaces": 1,
|
|
||||||
"decimalMantissa": 10
|
|
||||||
},
|
|
||||||
"aismultiplier": false,
|
"aismultiplier": false,
|
||||||
|
"aprice": null,
|
||||||
|
"aquantity": {
|
||||||
|
"decimalMantissa": 10,
|
||||||
|
"decimalPlaces": 1,
|
||||||
|
"floatingPoint": 1
|
||||||
|
},
|
||||||
"astyle": {
|
"astyle": {
|
||||||
"ascommodityside": "R",
|
"ascommodityside": "R",
|
||||||
"asdigitgroups": null,
|
|
||||||
"ascommodityspaced": true,
|
"ascommodityspaced": true,
|
||||||
"asprecision": 1,
|
"asdecimalpoint": ".",
|
||||||
"asdecimalpoint": "."
|
"asdigitgroups": null,
|
||||||
|
"asprecision": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -93,20 +93,20 @@ $ hledger -f- bal --output-format=json
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"aprice": null,
|
|
||||||
"acommodity": "AAA",
|
"acommodity": "AAA",
|
||||||
"aquantity": {
|
|
||||||
"floatingPoint": 1,
|
|
||||||
"decimalPlaces": 1,
|
|
||||||
"decimalMantissa": 10
|
|
||||||
},
|
|
||||||
"aismultiplier": false,
|
"aismultiplier": false,
|
||||||
|
"aprice": null,
|
||||||
|
"aquantity": {
|
||||||
|
"decimalMantissa": 10,
|
||||||
|
"decimalPlaces": 1,
|
||||||
|
"floatingPoint": 1
|
||||||
|
},
|
||||||
"astyle": {
|
"astyle": {
|
||||||
"ascommodityside": "R",
|
"ascommodityside": "R",
|
||||||
"asdigitgroups": null,
|
|
||||||
"ascommodityspaced": true,
|
"ascommodityspaced": true,
|
||||||
"asprecision": 1,
|
"asdecimalpoint": ".",
|
||||||
"asdecimalpoint": "."
|
"asdigitgroups": null,
|
||||||
|
"asprecision": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user