Skip to content
Newer
Older
100644 31 lines (22 sloc) 1005 Bytes
a83da41 @bos Start working on release notes and expanded examples.
authored Nov 30, 2011
1 -- This example is basically the same as in Simplest.hs, only it uses
2 -- SYB generics instead of explicit instances of ToJSON and FromJSON.
3
4 -- This mechanism is much slower than the newer generics mechanism
5 -- demonstrated in Generic.hs, but it works on versions of GHC older
6 -- than 7.2.
7
8 -- We enable the DeriveDataTypeable language extension so that GHC can
9 -- automatically derive the Typeable and Data classes for us.
10
11 {-# LANGUAGE DeriveDataTypeable #-}
12
13 {-# LANGUAGE OverloadedStrings #-}
14
15 import Data.Data (Typeable, Data)
16 import Data.Aeson.Generic (decode, encode)
17 import qualified Data.ByteString.Lazy.Char8 as BL
18
19 -- To decode or encode a value using the generic machinery, we must
20 -- make the type an instance of the Typeable and Data classes.
21
22 data Coord = Coord { x :: Double, y :: Double }
23 deriving (Show, Typeable, Data)
24
323c18f @bos Add a TH example.
authored Nov 30, 2011
25 main :: IO ()
a83da41 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
26 main = do
27 let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord
28 print req
29 let reply = Coord 123.4 20
30 BL.putStrLn (encode reply)
Something went wrong with that request. Please try again.