{-# 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)