Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ parseScientificText = scanScientific
(\sci rest -> if T.null rest then return sci else fail $ "Expecting end-of-input, got " ++ show (T.take 10 rest))
fail

parseBoundedScientificText :: Text -> Parser Scientific
parseBoundedScientificText t = parseScientificText t >>= rejectLargeExponent
where
rejectLargeExponent :: Scientific -> Parser Scientific
rejectLargeExponent s = withBoundedScientific' pure (Number s)
Comment thread
VictorCMiraldo marked this conversation as resolved.
Outdated

parseIntegralText :: Integral a => String -> Text -> Parser a
parseIntegralText name t =
prependContext name $
Expand Down Expand Up @@ -1730,6 +1736,10 @@ instance (FromJSON a, Integral a) => FromJSON (Ratio a) where
instance HasResolution a => FromJSON (Fixed a) where
parseJSON = prependContext "Fixed" . withBoundedScientific' (pure . realToFrac)

instance HasResolution a => FromJSONKey (Fixed a) where
fromJSONKey = FromJSONKeyTextParser $ \t ->
realToFrac <$> parseBoundedScientificText t

instance FromJSON Int where
parseJSON = parseBoundedIntegral "Int"

Expand Down
3 changes: 3 additions & 0 deletions tests/PropertyKeys.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module PropertyKeys ( keysTests ) where
import Prelude.Compat

import Control.Applicative (Const)
import Data.Fixed
Comment thread
VictorCMiraldo marked this conversation as resolved.
Outdated
import Data.Time.Compat (Day, LocalTime, TimeOfDay, UTCTime)
import Data.Time.Calendar.Compat (DayOfWeek)
import Data.Time.Calendar.Month.Compat (Month)
Expand Down Expand Up @@ -47,4 +48,6 @@ keysTests =
, testProperty "Lazy Text" $ roundTripKey @LT.Text
, testProperty "UUID" $ roundTripKey @UUID.UUID
, testProperty "Const Text" $ roundTripKey @(Const T.Text ())
, testProperty "Fixed E3" $ roundTripKey @(Fixed E3)
, testProperty "Fixed E6" $ roundTripKey @(Fixed E6)
]