Fail the parse instead of throwing an exception when hitting a negative Natural number
commit36738f1a60c24826c7803663527f649b86a28e28
authorBas van Dijk <v.dijk.bas@gmail.com>
dateWed 9 Dec 2015, 22:46 (11 years ago)
parent05249ea
Data/Aeson/Types/Instances.hs+4−1
@@ -359,7 +359,10 @@ instance ToJSON Natural where
359359 {-# INLINE toEncoding #-}
360360
361361instance FromJSON Natural where
362− parseJSON = withScientific "Integral" $ pure . truncate
362+ parseJSON = withScientific "Natural" $ \s ->
363+ if Scientific.coefficient s < 0
364+ then fail $ "Expected a Natural number but got the negative number: " <> show s
365+ else pure $ truncate s
363366#endif
364367
365368instance ToJSON Int8 where