是否可以使用 JSON 的深度选项来使用自定义 JSON 编组器?
我一直在尝试一些事情,如果没有深度选项,我的自定义编组器工作正常。但是当我添加 JSON.use('deep') 时,我的自定义编组器永远不会被调用......
还有什么我需要做的吗?
到目前为止我所尝试的(所有这些都在 Bootstrap 中,以及有和没有优先级参数(值 1 和 999999))。此外,我尝试将 JSON.use('deep') 调用放在开头和结尾,结果相同。
选项1
JSON.use("deep") {
JSON.registerObjectMarshaller(new UserMarshaller(),999999)
JSON.registerObjectMarshaller(new TeamMarshaller(),999999)
JSON.registerObjectMarshaller(new LevelMarshaller(),999999)
JSON.registerObjectMarshaller(new BehaviourMarshaller(),999999)
JSON.registerObjectMarshaller(new AchievementMarshaller(),999999)
JSON.registerObjectMarshaller(new PercentageAchievementMarshaller(),999999)
JSON.registerObjectMarshaller(new IntervalMarshaller(),999999)
JSON.registerObjectMarshaller(new MissionMarshaller(),999999)
JSON.registerObjectMarshaller(new ActivityMarshaller(),999999)
JSON.registerObjectMarshaller(new RecentActivityMarshaller(),999999)
JSON.registerObjectMarshaller(new ProfileMarshaller(),999999)
JSON.registerObjectMarshaller(new StatusMarshaller(),999999)
JSON.registerObjectMarshaller(ObjectId){ObjectId oid->
return oid.toString()
}
}
JSON.use('deep')
选项 2
JSON.registerObjectMarshaller(new UserMarshaller(),999999)
JSON.registerObjectMarshaller(new TeamMarshaller(),999999)
JSON.registerObjectMarshaller(new LevelMarshaller(),999999)
JSON.registerObjectMarshaller(new BehaviourMarshaller(),999999)
JSON.registerObjectMarshaller(new AchievementMarshaller(),999999)
JSON.registerObjectMarshaller(new PercentageAchievementMarshaller(),999999)
JSON.registerObjectMarshaller(new IntervalMarshaller(),999999)
JSON.registerObjectMarshaller(new MissionMarshaller(),999999)
JSON.registerObjectMarshaller(new ActivityMarshaller(),999999)
JSON.registerObjectMarshaller(new RecentActivityMarshaller(),999999)
JSON.registerObjectMarshaller(new ProfileMarshaller(),999999)
JSON.registerObjectMarshaller(new StatusMarshaller(),999999)
JSON.registerObjectMarshaller(ObjectId){ObjectId oid->
return oid.toString()
}
JSON.use('deep')
并且也尝试了Config选项,但是没有效果,没有触发深度转换……
更新:一个例子一个任务有很多成就,我想检索一个任务及其成就
class Mission {
/** Identificador */
ObjectId id
/** Nombre */
String name
/** Indica si la misión está o no activa */
boolean active = true
List<AchievementBase> achievements = []
static hasMany = [achievements:AchievementBasen]
}
class AchievementBase {
/** Identificador */
ObjectId id
/** Nombre del logro */
String name
/** Valor booleano para indicar si el logro está o no activo */
boolean active = true
/** Valor booleano para indicar si el logro está o no visible */
boolean hidden = false
}
这是我正在写的编组器的一个例子
class MissionMarshaller implements ObjectMarshaller<JSON>{
@Override
public boolean supports(Object object) {
object instanceof Mission
}
@Override
public void marshalObject(Object object, JSON converter)
throws ConverterException {
JSONWriter writer = converter.getWriter()
writer.object()
writer.key('id').value(object.id)
.key("name").value(object.name)
.key('achievements').value(object.achievements)
writer.endObject()
}
}
class AchievementMarshaller implements ObjectMarshaller<JSON>{
@Override
public boolean supports(Object object) {
object instanceof Achievement
}
@Override
public void marshalObject(Object object, JSON converter)
throws ConverterException {
JSONWriter writer = converter.getWriter()
writer.object()
writer.key('id').value(object.id)
.key("name").value(object.name)
writer.endObject()
}
}