-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonParse.hs
72 lines (58 loc) · 2.03 KB
/
jsonParse.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{-# LANGUAGE OverloadedStrings, DeriveGeneric, DeriveAnyClass #-}
module jsonParse where
import GHC.Generics
import Data.ByteString.Lazy.readFile -- readFile :: FilePath -> IO ByteString
import Data.Aeson
import MonadLib
import Data.Maybe
import Data.Text.Lazy.IO
import Data.Text.Lazy.Encoding
-- automatically define how to read the connection file into a haskell datatype
-- i.e. defines encode and decode functions
-- encode :: ConnectionFile -> ByteString
-- decode :: ByteString -> Maybe ConnectionFile
data ConnectionFile = ConnectionFile {
control_port :: Int,
shell_port :: Int,
transport :: String,
signature_scheme :: String,
stdin_port :: Int,
hb_port :: Int,
ip :: String,
iopub_port :: Int,
key :: String }
deriving (Generic, ToJSON, FromJSON)
data JupyterMessageHeader = JupyterMessageHeader {
msg_id :: Text,
username :: Text,
session :: Text,
date :: Text,
msg_type :: Text,
version :: Scientific }
deriving (Generic, ToJSON, FromJSON)
data JupyterMessage = JupyterMessage {
header :: JupyterMessageHeader,
parent_header :: Object,
metadata :: Object,
content :: Object,
buffers :: Array }
deriving (Generic, ToJSON, FromJSON)
-------- utility functions
-- get a json bytestring from a file
getJsonText :: IO ByteString -- utf8 encoding result
getJsonText filepath = Data.ByteString.Lazy.readFile filepath >>=
Data.Text.Lazy.Encoding.encodeUtf8
-- get decoded data from specified json file
decodeJsonFile :: FilePath -> IO Value
decodeJsonFile path = let decodes = return . decode :: IO (Maybe Value)
in getJsonText >>= decodes >>= return . fromJust
-------- jupyter messages session monad
-- it carries state about session id, username
-- it generates information about time etc
-- it handles incrementing message uuid's
-- it handles the parent header stuff
-- it wraps the ZMQ monad which does the actual communication
-- it handles communicating with the kernel thread
-->
data JupyterSession = JupyterSession
-------- pre-defined jupyter massages