@@ -6,7 +6,6 @@
66{-# LANGUAGE TypeOperators #-}
77{-# OPTIONS_GHC -fno-warn-orphans #-}
88import Data.Aeson
9−import Data.Aeson.Encode.Pretty (encodePretty)
109import Data.Proxy
1110import Data.String.Conversions
1211import Data.Text (Text)
@@ -20,16 +19,14 @@ import Servant.Docs
2019newtype Greet = Greet Text
2120 deriving (Generic, Show)
2221
22+-- | We can get JSON support automatically. This will be used to parse
23+-- and encode a Greeting as 'JSON'.
2324instance FromJSON Greet
2425instance ToJSON Greet
2526
26−-- | A 'Greet' value can be rendered to 'JSON'.
27−instance MimeRender JSON Greet where
28− toByteString Proxy = encodePretty
29−
30−-- | A 'Greet' value can be rendered to 'HTML'.
31−instance MimeRender HTML Greet where
32− toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
27+-- | We can also implement 'MimeRender' for additional formats like 'PlainText'.
28+instance MimeRender PlainText Greet where
29+ toByteString Proxy (Greet s) = "\"" <> cs s <> "\""
3330
3431-- We add some useful annotations to our captures,
3532-- query parameters and request body to make the docs
@@ -76,8 +73,8 @@ intro2 = DocIntro "This title is below the last"
7673
7774-- API specification
7875type TestApi =
79− -- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
80− "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
76+ -- GET /hello/:name?capital={true, false} returns a Greet as JSON or PlainText
77+ "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, PlainText] Greet
8178
8279 -- POST /greet with a Greet as JSON in the request body,
8380 -- returns a Greet as JSON