servant-ede-0.5.1: Combinators for rendering EDE templates in servant web applications

Safe HaskellNone
LanguageHaskell2010

Servant.EDE.Internal.ToObject

Synopsis

Documentation

class ToObject a where Source

Turn haskell values into JSON objects.

This is the mechanism used by EDE to marshall data from Haskell to the templates. The rendering is then just about feeding the resulting Object to a compiled Template. Example:

import Text.EDE

data User = User { name :: String, age :: Int }

instance ToObject User where
  toObject user =
    fromPairs [ "name" .= name user
              , "age"  .= age user
              ]

However, you're not forced to write the instance yourself for such a type. Indeed, for any record type (i.e a datatype with a single constructor and with field selectors) you can let GHC.Generics derive the ToObject instance for you.

data User = User { name :: String, age :: Int } deriving Generic
instance ToObject User

This will generate an equivalent instance to the previous one.

Minimal complete definition

Nothing

Methods

toObject :: a -> Object Source

Turn values of type a into JSON Objects.

-- Reminder:
type Object = HashMap Text Value

Instances

class GToObject f where Source

Methods

gtoObject :: f a -> Object Source

Instances

GToObject V1 
GToObject U1 
(GToObject f, GToObject g) => GToObject ((:*:) f g) 
GToObject a => GToObject (M1 D d a) 
GToObject a => GToObject (M1 C c a) 
(Selector s, ToJSON a) => GToObject (M1 S s (K1 r a))