是否可以使用
JSON的深度选项来使用自定义
JSON marshallers?
我一直在尝试一些事情,没有深刻的选择,我的定制marshallers工作正常.但是当我添加JSON.use(‘deep’)时,我的自定义marshallers从未被调用过……
我还需要做些什么吗?
到目前为止我所尝试的(所有这些都在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() } }
解决方法
这将使任务取得成就.这回答了你的问题了吗?
JSON.registerObjectMarshaller(Mission) { def returnSet = [:] returnSet.id = it.id returnSet.name = it.name returnSet.active = it.active returnSet.achievements = it.achievements return returnSet } JSON.registerObjectMarshaller(AchievementBase) { def returnSet = [:] returnSet.id = it.id returnSet.name = it.name returnSet.active = it.active returnSet.hidden = it.hidden return returnSet }