@@ -12,6 +12,8 @@ module Elm.Compiler.Type
14import Control.Arrow ((***))
14import Control.Arrow ((***))
15+import Data.Aeson ((.:))
16+import qualified Data.Aeson as Aeson
15import qualified Data.Text as Text
17import qualified Data.Text as Text
16import Data.Text (Text)
18import Data.Text (Text)
17import qualified Text.PrettyPrint as P
19import qualified Text.PrettyPrint as P
@@ -173,13 +175,50 @@ encode tipe =
174decoder :: Decode.Decoder Type
176decoder :: Decode.Decoder Type
176− do txt <- Decode.text
180+ do txt <- Decode.text
177− case Parse.run Type.expression txt of
181+ case Parse.run Type.expression (Text.replace "'" "_" txt) of
178− Left _ ->
182+ Left _ ->
179− Decode.fail "a type, like (String -> Int)"
183+ Decode.fail "a type, like (String -> Int)"
185+ Right (tipe, _, _) ->
186+ Decode.succeed (fromRawType tipe)
192+instance Aeson.FromJSON Type where
194+ Aeson.withObject "Type" $ \object ->
195+ do tag <- object .: "tag"
196+ case (tag :: String) of
198+ Lambda <$> object .: "in" <*> object .: "out"
201+ Var <$> object .: "name"
204+ do name <- object .: "name"
205+ return (Type name [])
208+ do func <- object .: "func"
209+ args <- object .: "args"
212+ return (Type name args)
215+ fail "unexpected type application"
218+ Record <$> object .: "fields" <*> object .: "extension"
181− Right (tipe, _, _) ->
220+ _ ->
182− Decode.succeed (fromRawType tipe)
221+ fail $ "Error when decoding type with tag: " ++ tag
185fromRawType :: Type.Raw -> Type
224fromRawType :: Type.Raw -> Type