At the first time my code worked but without this code :
"decisions" .= object ["texts" .= texts,
"liens" .= liens]
And when I try to make a nasted json how you can see in my next code that not worked:
data Decisions = Decisions { texts :: Text,
liens :: Int
}
deriving Show
data Chap = Chap { titre :: String,
image :: String,
paragraph :: [Text],
decisione :: [Text],
link :: [Int],
battle :: [Text],
decisions :: [Decisions]
}
deriving Show
instance ToJSON Chap where
toJSON (Chap titre image paragraph decisione link battle decisions) = object [
"titre" .= titre,
"image" .= image,
"paragraph" .= array paragraph,
"decisione" .= array decisione,
"link" .= array link,
"battle" .= array battle,
"decisions" .= object ["texts" .= texts,
"liens" .= liens]
]
getServiceR :: Int -> Handler Value
getServiceR id = do
returnJson $ extraire $ find (condition id) chapitres
where
condition id (idChap, chap) = id == idChap
extraire Nothing = Chap "" "" [] [] [] [] [[],[]]
extraire (Just (id,p)) = p
chapitres :: [(Int, Chap)]
chapitres = [
(1,Chap "Titre 1" "image 1" ["paragraph1", "paragraph11"] ["decisione 1"] [125] ["yes"] [(["turn left"],["5"]) (["turn right"],["15"])]),
(2,Chap "Titre 2" "image 2" ["paragraph2", "paragraph21"] ["decisione 2"] [125] ["yes"] [["turn left"],["35"]] (["turn right"],["105"])]),
(3,Chap "Titre 3" "image 3" ["paragraph1", "paragraph11"] ["decisione 1"] [125] ["yes"] [["turn left"],["55"]] ([],[])]),
]
Error which i recive :
Handler\Game.hs:130:758:
Couldn't match expected type `[a15] -> Decisions'
with actual type `[a14]'
The function `[]' is applied to one argument,
but its type `[a14]' has none
In the expression: [] []
In the 7th argument of `Chap', namely `[[] []]'
Build failure, pausing...
Please I need to know a right syntaxe to make the nested jeson in haskell.
[[], []]but accidentally wrote[[] []]instead.