Replace HTML with PlainText in examples

This commit is contained in:
Thomas Sutton 2015-02-22 19:42:38 +11:00
parent bdf61e4df9
commit 97ff49c3c4
2 changed files with 12 additions and 18 deletions

View file

@ -27,16 +27,13 @@ import Servant
data Greet = Greet { _msg :: Text }
deriving (Generic, Show)
-- we get our JSON serialization for free
-- we get our JSON serialization for free. This will be used by the default
-- 'MimeRender' instance for 'JSON'.
instance FromJSON Greet
instance ToJSON Greet
-- we can render a Greeting into JSON using this ToJSON instance
instance MimeRender JSON Greet where
toByteString Proxy = encodePretty
-- or we can render it to HTML
instance MimeRender HTML Greet where
-- We can also implement 'MimeRender' explicitly for additional formats.
instance MimeRender PlainText Greet where
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
-- we provide a sample value for the 'Greet' type
@ -59,7 +56,7 @@ instance ToCapture (Capture "greetid" Text) where
-- API specification
type TestApi =
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,PlainText] Greet
:<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
:<|> "delete" :> Capture "greetid" Text :> Delete

View file

@ -6,7 +6,6 @@
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Data.Aeson
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.Proxy
import Data.String.Conversions
import Data.Text (Text)
@ -20,16 +19,14 @@ import Servant.Docs
newtype Greet = Greet Text
deriving (Generic, Show)
-- | We can get JSON support automatically. This will be used to parse
-- and encode a Greeting as 'JSON'.
instance FromJSON Greet
instance ToJSON Greet
-- | A 'Greet' value can be rendered to 'JSON'.
instance MimeRender JSON Greet where
toByteString Proxy = encodePretty
-- | A 'Greet' value can be rendered to 'HTML'.
instance MimeRender HTML Greet where
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
-- | We can also implement 'MimeRender' for additional formats like 'PlainText'.
instance MimeRender PlainText Greet where
toByteString Proxy (Greet s) = "\"" <> cs s <> "\""
-- We add some useful annotations to our captures,
-- query parameters and request body to make the docs
@ -76,8 +73,8 @@ intro2 = DocIntro "This title is below the last"
-- API specification
type TestApi =
-- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
-- GET /hello/:name?capital={true, false} returns a Greet as JSON or PlainText
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, PlainText] Greet
-- POST /greet with a Greet as JSON in the request body,
-- returns a Greet as JSON