Skip to content

Instantly share code, notes, and snippets.

@dmwit
Created May 8, 2019 18:37
Show Gist options
  • Save dmwit/7ef3dd25c4ee05f55352247f671beb10 to your computer and use it in GitHub Desktop.
Save dmwit/7ef3dd25c4ee05f55352247f671beb10 to your computer and use it in GitHub Desktop.
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