corrected json printing

some object labels must be preceded by ".", to not be in conflict with GF records (which are stored as json objects)
plus some minor bugfixes and cleaning
This commit is contained in:
Peter Ljunglöf
2019-03-08 17:35:01 +01:00
parent 8cf4446e8c
commit 3328279120

View File

@@ -3,7 +3,6 @@ module GF.Grammar.CanonicalJSON (
) where ) where
import Text.JSON import Text.JSON
import qualified Control.Monad as CM (mapM, msum)
import GF.Grammar.Canonical import GF.Grammar.Canonical
@@ -38,23 +37,19 @@ instance JSON CatDef where
instance JSON FunDef where instance JSON FunDef where
showJSON (FunDef f ty) = makeObj [("fun", showJSON f), ("type", showJSON ty)] showJSON (FunDef f ty) = makeObj [("fun", showJSON f), ("type", showJSON ty)]
{-
instance FromJSON FunDef where
parseJSON = withObject "FunDef" $ \o -> FunDef <$> o .: "fun" <*> o .: "type"
-}
instance JSON Type where instance JSON Type where
showJSON (Type bs ty) = makeObj [("args", showJSON bs), ("result", showJSON ty)] showJSON (Type bs ty) = makeObj [(".args", showJSON bs), (".result", showJSON ty)]
instance JSON TypeApp where instance JSON TypeApp where
-- non-dependent categories are encoded as simple strings: -- non-dependent categories are encoded as simple strings:
showJSON (TypeApp c []) = showJSON c showJSON (TypeApp c []) = showJSON c
showJSON (TypeApp c args) = makeObj [("cat", showJSON c), ("args", showJSON args)] showJSON (TypeApp c args) = makeObj [(".cat", showJSON c), (".args", showJSON args)]
instance JSON TypeBinding where instance JSON TypeBinding where
-- non-dependent categories are encoded as simple strings: -- non-dependent categories are encoded as simple strings:
showJSON (TypeBinding Anonymous (Type [] (TypeApp c []))) = showJSON c showJSON (TypeBinding Anonymous (Type [] (TypeApp c []))) = showJSON c
showJSON (TypeBinding x ty) = makeObj [("var", showJSON x), ("type", showJSON ty)] showJSON (TypeBinding x ty) = makeObj [(".var", showJSON x), (".type", showJSON ty)]
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@@ -88,8 +83,8 @@ instance JSON LinType where
-- parameters are also encoded as strings: -- parameters are also encoded as strings:
ParamType pt -> showJSON pt ParamType pt -> showJSON pt
-- tables/tuples are encoded as JSON objects: -- tables/tuples are encoded as JSON objects:
TableType pt lt -> makeObj [("tblarg", showJSON pt), ("tblval", showJSON lt)] TableType pt lt -> makeObj [(".tblarg", showJSON pt), (".tblval", showJSON lt)]
TupleType lts -> makeObj [("tuple", showJSON lts)] TupleType lts -> makeObj [(".tuple", showJSON lts)]
-- records are encoded as records: -- records are encoded as records:
RecordType rows -> showJSON rows RecordType rows -> showJSON rows
@@ -99,16 +94,16 @@ instance JSON LinValue where
-- concatenation is encoded as a JSON array: -- concatenation is encoded as a JSON array:
ConcatValue v v' -> showJSON [showJSON v, showJSON v'] ConcatValue v v' -> showJSON [showJSON v, showJSON v']
-- most values are encoded as JSON objects: -- most values are encoded as JSON objects:
ParamConstant pv -> makeObj [("param", showJSON pv)] ParamConstant pv -> makeObj [(".param", showJSON pv)]
PredefValue p -> makeObj [("predef", showJSON p)] PredefValue p -> makeObj [(".predef", showJSON p)]
TableValue t tvs -> makeObj [("tblarg", showJSON t), ("tblrows", showJSON tvs)] TableValue t tvs -> makeObj [(".tblarg", showJSON t), (".tblrows", showJSON tvs)]
TupleValue lvs -> makeObj [("tuple", showJSON lvs)] TupleValue lvs -> makeObj [(".tuple", showJSON lvs)]
VarValue v -> makeObj [("var", showJSON v)] VarValue v -> makeObj [(".var", showJSON v)]
ErrorValue s -> makeObj [("error", showJSON s)] ErrorValue s -> makeObj [(".error", showJSON s)]
Projection lv l -> makeObj [("project", showJSON lv), ("label", showJSON l)] Projection lv l -> makeObj [(".project", showJSON lv), (".label", showJSON l)]
Selection tv pv -> makeObj [("select", showJSON tv), ("key", showJSON pv)] Selection tv pv -> makeObj [(".select", showJSON tv), (".key", showJSON pv)]
VariantValue vs -> makeObj [("variants", showJSON vs)] VariantValue vs -> makeObj [(".variants", showJSON vs)]
PreValue alts def -> makeObj [("pre", showJSON alts), ("default", showJSON def)] PreValue pre def -> makeObj [(".pre", showJSON pre),(".default", showJSON def)]
-- records are encoded directly as JSON records: -- records are encoded directly as JSON records:
RecordValue rows -> showJSON rows RecordValue rows -> showJSON rows
@@ -132,17 +127,17 @@ instance JSON LinPattern where
instance JSON arg => JSON (Param arg) where instance JSON arg => JSON (Param arg) where
-- parameters without arguments are encoded as strings: -- parameters without arguments are encoded as strings:
showJSON (Param p []) = showJSON p showJSON (Param p []) = showJSON p
showJSON (Param p args) = makeObj [("paramid", showJSON p), ("args", showJSON args)] showJSON (Param p args) = makeObj [(".paramid", showJSON p), (".args", showJSON args)]
instance JSON a => JSON (RecordRow a) where instance JSON a => JSON (RecordRow a) where
-- record rows and lists of record rows are both encoded as JSON records (i.e., objects) -- record rows and lists of record rows are both encoded as JSON records (i.e., objects)
showJSON row = makeObj [toJSONRecordRow row] showJSON row = showJSONs [row]
showJSONs rows = makeObj (map toRow rows)
where toRow (RecordRow (LabelId lbl) val) = (lbl, showJSON val)
toJSONRecordRow :: JSON a => RecordRow a -> (String,JSValue)
toJSONRecordRow (RecordRow (LabelId lbl) val) = (lbl, showJSON val)
instance JSON TableRowValue where instance JSON TableRowValue where
showJSON (TableRowValue l v) = makeObj [("pattern", showJSON l), ("value", showJSON l)] showJSON (TableRowValue l v) = makeObj [(".pattern", showJSON l), (".value", showJSON v)]
-- *** Identifiers in Concrete Syntax -- *** Identifiers in Concrete Syntax
@@ -166,12 +161,12 @@ instance JSON VarId where
showJSON (VarId x) = showJSON x showJSON (VarId x) = showJSON x
instance JSON QualId where instance JSON QualId where
showJSON (Qual (ModId m) n) = showJSON (m++"_"++n) showJSON (Qual (ModId m) n) = showJSON (m++"."++n)
showJSON (Unqual n) = showJSON n showJSON (Unqual n) = showJSON n
instance JSON Flags where instance JSON Flags where
-- flags are encoded directly as JSON records (i.e., objects): -- flags are encoded directly as JSON records (i.e., objects):
showJSON (Flags fs) = makeObj [(f,showJSON v) | (f, v) <- fs] showJSON (Flags fs) = makeObj [(f, showJSON v) | (f, v) <- fs]
instance JSON FlagValue where instance JSON FlagValue where
-- flag values are encoded as basic JSON types: -- flag values are encoded as basic JSON types: