Use TemplateHaskell for JSON instances

This commit is contained in:
Rene Vergara 2024-08-06 16:08:51 -05:00
parent 675ca9d5e3
commit 339c93905f
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2

View file

@ -10,9 +10,11 @@
module Zenith.Types where
import Data.Aeson
import Data.Aeson.TH (deriveJSON)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Char8 as C
import Data.Char (toLower)
import Data.HexString
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
@ -110,25 +112,7 @@ data ZcashWalletAPI = ZcashWalletAPI
, zw_lastSync :: !Int
} deriving (Eq, Prelude.Show)
instance ToJSON ZcashWalletAPI where
toJSON (ZcashWalletAPI i n net b l) =
object
[ "index" .= i
, "name" .= n
, "network" .= net
, "birthday" .= b
, "lastSync" .= l
]
instance FromJSON ZcashWalletAPI where
parseJSON =
withObject "ZcashWalletAPI" $ \obj -> do
i <- obj .: "index"
n <- obj .: "name"
net <- obj .: "network"
b <- obj .: "birthday"
l <- obj .: "lastSync"
pure $ ZcashWalletAPI i n net b l
$(deriveJSON defaultOptions {fieldLabelModifier = drop 3} ''ZcashWalletAPI)
data ZcashAccountAPI = ZcashAccountAPI
{ za_index :: !Int
@ -136,17 +120,18 @@ data ZcashAccountAPI = ZcashAccountAPI
, za_name :: !T.Text
} deriving (Eq, Prelude.Show)
instance ToJSON ZcashAccountAPI where
toJSON (ZcashAccountAPI i w n) =
object ["index" .= i, "wallet" .= w, "name" .= n]
$(deriveJSON defaultOptions {fieldLabelModifier = drop 3} ''ZcashAccountAPI)
instance FromJSON ZcashAccountAPI where
parseJSON =
withObject "ZcashAccountAPI" $ \obj -> do
i <- obj .: "index"
w <- obj .: "wallet"
n <- obj .: "name"
pure $ ZcashAccountAPI i w n
data ZcashAddressAPI = ZcashAddressAPI
{ zd_index :: !Int
, zd_account :: !Int
, zd_name :: !T.Text
, zd_ua :: !T.Text
, zd_legacy :: !(Maybe T.Text)
, zd_transparent :: !(Maybe T.Text)
} deriving (Eq, Prelude.Show)
$(deriveJSON defaultOptions {fieldLabelModifier = drop 3} ''ZcashAddressAPI)
-- ** `zebrad`
-- | Type for modeling the tree state response