7

是否可以使用 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()
    }

}
4

1 回答 1

6

这将使成就与使命。这回答了你的问题了吗?

   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
    }
于 2013-10-14T14:35:28.037 回答