forked from purescript/purescript-foreign
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Complex.purs
executable file
·45 lines (38 loc) · 1.21 KB
/
Complex.purs
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
module Example.Complex where
import Prelude
import Control.Monad.Except (runExcept)
import Data.Maybe (Maybe)
import Data.Traversable (traverse)
import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString, readNullOrUndefined)
import Foreign.Index ((!))
type SomeObject =
{ foo :: String
, bar :: Boolean
, baz :: Number
, list :: Array ListItem
}
readSomeObject :: Foreign -> F SomeObject
readSomeObject value = do
foo <- value ! "foo" >>= readString
bar <- value ! "bar" >>= readBoolean
baz <- value ! "baz" >>= readNumber
list <- value ! "list" >>= readArray >>= traverse readListItem
pure { foo, bar, baz, list }
type ListItem =
{ x :: Number
, y :: Number
, z :: Maybe Number
}
readListItem :: Foreign -> F ListItem
readListItem value = do
x <- value ! "x" >>= readNumber
y <- value ! "y" >>= readNumber
z <- value ! "z" >>= readNullOrUndefined >>= traverse readNumber
pure { x, y, z }
main :: Effect Unit
main = do
let json = """{"foo":"hello","bar":true,"baz":1,"list":[{"x":1,"y":2},{"x":3,"y":4,"z":999}]}"""
logShow $ runExcept $ readSomeObject =<< foreignValue json