-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.purs
52 lines (44 loc) · 1.28 KB
/
Main.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
46
47
48
49
50
51
52
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Either (Either)
import Data.Foreign (ForeignError)
import Data.List.NonEmpty (NonEmptyList)
import Data.Maybe (fromMaybe)
import Data.Nullable as Nullable
import Global.Unsafe (unsafeStringify)
import Simple.JSON (readJSON)
type MyThingy =
{ a :: String
, b :: Array String
}
myThingyJsonThing1 :: String
myThingyJsonThing1 =
"""
{ "a": "hello"
, "b": null
}
"""
myThingyJsonThing2 :: String
myThingyJsonThing2 =
"""
{ "a": "hello"
, "b": []
}
"""
parseMyThingyJsonFromImperfectJsonButConvertTheDirtyProperty ::
String -> Either (NonEmptyList ForeignError) MyThingy
parseMyThingyJsonFromImperfectJsonButConvertTheDirtyProperty str = do
json <- readJSON str
let b = fromMaybe [] <<< Nullable.toMaybe $ json.b
pure $ json { b = b }
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log' $ parseMyThingyJsonFromImperfectJsonButConvertTheDirtyProperty myThingyJsonThing1
-- {"value0":{"b":[],"a":"hello"}}
log' $ parseMyThingyJsonFromImperfectJsonButConvertTheDirtyProperty myThingyJsonThing2
-- {"value0":{"b":[],"a":"hello"}}
where
-- Show for records coming in 0.12 :)
log' = log <<< unsafeStringify