on 0.8.1.1 help sign in
typeable/aeson/examples/GenericSYB.hs
30 lines 1.0 kB history raw
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.
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.
8-- We enable the DeriveDataTypeable language extension so that GHC can
9-- automatically derive the Typeable and Data classes for us.
11{-# LANGUAGE DeriveDataTypeable #-}
13{-# LANGUAGE OverloadedStrings #-}
15import Data.Data (Typeable, Data)
16import Data.Aeson.Generic (decode, encode)
17import qualified Data.ByteString.Lazy.Char8 as BL
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.
22data Coord = Coord { x :: Double, y :: Double }
23 deriving (Show, Typeable, Data)
25main :: IO ()
26main = 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)