Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
Created November 14, 2012 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanHowell/964ec113215ff3f49dfb to your computer and use it in GitHub Desktop.
Save NathanHowell/964ec113215ff3f49dfb to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-} -- for JTask and Fields ToJSON instances:w!
{-# LANGUAGE FlexibleInstances #-} -- for the HashMap ToJSON instances
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
import Prelude
import Data.ByteString
import GHC.Generics (Generic )
import Data.Aeson
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Hashable
import Data.ByteString.Char8 ()
data JTask = JTask {fields :: Fields} deriving (Generic)
data Fields = Fields { project :: HashMap Key Project
, summary :: ByteString
, issuetype :: HashMap Name Task
, versions :: [HashMap Name Version]
, description :: ByteString
} deriving (Generic)
data Key = Key deriving (Generic)
instance Hashable Key where
hash _ = 0
instance Show Key where
show Key = "key"
data Name = Name deriving (Generic)
instance Hashable Name where
hash _ = 0
instance Show Name where
show Name = "name"
data Task = Task deriving (Generic)
instance Hashable Task where
hash _ = 0
type Version = ByteString -- Placeholder type. Probably using Day for realsies.
data Project = BNAP deriving (Generic) -- Fill this out as we go
instance ToJSON Fields
instance ToJSON JTask
instance ToJSON Key
instance ToJSON Name
instance ToJSON Task
instance ToJSON Project
mapfst :: (a -> b) -> [(a, v)] -> [(b, v)]
mapfst f = fmap $ \ (k, v) -> (f k, v)
instance ToJSON a => ToJSON (HashMap Key a) where
toJSON = toJSON . HashMap.fromList . mapfst show . HashMap.toList
instance ToJSON a => ToJSON (HashMap Name a) where
toJSON = toJSON . HashMap.fromList . mapfst show . HashMap.toList
createObject :: Value
createObject =
toJSON
. JTask
$ Fields { project = HashMap.singleton Key BNAP
, summary = "summary"
, issuetype = HashMap.singleton Name Task
, versions = [HashMap.singleton Name ("version")]
, description = "description"
}
*Main Data.ByteString.Lazy> Data.ByteString.Lazy.putStrLn (encode createObject)
{"fields":{"project":{"key":[]},"description":"description","issuetype":{"name":[]},"summary":"summary","versions":[{"name":"version"}]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment