Skip to content

Instantly share code, notes, and snippets.

@purpleP
Created March 9, 2017 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purpleP/ce84b570b654b00f383dca364f0320cd to your computer and use it in GitHub Desktop.
Save purpleP/ce84b570b654b00f383dca364f0320cd to your computer and use it in GitHub Desktop.
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