forked from haskell/aeson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodetest.hs
83 lines (77 loc) · 3.18 KB
/
encodetest.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Data.Aeson
import Control.Applicative
import qualified Data.Text as T
import Data.Aeson.TH
import qualified Data.ByteString.Lazy.Char8 as L
import Data.ByteString.Lazy (ByteString)
import Data.HashMap.Base ()
import Data.Aeson.TH ()
data Test = Test { z :: Maybe [String]
, a :: Maybe T.Text
, b :: T.Text
, c :: ()
, d :: [String]
}
deriving (Show)
{-
instance ToJSON Test where
toJSON (Test z a b c d) = object [ T.pack "z" .= z
, T.pack "a" .= a
, T.pack "b" .= b
, T.pack "c" .= c
, T.pack "d" .= d
]
-}
-- $(deriveJSON id ''Test)
instance ToJSON Test where
{ toJSON
= \ value[a8qB]
-> case value[a8qB] of {
Test arg1[a8qC] arg2[a8qD] arg3[a8qE] arg4[a8qF] arg5[a8qG]
-> object
[(T.pack "z" .= arg1[a8qC]), (T.pack "a" .= arg2[a8qD]),
(T.pack "b" .= arg3[a8qE]), (T.pack "c" .= arg4[a8qF]),
(T.pack "d" .= arg5[a8qG])] } }
instance FromJSON Test where
{ parseJSON
= \ value[a8qH]
-> case value[a8qH] of
Object recObj[a8qI]
-> if (Data.HashMap.Base.size
recObj[a8qI]
==
5)
then
(((((Test
<$>
Data.Aeson.TH.lookupField
"Main.Test" "Test" recObj[a8qI] (T.pack "z"))
<*>
Data.Aeson.TH.lookupField
"Main.Test" "Test" recObj[a8qI] (T.pack "a"))
<*>
Data.Aeson.TH.lookupField
"Main.Test" "Test" recObj[a8qI] (T.pack "b"))
<*>
Data.Aeson.TH.lookupField
"Main.Test" "Test" recObj[a8qI] (T.pack "c"))
<*>
Data.Aeson.TH.lookupField
"Main.Test" "Test" recObj[a8qI] (T.pack "d"))
else
Data.Aeson.TH.parseTypeMismatch'
"Test"
"Main.Test"
"Object with 5 name/value pairs"
((show . unordered-containers-0.2.1.0:Data.HashMap.Base.size)
recObj[a8qI]
++
" name/value pairs")
other ->
Data.Aeson.TH.parseTypeMismatch'
"Test"
"Main.Test"
"Object"
(Data.Aeson.TH.valueConName other) }
main = do print $ encode $ Test Nothing (Nothing) (T.pack "hi") () ["hello"]
print $ (decode $ (L.pack "{\"b_\":\"hi\",\"d_\":[\"hello\"]}") :: Maybe Test)