Created
March 9, 2017 22:33
-
-
Save purpleP/ce84b570b654b00f383dca364f0320cd to your computer and use it in GitHub Desktop.
attempt at json serialization in haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveGeneric, DeriveAnyClass, StandaloneDeriving #-} | |
import Data.Map (empty, fromList, insert, delete, Map) | |
import GHC.Generics | |
import Data.Aeson | |
import qualified Data.Text.Lazy.IO as T | |
import qualified Data.Text.Lazy.Encoding as T | |
data Task = Task | |
{ id :: String | |
, description :: String | |
, dependsOn :: [String] | |
, dependentTasks :: [String] | |
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | |
type Storage = Map String Task | |
type Change = Storage -> Storage | |
s :: Storage | |
s = empty | |
addTask :: Task -> Change | |
addTask (Task id desc dep dept) = insert id (Task id desc dep dept) | |
instance ToJSON Change where | |
parseJSON (Array v) = do | |
name <- parseJSON $ v V.! 0 | |
task <- parseJSON $ v V.! 1 | |
return (addTask task) | |
removeTask :: String -> Change | |
removeTask tid = delete tid | |
changes = [addTask (Task "1" "Description" [] []), removeTask "1"] | |
-- main = putStrLn . show $ foldl (\s c -> c s) s changes | |
d = "[addTask, {\"id\": \"1\", \"description\": \"d\", \"dependsOn\": [], \"dependentTasks\": []}]" | |
main = putStrLn . show $ decode d $ s | |
-- main = T.putStrLn . T.decodeUtf8 . encode $ changes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment