Safe Haskell | None |
---|
- defaultLayoutJson :: (Yesod site, ToJSON a) => WidgetT site IO () -> HandlerT site IO a -> HandlerT site IO TypedContent
- jsonToRepJson :: (Monad m, ToJSON a) => a -> m Value
- returnJson :: (Monad m, ToJSON a) => a -> m Value
- provideJson :: (Monad m, ToJSON a) => a -> Writer (Endo [ProvidedRep m]) ()
- parseJsonBody :: (MonadHandler m, FromJSON a) => m (Result a)
- parseJsonBody_ :: (MonadHandler m, FromJSON a) => m a
- requireJsonBody :: (MonadHandler m, FromJSON a) => m a
- data Value
- class ToJSON a where
- class FromJSON a where
- array :: ToJSON a => [a] -> Value
- object :: [Pair] -> Value
- (.=) :: ToJSON a => Text -> a -> Pair
- (.:) :: FromJSON a => Object -> Text -> Parser a
- jsonOrRedirect :: (MonadHandler m, ToJSON a) => Route (HandlerSite m) -> a -> m Value
- acceptsJson :: MonadHandler m => m Bool
Convert from a JSON value
:: (Yesod site, ToJSON a) | |
=> WidgetT site IO () | HTML |
-> HandlerT site IO a | JSON |
-> HandlerT site IO TypedContent |
Provide both an HTML and JSON representation for a piece of
data, using the default layout for the HTML output
(defaultLayout
).
Since: 0.3.0
jsonToRepJson :: (Monad m, ToJSON a) => a -> m ValueSource
Deprecated: Use returnJson instead
Wraps a data type in a RepJson
. The data type must
support conversion to JSON via ToJSON
.
Since: 0.3.0
returnJson :: (Monad m, ToJSON a) => a -> m ValueSource
Convert a value to a JSON representation via aeson's toJSON
function.
Since 1.2.1
provideJson :: (Monad m, ToJSON a) => a -> Writer (Endo [ProvidedRep m]) ()Source
Provide a JSON representation for usage with selectReps
, using aeson's
toJSON
function to perform the conversion.
Since 1.2.1
Convert to a JSON value
parseJsonBody :: (MonadHandler m, FromJSON a) => m (Result a)Source
Parse the request body to a data type as a JSON value. The
data type must support conversion from JSON via FromJSON
.
If you want the raw JSON value, just ask for a
.
Result
Value
Note that this function will consume the request body. As such, calling it twice will result in a parse error on the second call, since the request body will no longer be available.
Since: 0.3.0
parseJsonBody_ :: (MonadHandler m, FromJSON a) => m aSource
Deprecated: Use requireJsonBody instead
Same as parseJsonBody
, but return an invalid args response on a parse
error.
requireJsonBody :: (MonadHandler m, FromJSON a) => m aSource
Same as parseJsonBody
, but return an invalid args response on a parse
error.
Produce JSON values
data Value
A JSON value represented as a Haskell value.
class ToJSON a where
A type that can be converted to JSON.
An example type and instance:
{-# LANGUAGE OverloadedStrings #-} data Coord { x :: Double, y :: Double } instance ToJSON Coord where toJSON (Coord x y) =object
["x".=
x, "y".=
y]
Note the use of the OverloadedStrings
language extension which enables
Text
values to be written as string literals.
Instead of manually writing your ToJSON
instance, there are three options
to do it automatically:
- Data.Aeson.TH provides template-haskell functions which will derive an instance at compile-time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
toJSON
function that accepts any type which is an instance ofData
. - If your compiler has support for the
DeriveGeneric
andDefaultSignatures
language extensions (GHC 7.2 and newer),toJSON
will have a default generic implementation.
To use the latter option, simply add a deriving
clause to your
datatype and declare a Generic
ToJSON
instance for your datatype without giving a
definition for toJSON
.
For example the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics data Coord { x :: Double, y :: Double } deriving Generic instance ToJSON Coord
Note that, instead of using DefaultSignatures
, it's also possible
to parameterize the generic encoding using genericToJSON
applied
to your encoding/decoding Options
:
instance ToJSON Coord where toJSON =genericToJSON
defaultOptions
class FromJSON a where
A type that can be converted from JSON, with the possibility of failure.
When writing an instance, use empty
, mzero
, or fail
to make a
conversion fail, e.g. if an Object
is missing a required key, or
the value is of the wrong type.
An example type and instance:
{-# LANGUAGE OverloadedStrings #-} data Coord { x :: Double, y :: Double } instance FromJSON Coord where parseJSON (Object
v) = Coord<$>
v.:
"x"<*>
v.:
"y" -- A non-Object
value is of the wrong type, so usemzero
to fail. parseJSON _ =mzero
Note the use of the OverloadedStrings
language extension which enables
Text
values to be written as string literals.
Instead of manually writing your FromJSON
instance, there are three options
to do it automatically:
- Data.Aeson.TH provides template-haskell functions which will derive an instance at compile-time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
fromJSON
function that parses to any type which is an instance ofData
. - If your compiler has support for the
DeriveGeneric
andDefaultSignatures
language extensions,parseJSON
will have a default generic implementation.
To use this, simply add a deriving
clause to your datatype and
declare a Generic
FromJSON
instance for your datatype without giving a definition
for parseJSON
.
For example the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics data Coord { x :: Double, y :: Double } deriving Generic instance FromJSON Coord
Note that, instead of using DefaultSignatures
, it's also possible
to parameterize the generic decoding using genericParseJSON
applied
to your encoding/decoding Options
:
instance FromJSON Coord where parseJSON =genericParseJSON
defaultOptions
(.:) :: FromJSON a => Object -> Text -> Parser a
Retrieve the value associated with the given key of an Object
.
The result is empty
if the key is not present or the value cannot
be converted to the desired type.
This accessor is appropriate if the key and value must be present in an object for it to be valid. If the key and value are optional, use '(.:?)' instead.
Convenience functions
:: (MonadHandler m, ToJSON a) | |
=> Route (HandlerSite m) | Redirect target |
-> a | Data to send via JSON |
-> m Value |
jsonOrRedirect simplifies the scenario where a POST handler sends a different response based on Accept headers:
- 200 with JSON data if the client prefers
application/json
(e.g. AJAX, seeacceptsJSON
). - 3xx otherwise, following the PRG pattern.
acceptsJson :: MonadHandler m => m BoolSource
Returns True
if the client prefers application/json
as
indicated by the Accept
HTTP header.