on master help sign in
Replace HTML with PlainText in examples
commit97ff49c3c4fcd395c902cff78bf2cc7e0d69f733 authorThomas Sutton <me@thomas-sutton.id.au> dateSun 22 Feb 2015, 19:42 (12 years ago) parentbdf61e4
2 files changed+12−18
README.md+5−8 example/greet.hs+7−10
README.md+5−8
@@ -27,16 +27,13 @@ import Servant
2727data Greet = Greet { _msg :: Text }
2828 deriving (Generic, Show)
2929
30−-- we get our JSON serialization for free
30+-- we get our JSON serialization for free. This will be used by the default
31+-- 'MimeRender' instance for 'JSON'.
3132instance FromJSON Greet
3233instance ToJSON Greet
3334
34−-- we can render a Greeting into JSON using this ToJSON instance
35−instance MimeRender JSON Greet where
36− toByteString Proxy = encodePretty
37−
38−-- or we can render it to HTML
39−instance MimeRender HTML Greet where
35+-- We can also implement 'MimeRender' explicitly for additional formats.
36+instance MimeRender PlainText Greet where
4037 toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
4138
4239-- we provide a sample value for the 'Greet' type
@@ -59,7 +56,7 @@ instance ToCapture (Capture "greetid" Text) where
5956
6057-- API specification
6158type TestApi =
62− "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
59+ "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,PlainText] Greet
6360 :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
6461 :<|> "delete" :> Capture "greetid" Text :> Delete
6562
example/greet.hs+7−10
@@ -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