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