Skip to content

Instantly share code, notes, and snippets.

@saevarb
Created January 29, 2014 19:44
Show Gist options
  • Save saevarb/8695476 to your computer and use it in GitHub Desktop.
Save saevarb/8695476 to your computer and use it in GitHub Desktop.
data RedditJsonReply a where
GoodJsonReply :: FromJSON a => a -> RedditJsonReply a
BadJsonReply :: FromJSON a => [Text] -> RedditJsonReply a
errors :: RedditJsonReply a -> [Text]
errors (BadJsonReply e) = e
errors _ = error "Used 'errors' on GoodJsonReply."
reply :: RedditJsonReply a -> a
reply (GoodJsonReply a) = a
reply _ = error "Used 'reply' on BadJsonReply."
instance FromJSON a => FromJSON (RedditJsonReply a) where
parseJSON (Object v) = do
e <- v .: "json" >>= (.: "errors")
case e of
[] -> v .: "json" >>= (.: "data") >>= parseJSON
e' -> return $ BadJsonReply e'
parseJSON _ = mzero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment