Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 31 lines (23 sloc) 0.858 kb
02ad3b4 @bos Merge Neill Bogie's changes
authored
1 {-# LANGUAGE OverloadedStrings #-}
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
2
a83da41 @bos Start working on release notes and expanded examples.
authored
3 import Control.Applicative ((<$>), (<*>), empty)
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
4 import Data.Aeson
a83da41 @bos Start working on release notes and expanded examples.
authored
5 import qualified Data.ByteString.Lazy.Char8 as BL
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
6
a83da41 @bos Start working on release notes and expanded examples.
authored
7 data Coord = Coord { x :: Double, y :: Double }
8 deriving (Show)
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
9
a83da41 @bos Start working on release notes and expanded examples.
authored
10 -- A ToJSON instance allows us to encode a value as JSON.
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
11
12 instance ToJSON Coord where
a83da41 @bos Start working on release notes and expanded examples.
authored
13 toJSON (Coord xV yV) = object [ "x" .= xV,
14 "y" .= yV ]
15
16 -- A FromJSON instance allows us to decode a value from JSON. This
17 -- should match the format used by the ToJSON instance.
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
18
19 instance FromJSON Coord where
20 parseJSON (Object v) = Coord <$>
a83da41 @bos Start working on release notes and expanded examples.
authored
21 v .: "x" <*>
22 v .: "y"
23 parseJSON _ = empty
efca226 @nbogie Add simplest example of parsing and encoding.
nbogie authored
24
323c18f @bos Add a TH example.
authored
25 main :: IO ()
a83da41 @bos Start working on release notes and expanded examples.
authored
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.