You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1 KiB

{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
module JSONDeser where
import Data.Maybe
import Data.ByteString.Lazy
import Data.Aeson
import GHC.Generics
data Cell = Cell { x :: Int, y :: Int}
deriving (Show, Generic)
data Unit = Unit { members :: [Cell], pivot :: Cell}
deriving (Show, Generic)
data Input = Input { id :: Int,
units :: [Unit],
width :: Int,
height :: Int,
filled :: [Cell],
sourceLength :: Int,
sourceSeeds :: [Int]
}
deriving (Show, Generic)
instance FromJSON Cell
instance ToJSON Cell
instance FromJSON Unit
instance ToJSON Unit
instance FromJSON Input
instance ToJSON Input
readInputInternal :: ByteString -> Input
readInputInternal str = if isJust result
then fromJust result
else error "Error during JSON parsing"
where
result = (decode str :: Maybe Input)