21 lines
622 B
Haskell
21 lines
622 B
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module MiniJson.Greeting where
|
|
|
|
import Data.Aeson ((.:), FromJSON (parseJSON), eitherDecodeStrict', withObject)
|
|
import Data.ByteString (ByteString)
|
|
import Data.Text (Text)
|
|
import qualified Data.Text as Text
|
|
|
|
data GreetingRequest = GreetingRequest
|
|
{ name :: Text
|
|
}
|
|
|
|
instance FromJSON GreetingRequest where
|
|
parseJSON = withObject "GreetingRequest" (\object -> GreetingRequest <$> object .: "name")
|
|
|
|
greetFromJson :: ByteString -> Either String String
|
|
greetFromJson input = do
|
|
request <- eitherDecodeStrict' input
|
|
pure ("hello, " ++ Text.unpack (name request) ++ ", from aeson")
|