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

Yesod.Json

Contents

Description

Efficient generation of JSON documents, with HTML-entity encoding handled via types.

Synopsis

Monad

data Json Source

A monad for generating Json output. In truth, it is just a newtype wrapper around Html; we thereby get the benefits of BlazeHtml (type safety and speed) without accidently mixing non-JSON content.

This is an opaque type to avoid any possible insertion of non-JSON content. Due to the limited nature of the JSON format, you can create any valid JSON document you wish using only jsonScalar, jsonList and jsonMap.

Instances

jsonToContent :: Json -> GHandler sub master ContentSource

Extract the final result from the given Json value.

See also: applyLayoutJson in Yesod.Yesod.

jsonToRepJson :: Json -> GHandler sub master RepJsonSource

Wraps the Content generated by jsonToContent in a RepJson.

Generate Json output

jsonScalar :: Html () -> JsonSource

Outputs a single scalar. This function essentially:

  • Performs HTML entity escaping as necesary.
  • Performs JSON encoding.
  • Wraps the resulting string in quotes.

jsonList :: [Json] -> JsonSource

Outputs a JSON list, eg ["foo","bar","baz"].

jsonMap :: [(String, Json)] -> JsonSource

Outputs a JSON map, eg {"foo":"bar","baz":"bin"}.

jsonRaw :: ByteString -> JsonSource

Outputs raw JSON data without performing any escaping. Use with caution: this is the only function in this module that allows you to create broken JSON documents.