Skip to content

Instantly share code, notes, and snippets.

@purpleP
Created Mar 9, 2017
Embed
What would you like to do?
attempt at json serialization in haskell
{-# 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