diff --git a/README.md b/README.md index cc27510b..28c450e7 100644 --- a/README.md +++ b/README.md @@ -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) = "

" <> cs s <> "

" -- 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 diff --git a/example/greet.hs b/example/greet.hs index 3aca7b24..10019f90 100644 --- a/example/greet.hs +++ b/example/greet.hs @@ -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) = "

" <> cs s <> "

" +-- | 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