yesod-core-1.4.25: Creation of type-safe, RESTful web applications.

Safe HaskellNone
LanguageHaskell98

Yesod.Core.Json

Contents

Synopsis

Convert from a JSON value

defaultLayoutJson Source

Arguments

:: (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 Value Source

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 Value Source

Convert a value to a JSON representation via aeson's toJSON function.

Since: 1.2.1

returnJsonEncoding :: (Monad m, ToJSON a) => a -> m Encoding Source

Convert a value to a JSON representation via aeson's toEncoding function.

Since: 1.4.21

provideJson :: (Monad m, ToJSON a) => a -> Writer (Endo [ProvidedRep m]) () Source

Provide a JSON representation for usage with selectReps, using aeson's toJSON (aeson >= 0.11: toEncoding) 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 a Source

Deprecated: Use requireJsonBody instead

Same as parseJsonBody, but return an invalid args response on a parse error.

requireJsonBody :: (MonadHandler m, FromJSON a) => m a Source

Same as parseJsonBody, but return an invalid args response on a parse error.

Produce JSON values

data Value :: *

Constructors

Object !Object 
Array !Array 
String !Text 
Number !Scientific 
Bool !Bool 
Null 

class ToJSON a where

Minimal complete definition

Nothing

Methods

toJSON :: a -> Value

toEncoding :: a -> Encoding

class FromJSON a where

Minimal complete definition

Nothing

Methods

parseJSON :: Value -> Parser a

array :: ToJSON a => [a] -> Value Source

Convert a list of values to an Array.

object :: [Pair] -> Value

(.=) :: KeyValue kv => forall v. ToJSON v => Text -> v -> kv

(.:) :: FromJSON a => Object -> Text -> Parser a

Convenience functions

jsonOrRedirect Source

Arguments

:: (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:

  1. 200 with JSON data if the client prefers application/json (e.g. AJAX, see acceptsJSON).
  2. 3xx otherwise, following the PRG pattern.

jsonEncodingOrRedirect Source

Arguments

:: (MonadHandler m, ToJSON a) 
=> Route (HandlerSite m)

Redirect target

-> a

Data to send via JSON

-> m Encoding 

jsonEncodingOrRedirect simplifies the scenario where a POST handler sends a different response based on Accept headers:

  1. 200 with JSON data if the client prefers application/json (e.g. AJAX, see acceptsJSON).
  2. 3xx otherwise, following the PRG pattern. @since 1.4.21

acceptsJson :: MonadHandler m => m Bool Source

Returns True if the client prefers application/json as indicated by the Accept HTTP header.