aeson-pretty icon indicating copy to clipboard operation
aeson-pretty copied to clipboard

Aeson.toEncoding method implementations not respected

Open TGOlson opened this issue 7 years ago • 2 comments

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.

TGOlson avatar Jun 10 '17 15:06 TGOlson

On top of that, the documentation is misleading:

encodePretty :: ToJSON a => a -> ByteString A drop-in replacement for aeson's encode function

One would then expect that it uses the same function as encode, which is toEncoding, whereas encodePretty uses toJSON. The name *encode*Pretty is misleading too.

giulioforesto avatar Feb 14 '22 10:02 giulioforesto

Agreed, I'm open to PRs!

martijnbastiaan avatar Feb 14 '22 10:02 martijnbastiaan