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

  1. {-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
  2. module JSONDeser where
  3. import Data.Maybe
  4. import Data.ByteString.Lazy
  5. import Data.Aeson
  6. import GHC.Generics
  7. data Cell = Cell { x :: Int, y :: Int}
  8. deriving (Show, Generic)
  9. data Unit = Unit { members :: [Cell], pivot :: Cell}
  10. deriving (Show, Generic)
  11. data Input = Input { id :: Int,
  12. units :: [Unit],
  13. width :: Int,
  14. height :: Int,
  15. filled :: [Cell],
  16. sourceLength :: Int,
  17. sourceSeeds :: [Int]
  18. }
  19. deriving (Show, Generic)
  20. instance FromJSON Cell
  21. instance ToJSON Cell
  22. instance FromJSON Unit
  23. instance ToJSON Unit
  24. instance FromJSON Input
  25. instance ToJSON Input
  26. readInputInternal :: ByteString -> Input
  27. readInputInternal str = if isJust result
  28. then fromJust result
  29. else error "Error during JSON parsing"
  30. where
  31. result = (decode str :: Maybe Input)