[haskell] Refactored JSON parsing code
This commit is contained in:
parent
0f279a06d8
commit
8249bf432a
4 changed files with 22 additions and 29 deletions
|
@ -9,7 +9,6 @@ module SpaceTraders.Model.Contract
|
|||
, Terms(..)
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.Aeson
|
||||
import Data.Time
|
||||
import GHC.Generics
|
||||
|
@ -25,15 +24,15 @@ data Contract = Contract { accepted :: Bool
|
|||
, terms :: Terms
|
||||
} deriving (Generic, Show)
|
||||
instance FromJSON Contract where
|
||||
parseJSON (Object o) = Contract <$> o .: "accepted"
|
||||
<*> o .: "id"
|
||||
<*> o .: "type"
|
||||
<*> o .: "expiration"
|
||||
<*> o .: "deadlineToAccept"
|
||||
<*> o .: "factionSymbol"
|
||||
<*> o .: "fulfilled"
|
||||
<*> o .: "terms"
|
||||
parseJSON _ = mzero
|
||||
parseJSON = withObject "Contract" $ \o ->
|
||||
Contract <$> o .: "accepted"
|
||||
<*> o .: "id"
|
||||
<*> o .: "type"
|
||||
<*> o .: "expiration"
|
||||
<*> o .: "deadlineToAccept"
|
||||
<*> o .: "factionSymbol"
|
||||
<*> o .: "fulfilled"
|
||||
<*> o .: "terms"
|
||||
instance ToJSON Contract where
|
||||
toEncoding (Contract a i ty e d fa fu te) = pairs ( "accepted" .= a
|
||||
<> "id" .= i
|
||||
|
|
|
@ -7,7 +7,6 @@ module SpaceTraders.Model.Route
|
|||
, RouteEndpoint(..)
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.Aeson
|
||||
import Data.Time
|
||||
import GHC.Generics
|
||||
|
@ -26,15 +25,15 @@ data RouteEndpoint = RouteEndpoint { routeEndpointType :: T.Text
|
|||
, y :: Int
|
||||
} deriving (Generic, Show)
|
||||
instance FromJSON RouteEndpoint where
|
||||
parseJSON (Object o) = RouteEndpoint <$> o .: "type"
|
||||
<*> o .: "symbol"
|
||||
<*> o .: "systemSymbol"
|
||||
<*> o .: "x"
|
||||
<*> o .: "y"
|
||||
parseJSON _ = mzero
|
||||
parseJSON = withObject "RouteEndpoint" $ \o ->
|
||||
RouteEndpoint <$> o .: "type"
|
||||
<*> o .: "symbol"
|
||||
<*> o .: "systemSymbol"
|
||||
<*> o .: "x"
|
||||
<*> o .: "y"
|
||||
instance ToJSON RouteEndpoint where
|
||||
toEncoding (RouteEndpoint t s ss xx yy) = pairs ( "type" .= t
|
||||
<> "symbol" .= s
|
||||
<> "systemSymbol" .= ss
|
||||
<> "x" .= xx
|
||||
<> "y" .= yy )
|
||||
<> "symbol" .= s
|
||||
<> "systemSymbol" .= ss
|
||||
<> "x" .= xx
|
||||
<> "y" .= yy )
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue