haskell / haskell/aeson

Inconsistent encoding of Double depending on container structure

Open
#660 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
1.3k
Forks
336
Avg merge
3d 7h
Merged PRs (30d)
4

Description

This is closely related to #546. Initially I thought there was some oddness specific to derived instances, but [this comment](https://github.com/bos/aeson/issues/546#issuecomment-299991780) pointed me toward a nice trivial example of the underlying cause:
```haskell
λ. import Data.Aeson
λ. encode 0.0
"0.0"
λ. encode $ toJSON 0.0
"0"
```
And its consequence:
```haskell
λ. :set -XDeriveGeneric -XDeriveAnyClass -XDerivingStrategies -XGeneralizedNewtypeDeriving
λ. newtype Foo = Foo Double deriving Generic
λ. deriving newtype instance ToJSON Foo
λ. encode $ Foo 0.0
"0.0"
λ. newtype Bar = Bar Double deriving Generic
λ. deriving anyclass instance ToJSON Bar
λ. encode $ Bar 0.0
"0"
λ. data Baz = Baz Double deriving (Generic, ToJSON)
λ. encode $ Baz 0.0
"0"
λ. data Qux = Qux Foo deriving (Generic, ToJSON)
λ. encode $ Qux (Foo 0.0)
"0"
```
That last one is especially irksome: checking the newtype encoding doesn't reflect what the full structure will look like. (And of course, that's exactly the context where I ran into this.)

This issue is specifically about the _inconsistency_. It is surprising and undesirable that `Double`s render differently depending on context. Which way they _should_ be rendered probably belongs to #546. However, I should add that at present it's not a clear-cut matter of `deriving newtype` being the odd one out:
```haskell
λ. encode (0.0, "whoops")
"[0.0,\"whoops\"]"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.