Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 5675072

Browse files
committed
Merge pull request #35 from cocreature/json-instances
Handwrite ToJSON instances, fix #32
2 parents c71043f + 29dd387 commit 5675072

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

haskell-ide-plugin-api/Haskell/Ide/Engine/PluginDescriptor.hs

+64-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE OverloadedStrings #-}
12
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE TypeSynonymInstances #-}
34
{-# LANGUAGE FlexibleInstances #-}
@@ -21,6 +22,7 @@
2122

2223
module Haskell.Ide.Engine.PluginDescriptor where
2324

25+
import Control.Applicative
2426
import Control.Monad.IO.Class
2527
import Data.Aeson
2628
import Data.Aeson.Types
@@ -134,57 +136,105 @@ type Dispatcher = forall m. (MonadIO m,GHC.GhcMonad m,HasIdeState m)
134136
-- JSON instances
135137

136138
instance ToJSON Context where
137-
toJSON = genericToJSON defaultOptions
139+
toJSON ctxt = object [ "cabal" .= toJSON (ctxCabal ctxt)
140+
, "file" .= toJSON (ctxFile ctxt)
141+
, "start_pos" .= toJSON (ctxStartPos ctxt)
142+
, "end_pos" .= toJSON (ctxEndPos ctxt) ]
138143

139144
instance FromJSON Context where
140-
-- No need to provide a parseJSON implementation.
145+
parseJSON (Object v) =
146+
Context <$> v .: "cabal"
147+
<*> v .: "file"
148+
<*> v .: "start_pos"
149+
<*> v .: "end_pos"
150+
parseJSON _ = empty
141151

142152
-- -------------------------------------
143153

144154
instance ToJSON CabalSection where
145-
toJSON = genericToJSON defaultOptions
155+
toJSON (CabalSection s) = toJSON s
146156

147157
instance FromJSON CabalSection where
148-
-- No need to provide a parseJSON implementation.
158+
parseJSON (String s) = pure $ CabalSection s
159+
parseJSON _ = empty
149160

150161
-- -------------------------------------
151162

152163
instance ToJSON AcceptedContext where
153-
toJSON = genericToJSON defaultOptions
164+
toJSON CtxNone = String "none"
165+
toJSON CtxPoint = String "point"
166+
toJSON CtxRegion = String "region"
167+
toJSON CtxFile = String "file"
168+
toJSON CtxCabalTarget = String "cabal_target"
169+
toJSON CtxProject = String "project"
154170

155171
instance FromJSON AcceptedContext where
156-
-- No need to provide a parseJSON implementation.
172+
parseJSON (String "none") = pure CtxNone
173+
parseJSON (String "point") = pure CtxPoint
174+
parseJSON (String "region") = pure CtxRegion
175+
parseJSON (String "file") = pure CtxFile
176+
parseJSON (String "cabal_target") = pure CtxCabalTarget
177+
parseJSON (String "project") = pure CtxProject
178+
parseJSON _ = empty
157179

158180
-- -------------------------------------
159181

160182
instance ToJSON RequiredParam where
161-
toJSON = genericToJSON defaultOptions
183+
toJSON (RP s) = toJSON s
162184

163185
instance FromJSON RequiredParam where
164-
-- No need to provide a parseJSON implementation.
186+
parseJSON (String s) = pure $ RP s
187+
parseJSON _ = empty
165188

166189
-- -------------------------------------
167190

168191
instance ToJSON CommandDescriptor where
169-
toJSON = genericToJSON defaultOptions
192+
toJSON cmdDescriptor = object [ "name" .= cmdName cmdDescriptor
193+
, "ui_description" .= cmdUiDescription cmdDescriptor
194+
, "contexts" .= cmdContexts cmdDescriptor
195+
, "additional_params" .= cmdAdditionalParams cmdDescriptor ]
170196

171197
instance FromJSON CommandDescriptor where
172-
-- No need to provide a parseJSON implementation.
198+
parseJSON (Object v) =
199+
CommandDesc <$> v .: "name"
200+
<*> v .: "ui_description"
201+
<*> v .: "contexts"
202+
<*> v .: "additional_params"
203+
parseJSON _ = empty
173204

174205
-- -------------------------------------
175206

176207
instance ToJSON IdeRequest where
177-
toJSON = genericToJSON defaultOptions
208+
toJSON (IdeRequest{ideCommand = command,ideContext = context,ideParams = params}) =
209+
object [ "command" .= command
210+
, "context" .= context
211+
, "params" .= params]
178212

179213
instance FromJSON IdeRequest where
180-
-- No need to provide a parseJSON implementation.
214+
parseJSON (Object v) =
215+
IdeRequest <$> v .: "command"
216+
<*> v .: "context"
217+
<*> v .: "params"
218+
parseJSON _ = empty
181219

182220
-- -------------------------------------
183221

184222
instance ToJSON IdeResponse where
185-
toJSON = genericToJSON defaultOptions
223+
toJSON (IdeResponseOk v) = object [ "tag" .= String "ok"
224+
, "contents" .= toJSON v ]
225+
toJSON (IdeResponseFail v) = object [ "tag" .= String "fail"
226+
, "contents" .= toJSON v ]
227+
toJSON (IdeResponseError v) = object [ "tag" .= String "error"
228+
, "contents" .= toJSON v ]
186229

187230
instance FromJSON IdeResponse where
188-
-- No need to provide a parseJSON implementation.
231+
parseJSON (Object v) = do
232+
tag <- v .: "tag" :: Parser T.Text
233+
case tag of
234+
"ok" -> IdeResponseOk <$> v .: "contents"
235+
"fail" -> IdeResponseFail <$> v .: "contents"
236+
"error" -> IdeResponseError <$> v .: "contents"
237+
_ -> empty
238+
parseJSON _ = empty
189239

190240
-- EOF

0 commit comments

Comments
 (0)