Skip to content
Newer
Older
100644 36 lines (27 sloc) 957 Bytes
9e1f311 @bos Ensure that basic examples are buildable
authored May 27, 2015
1 {-# LANGUAGE OverloadedStrings, RecordWildCards #-}
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 7, 2011
2
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Nov 30, 2011
3 import Control.Applicative ((<$>), (<*>), empty)
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 6, 2011
4 import Data.Aeson
9e1f311 @bos Ensure that basic examples are buildable
authored May 27, 2015
5 import Data.Monoid
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
6 import qualified Data.ByteString.Lazy.Char8 as BL
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 6, 2011
7
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
8 data Coord = Coord { x :: Double, y :: Double }
9 deriving (Show)
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 6, 2011
10
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
11 -- A ToJSON instance allows us to encode a value as JSON.
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 6, 2011
12
13 instance ToJSON Coord where
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
14 toJSON (Coord xV yV) = object [ "x" .= xV,
15 "y" .= yV ]
16
b54d74b @bos Rename series to pairs
authored Jul 5, 2015
17 toEncoding Coord{..} = pairs $
9e1f311 @bos Ensure that basic examples are buildable
authored May 27, 2015
18 "x" .= x <>
19 "y" .= y
20
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
21 -- A FromJSON instance allows us to decode a value from JSON. This
22 -- should match the format used by the ToJSON instance.
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 6, 2011
23
24 instance FromJSON Coord where
25 parseJSON (Object v) = Coord <$>
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
26 v .: "x" <*>
27 v .: "y"
28 parseJSON _ = empty
1d27fc0 @nbogie Add simplest example of parsing and encoding.
nbogie authored Jun 6, 2011
29
4666c3b @bos Add a TH example.
authored Nov 30, 2011
30 main :: IO ()
eb3e4d3 @bos Start working on release notes and expanded examples.
authored Dec 1, 2011
31 main = do
32 let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord
33 print req
34 let reply = Coord 123.4 20
35 BL.putStrLn (encode reply)
Something went wrong with that request. Please try again.