我的sum数据类型如下所示:
data Declaration =
IndDecl { what :: String, name :: String, argnames :: Maybe [String], constructors :: [Constructor] }
| TypeDecl { what :: String, name :: String, argnames :: Maybe [String], value :: Maybe Arg }
| FixDecl { what :: String, fixlist :: Maybe [Fixitem] }
| TermDecl { what :: String, name :: String, typ :: Maybe Typ, value :: Maybe Arg }
deriving (Show, Eq)
我正在使用Data.Aeson.TH
使用以下选项派生JSON序列化器/解串器:
$(deriveJSON defaultOptions { sumEncoding = UntaggedValue } ''Declaration)
从JSON构造中删除标记。同时,我认为我的数据已经有一个标记,即what
字段。它可以是以下之一:
IndDecl : "decl:ind"
FixDecl : "decl:fix"
等。因此,我对我的JSON反序列化对象使用Untagged配置这一事实令人担忧。如何使Aeson根据我的数据已有的标记派生JSON存根?
或者最好删除what
字段并让Aeson添加一个描述构造函数的“tag”字段?
编辑:我通过添加标记字段来尝试后者。现在看起来像:
-- Declarations
data Declaration =
IndDecl { what :: String, tag :: String, name :: String, argnames :: Maybe [String], constructors :: [Constructor] }
| TypeDecl { what :: String, tag :: String, name :: String, argnames :: Maybe [String], value :: Maybe Arg }
| FixDecl { what :: String, tag :: String, fixlist :: Maybe [Fixitem] }
| TermDecl { what :: String, tag :: String, name :: String, typ :: Maybe Typ, value :: Maybe Arg }
deriving (Show, Eq)
但是,我收到以下错误:
$ .declarations [0]出错:键“tag”不存在
我不明白,因为我创建了一个“标记”字段。