Created
May 8, 2019 18:37
-
-
Save dmwit/7ef3dd25c4ee05f55352247f671beb10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data RawDebtRec = RawDebtRec | |
{ company :: Text | |
, debt :: Int | |
, phones :: [Int] | |
} | |
parseCompany o = o .: "company" | |
<|> o .: "company" >>= (.: "name") | |
parseInt o = parseJSON o <|> (read <$> parseJSON o) | |
parseDebt o = o .: "debt" >>= parseInt | |
parsePhone o = (o .: "phone" >>= parseInt >>= pure . pure) | |
<|> pure [] | |
parsePhoneList (Array vs) = toList <$> mapM parseInt vs | |
parsePhoneList (Number n) = pure (round n) | |
parsePhoneList _ = typeMismatch "whatevs" | |
parsePhones o = liftA2 (++) (parsePhone o) (o .: "phones" >>= parsePhoneList) | |
instance FromJSON RawDebtRec where | |
parseJSON o = pure RawDebtRec | |
<*> parseCompany o | |
<*> parseDebt o | |
<*> parsePhones o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment