Skip to content

Add a FromJSONKey instance for Data.Fixed#1165

Open
VictorCMiraldo wants to merge 2 commits intohaskell:masterfrom
VictorCMiraldo:vcm/add-fromjsonkey-fixed
Open

Add a FromJSONKey instance for Data.Fixed#1165
VictorCMiraldo wants to merge 2 commits intohaskell:masterfrom
VictorCMiraldo:vcm/add-fromjsonkey-fixed

Conversation

@VictorCMiraldo
Copy link
Copy Markdown

@VictorCMiraldo VictorCMiraldo commented Apr 10, 2026

We have a ToJSONKey (Fixed a) instance:

instance HasResolution a => ToJSONKey (Fixed a) where
toJSONKey = toJSONKeyTextEnc (E.scientificText . realToFrac)

But there's no corresponding FromJSONKey (Fixed a) available. Because we have a FromJSONKey Double, it seems like this was just never added and never needed. I now have some Maps with fixed-point numbers as their keys and therefore would benefit from a FromJSONKey (Fixed a) instance. A lot of the scientific parsing functionality is internal to aeson and not exported, so I can't just add it the orphan instance on my project and call it a day, plus, if I can fix it for everyone, than it's better for everyone! :)

I tried reusing the Scientific parsing as much as possible and checked it does indeed reject large exponents in a cabal repl:

ghci> x = Object (KM.fromList [("4",String "a"),("5.32e4300",String "b")])
ghci> fromJSON x :: Result (M.Map (Fixed E12) String)
Error "found a number with exponent 4298, but it must not be greater than 1024"

Comment thread src/Data/Aeson/Types/FromJSON.hs Outdated
parseBoundedScientificText t = parseScientificText t >>= rejectLargeExponent
where
rejectLargeExponent :: Scientific -> Parser Scientific
rejectLargeExponent s = withBoundedScientific' pure (Number s)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates too much of parseIntegralText; I don't like this duplication.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable! I refactored parseIntegralText; how does it look now?

Comment thread tests/PropertyKeys.hs Outdated
import Prelude.Compat

import Control.Applicative (Const)
import Data.Fixed
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wildcard import. Bad.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Respectfully disagree. Happy to oblige. :)

@VictorCMiraldo VictorCMiraldo requested a review from phadej April 14, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants