Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE DeriveGeneric #-}
- module Integration.TestAeson where
- import Data.Aeson
- import GHC.Generics
- data Pets = Dogs [Dog] | Cats [Cat] | Rodents [Rodent] | Reptiles [Reptile]
- deriving (Show, Generic)
- data Dog = Dog {
- dogField :: String
- } deriving (Show, Generic)
- data Cat = Cat {
- catField :: String
- } deriving (Show, Generic)
- data Rodent = Rodent {
- rodentField :: String
- } deriving (Show, Generic)
- data Reptile = Reptile {
- reptileField :: String
- } deriving (Show, Generic)
- data Person = Person {
- name :: String,
- species :: String,
- pets :: Pets
- } deriving (Show,Generic)
- instance ToJSON Cat
- instance FromJSON Cat
- instance ToJSON Dog
- instance FromJSON Dog
- instance ToJSON Rodent
- instance FromJSON Rodent
- instance ToJSON Reptile
- instance FromJSON Reptile
- instance ToJSON Pets
- instance FromJSON Pets
- instance ToJSON Person
- instance FromJSON Person
- doit :: IO ()
- doit = do
- let agerbil = Rodent "Ager"
- let amouse = Rodent "Amose"
- let me = Person "user5402" "Human" (Rodents [agerbil, amouse])
- print $ encode me
- {- run this
- λ> doit
- "{\"species\":\"Human\",\"name\":\"user5402\",\"pets\":{\"tag\":\"Rodents\",\"contents\":[{\"rodentField\":\"Ager\"},{\"rodentField\":\"Amose\"}]}}"
- -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement