Simplify the JSON representation of AST

This commit is contained in:
Adam Sapek 2015-03-25 23:46:33 -07:00
Родитель df910f48d3
Коммит 48e1a55847
2 изменённых файлов: 39 добавлений и 9 удалений

Просмотреть файл

@ -59,7 +59,7 @@ instance FromJSON Type where
o .: "value"
String "user" -> BT_UserDefined <$>
o .: "declaration" <*>
o .: "arguments"
o .:? "arguments" .!= []
_ -> modifyFailure
(const $ "Invalid value `" ++ show type_ ++ "` for the `type` key.")
empty
@ -121,6 +121,10 @@ instance ToJSON Type where
[ "type" .= String "parameter"
, "value" .= p
]
toJSON (BT_UserDefined decl []) = object
[ "type" .= String "user"
, "declaration" .= decl
]
toJSON (BT_UserDefined decl args) = object
[ "type" .= String "user"
, "declaration" .= decl
@ -206,11 +210,38 @@ deriving instance Generic Language
instance FromJSON Language
instance ToJSON Language
deriving instance Generic Namespace
instance FromJSON Namespace
instance ToJSON Namespace
instance FromJSON Namespace where
parseJSON (Object v) =
Namespace <$>
v .:? "language" <*>
v .: "name"
parseJSON x = modifyFailure
(const $ "Expected an object but found: " ++ show x)
empty
deriving instance Generic Bond
instance FromJSON Bond
instance ToJSON Bond
instance ToJSON Namespace where
toJSON (Namespace Nothing name) = object
[ "name" .= name
]
toJSON Namespace {..} = object
[ "language" .= nsLanguage
, "name" .= nsName
]
instance FromJSON Bond where
parseJSON (Object v) =
Bond <$>
v .: "imports" <*>
v .: "namespaces" <*>
v .: "declarations"
parseJSON x = modifyFailure
(const $ "Expected an object but found: " ++ show x)
empty
instance ToJSON Bond where
toJSON Bond {..} = object
[ "imports" .= bondImports
, "namespaces" .= bondNamespaces
, "declarations" .= bondDeclarations
]

Просмотреть файл

@ -138,8 +138,7 @@ foldMapFields f t = case t of
(BT_UserDefined a@Alias {..} args) -> foldMapFields f $ resolveAlias a args
_ -> mempty
foldMapStructFields :: Monoid m
=> (Field -> m) -> Declaration -> m
foldMapStructFields :: Monoid m => (Field -> m) -> Declaration -> m
foldMapStructFields f s = foldMapFields f $ BT_UserDefined s []
foldMapType :: (Monoid m) => (Type -> m) -> Type -> m