forked from haskell/aeson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PossibleGen.hs
54 lines (41 loc) · 1.6 KB
/
PossibleGen.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- module Possible where
-- This example is basically the same as in Simplest.hs, only it uses
-- GHC's builtin generics instead of explicit instances of ToJSON and
-- FromJSON.
-- We enable the DeriveGeneric language extension so that GHC can
-- automatically derive the Generic class for us.
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Data.Aeson.TH (deriveJSON, defaultOptions)
import GHC.Generics (Generic)
import Data.Possible
-- (FromJSON, ToJSON, decode, encode)
import qualified Data.ByteString.Lazy.Char8 as BL
-- import Data.Map
-- To decode or encode a value using the generic machinery, we must
-- make the type an instance of the Generic class.
data Coord = Coord { x :: Double, y :: Double
, z :: Possible Double}
deriving (Show, Generic)
-- While we still have to declare our type as instances of FromJSON
-- and ToJSON, we do *not* need to provide bodies for the instances.
-- Default versions will be supplied for us.
-- $(deriveJSON defaultOptions ''Coord)
instance FromJSON Coord
instance ToJSON Coord
blen :: ToJSON a => a -> IO ()
blen a = do
BL.putStrLn . encode $ a
print $ toJSON a
pde = print . (\a -> decode a :: Maybe Coord)
main :: IO ()
main = do
pde "{\"x\":3.0,\"y\":-1.0,\"z\":1}"
pde "{\"x\":3.0,\"y\":-1.0,\"z\":null}"
pde "{\"x\":3.0,\"y\":-1.0,\"d\":3.5e2}"
blen $ Coord 3 (-1) (HaveData 1)
blen $ Coord 3 (-2) HaveNull
blen $ Coord 3 (-1) MissingData
blen $ object ["1" .= Missing, "2" .= Null ]
blen $ object ["a" .= Null , "b" .= Missing, "c" .= Number 1, "d" .= RawNumber "3.4e4" ]