informatikr / aeson-pretty Goto Github PK
View Code? Open in Web Editor NEWJSON pretty-printing library and command-line tool.
Home Page: http://hackage.haskell.org/package/aeson-pretty
License: BSD 3-Clause "New" or "Revised" License
JSON pretty-printing library and command-line tool.
Home Page: http://hackage.haskell.org/package/aeson-pretty
License: BSD 3-Clause "New" or "Revised" License
I have been looking for a Value
prettyprinter that produces a prettyprinter
Doc
. aeson-pretty
doesn't, but perhaps it's something it could do?
data Item =
Item
{ code:: String
, thumbUrl :: Maybe T.Text
}
deriving (Generic, Show, Eq, Ord)
instance ToJSON Item where
toEncoding = genericToEncoding defaultOptions
{ omitNothingFields = True }
instance Item Game where
parseJSON = genericParseJSON defaultOptions { omitNothingFields = True}
encodePretty [ Item {code= "foo", thumbUrl = Nothing}]
Expected output:
"[\n {\n "code": "foo"\n }\n]"
Actual output:
"[\n {\n "thumbUrl": null,\n "code": "foo"\n }\n]"
It would be nice to be able to encode JSON with sorted keys, but still minified without newlines or indentation.
$ cabal repl --build-depends aeson-pretty Sat 21:12
Resolving dependencies...
Build profile: -w ghc-8.10.7 -O1
In order, the following will be built (use -v for more details):
- aeson-pretty-0.8.8 (lib) (requires build)
- fake-package-0 (lib) (first run)
Starting aeson-pretty-0.8.8 (lib)
Building aeson-pretty-0.8.8 (lib)
Failed to build aeson-pretty-0.8.8.
Build log (
/home/hasufell/.cabal/logs/ghc-8.10.7/aeson-pretty-0.8.8-7d5e0bebeaf77dbf14fbe263246433acbce057be5e13a95f8a842096920829bb.log
):
Configuring library for aeson-pretty-0.8.8..
Preprocessing library for aeson-pretty-0.8.8..
Building library for aeson-pretty-0.8.8..
[1 of 1] Compiling Data.Aeson.Encode.Pretty ( Data/Aeson/Encode/Pretty.hs, dist/build/Data/Aeson/Encode/Pretty.o, dist/build/Data/Aeson/Encode/Pretty.dyn_o )
Data/Aeson/Encode/Pretty.hs:58:1: error:
Could not load module ‘Data.Aeson.Encode’
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
It is a member of the hidden package ‘aeson-1.5.6.0’.
Perhaps you need to add ‘aeson’ to the build-depends in your .cabal file.
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
58 | import qualified Data.Aeson.Encode as Aeson
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cabal: Failed to build aeson-pretty-0.8.8 (which is required by
fake-package-0). See the build log above for details.
Hello. I just installed your library via cabal install aeson-pretty
. Then I tried to use it and found strange signature:
encodePretty :: aeson-0.7.0.0:Data.Aeson.Types.Class.ToJSON a => a -> Data.ByteString.Lazy.Internal.ByteString
Why the type constraint is aeson-0.7.0.0:Data.Aeson.Types.Class.ToJSON a
and not ToJSON a
? Have I installed it wrong?
It doesn't appear that encodePretty
or any of the other variants of that function respect custom encoding options defined in toEncoding
.
Small example:
data Person = Person
{ _name :: Text
, _age :: Int
} deriving (Generic, Show)
instance ToJSON Blockchain where
toEncoding = genericToEncoding $ defaultOptions { fieldLabelModifier = stripUnderscorePrefix }
where stripUnderscorePrefix = ...
Using encode
on this data type, I correctly get {"name": ..., "age": ...}
. But using encodePretty
instead gives me {"_name": ..., "_age": ...}
.
I think this is because the underlying encodePrettyToTextBuilder'
calls toJSON
directly, which does not apply the encoding options. If possible, it'd be great to preserve the options when using encodePretty
.
It would be nice if you could expose a lazy text builder a la
Data.Aeson.Encode.encodeToTextBuilder
but with pretty-printing support. Something like
encodeToTextBuilder :: Config -> Value -> Builder
encodeToTextBuilder Config{..} = toLazyText . fromValue st
where
st = PState confIndent 0 condSort
condSort = sortBy (confCompare `on` fst)
It would be cool if there was a config flag to switch on syntax highlighting
Right now the keys are always resorted when using encodePretty
.
However, sometimes it is desired to preserve the key order from toEncoding
.
See GetShopTV/swagger2#98 for a possible use case.
I have cabal version 1.22.6.0 and I installed aeson-pretty-0.7.2
=>cabal install aeson-pretty
Resolving dependencies...
All the requested packages are already installed:
aeson-pretty-0.7.2
but when I run aeson-pretty I get zsh: command not found: aeson-pretty
I tried to follow the instructions for installation but I couldn't run it anyway from the terminal
My OSX version: 10.11.4
Please add a CHANGELOG.md
file and add it to extra-source-files
in the .cabal
file.
I think v0.7.2 and up should be revised to prevent building with aeson >= 2.
Doesn't seem to break anything, even fail
is now from Control.Monad.Fail
.
Would you please revert #19 . The reason - scientific package has updated, now it formats everything like it was proposed:
Generic ->
doFmt (if e < 0 || e > 7 then Exponent else Fixed)
(is,e)
Aeson 1.2.1, instead, does not check for large numbers (with exponent higher than 7):
fromScientific :: Scientific -> Builder
fromScientific s = formatScientificBuilder format prec s
where
(format, prec)
| base10Exponent s < 0 = (Generic, Nothing)
| otherwise = (Fixed, Just 0)
So, currently we have output:
"intBillion" : 1000000000
If you do not want to output trailing .0
, you need to make a special formatter:
prettyFormatter :: S.Scientific -> Builder
prettyFormatter n
| S.isFloating n == True = formatScientificBuilder S.Generic Nothing n
| otherwise =formatScientificBuilder S.Generic (Just 0) n -- output no digits after decimal point
This will output
"double0" : 0,
"doubleHalf" : 0.5,
"intTwo" : 2,
"intMillion" : 1000000
"intBillion" : 1.0e9
There is a Debian bug report about the behavior of aeson-pretty -s.
The problem is that the json' parser is returning the keys to aeson-pretty already sorted.
This line uses mapKeys
which has only existed since unordered-containers-0.2.14.0
. Can you update the version bounds (and ideally those of the most recently released Hackage version) to reflect this?
For using "\t"
instead of " "
. Default False
.
instead of
{
"name": "John Doe",
"country": "Germany",
"favorite_color": "green"
}
format it as
name: John Doe
country: Germany
favorite_color: green
Apparently, such a feature is available in the Data.Aeson.TH
module (see omitNothingFields
), but that module requires that TemplateHaskell
be enabled.
Feature request: I'd like to be able to encode JSON with spaces but without line breaks.
For example, let x = Array (fromList [Number 1, Number 2])
.
encode x
produces "[1,2]"
encodePretty x
produces "[\n 1,\n 2\n]"
"[1, 2]"
A viable use case for pretty printing is outputting a JSON value to console or a file, and in both cases a trailing newline is desirable.
Installing aeson-pretty-0.7.1
together with aeson-0.7.0.6
gives this compilation error:
In the use of ‘Aeson.fromValue’ (imported from Data.Aeson.Encode):
Deprecated: "Use 'encodeToTextBuilder' instead"
In-place registering aeson-pretty-0.7.1...
Preprocessing executable 'aeson-pretty' for aeson-pretty-0.7.1...
[1 of 2] Compiling Paths_aeson_pretty ( dist/dist-sandbox-75adb5f/build/autogen/Paths_aeson_pretty.hs, dist/dist-sandbox-75adb5f/build/aeson-pretty/aeson-pretty-tmp/Paths_aeson_pretty.o )
[2 of 2] Compiling Main ( cli-tool/Main.hs, dist/dist-sandbox-75adb5f/build/aeson-pretty/aeson-pretty-tmp/Main.o )
Failed to load interface for ‘Data.Aeson.Encode.ByteString’
There are files missing in the ‘aeson-0.7.0.6’ package,
"Spaces" appears to be missing
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.