summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Model
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-10 00:31:13 +0200
committerJulien Dessaux2023-07-10 00:31:13 +0200
commit8249bf432abcf4aad261a79ac2d802c0ac3a0391 (patch)
treed9d89135dcefe6c388b8fa7789288bc431634ecf /haskell/src/SpaceTraders/Model
parent[haskell] Simplify API Error JSON decoding (diff)
downloadspacetraders-8249bf432abcf4aad261a79ac2d802c0ac3a0391.tar.gz
spacetraders-8249bf432abcf4aad261a79ac2d802c0ac3a0391.tar.bz2
spacetraders-8249bf432abcf4aad261a79ac2d802c0ac3a0391.zip
[haskell] Refactored JSON parsing code
Diffstat (limited to '')
-rw-r--r--haskell/src/SpaceTraders/Model/Contract.hs19
-rw-r--r--haskell/src/SpaceTraders/Model/Route.hs21
2 files changed, 19 insertions, 21 deletions
diff --git a/haskell/src/SpaceTraders/Model/Contract.hs b/haskell/src/SpaceTraders/Model/Contract.hs
index fd7a70b..742062e 100644
--- a/haskell/src/SpaceTraders/Model/Contract.hs
+++ b/haskell/src/SpaceTraders/Model/Contract.hs
@@ -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
diff --git a/haskell/src/SpaceTraders/Model/Route.hs b/haskell/src/SpaceTraders/Model/Route.hs
index 959edff..9681214 100644
--- a/haskell/src/SpaceTraders/Model/Route.hs
+++ b/haskell/src/SpaceTraders/Model/Route.hs
@@ -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 )