我想序列化各种类的实体对象数组。它们来自从单表继承实体组的抽象存储库中获取。
使用 API-Platform 的序列化程序,如果我不将实体标记为@ApiResource
,我会得到一个@context
包含所有我不需要的属性的大块。
{
"@context":{
"@vocab":"http:\/\/localhost:8080\/api\/docs.jsonld#",
"hydra":"http:\/\/www.w3.org\/ns\/hydra\/core#",
"name":"ProductCategory\/name",
"products":"ProductCategory\/products",
"lft":"ProductCategory\/lft",
"lvl":"ProductCategory\/lvl",
"rgt":"ProductCategory\/rgt",
"root":"ProductCategory\/root",
"parent":"ProductCategory\/parent",
"children":"ProductCategory\/children",
"position":"ProductCategory\/position",
"type":"ProductCategory\/type"
},
"@type":"ProductCategory",
"@id":"_:1404",
"id":3,
"name":"Apple crap"
},
没有:
@ApiResource
在实体中设置- 或编写自定义规范器,
- 或创建一个“getClass”可序列化方法(尽管我认为我最终会在抽象中这样做),
我怎样才能:
- 序列化此对象时获得简短的上下文或根本没有上下文
- 或使用纯 JSON 序列化获取类名
我想实现这样的目标(我需要“@type”),可能更短,但使用类名:
{
"@context":"\/api\/contexts\/Product",
"@id":"\/api\/products\/4",
"@type":"Product",
"id":4,
"name":"Apple watch"
},