diff --git a/src/Hanafuda/Message.hs b/src/Hanafuda/Message.hs index 53d369b..2f13b60 100644 --- a/src/Hanafuda/Message.hs +++ b/src/Hanafuda/Message.hs @@ -37,10 +37,13 @@ import qualified Hanafuda.KoiKoi as KoiKoi ( , Score, Scores, Source(..), Step(..), Yaku(..) ) import Hanafuda.Player (Player(..), Players(..)) +import Text.Read (readMaybe) instance IDType a => FromJSON (ID a) where - parseJSON = withText decoding (return . read . Text.unpack) - where decoding = let Prefix p = (prefix :: Prefix a) in p ++ "ID" + parseJSON = withText decoding (safeRead . Text.unpack) + where + decoding = let Prefix p = (prefix :: Prefix a) in p ++ "ID" + safeRead s = maybe (fail $ "Not an ID: '" ++ s ++ "'") return $ readMaybe s instance IDType a => FromJSONKey (ID a) where fromJSONKey = FromJSONKeyText (read . Text.unpack) instance IDType a => ToJSON (ID a) where