Created
October 11, 2017 05:17
-
-
Save khanage/d98a1dbbdf787dc35123af60a578d9be to your computer and use it in GitHub Desktop.
This file contains 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
module Main where | |
import Data.Aeson | |
import Data.Aeson.Types | |
import Control.Applicative (Alternative(..)) | |
data State = State { stateName :: String, stateNum :: Int } deriving (Eq, Show) | |
parseState :: Value -> Parser State | |
parseState = withObject "State" $ \v -> State <$> v .: "name" <*> v .: "id" | |
parseProvince :: Value -> Parser State | |
parseProvince = withObject "State" $ \v -> State <$> v .: "provinceName" <*> pure 20 | |
instance FromJSON State where | |
parseJSON v = | |
parseState v <|> parseProvince v | |
main :: IO () | |
main = do | |
let state = "{\"name\": \"WA\", \"id\": 1}" | |
province = "{\"provinceName\": \"NY\"}" | |
print (decode state :: Maybe State) | |
print (decode province :: Maybe State) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment