Skip to content
This repository
Newer
Older
100644 29 lines (22 sloc) 0.888 kb
323c18f9 »
2011-11-30 Add a TH example.
1 -- We can use Template Haskell (TH) to generate instances of the
2 -- FromJSON and ToJSON classes automatically. This is the fastest way
3 -- to add JSON support for a type.
4
5 {-# LANGUAGE TemplateHaskell #-}
6
7 {-# LANGUAGE OverloadedStrings #-}
8
9 import Data.Aeson (decode, encode)
19bec1d5 » ret
2014-04-28 Modernizing the TH example.
10 import Data.Aeson.TH (deriveJSON, defaultOptions)
323c18f9 »
2011-11-30 Add a TH example.
11 import qualified Data.ByteString.Lazy.Char8 as BL
12
13 data Coord = Coord { x :: Double, y :: Double }
14 deriving (Show)
15
16 -- This splice will derive instances of ToJSON and FromJSON for us.
17 --
18 -- The use of "id" below is a placeholder function to transform the
19 -- names of the type's fields. We don't want to transform them, so we
20 -- use the identity function.
21
19bec1d5 » ret
2014-04-28 Modernizing the TH example.
22 $(deriveJSON defaultOptions ''Coord)
323c18f9 »
2011-11-30 Add a TH example.
23
24 main :: IO ()
25 main = do
26 let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord
27 print req
28 let reply = Coord 123.4 20
29 BL.putStrLn (encode reply)
Something went wrong with that request. Please try again.