在Grails中使用带有深度选项的自定义JSON marshaller

时间:2013-10-14 12:10:09

标签: json grails

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

}

1 个答案:

答案 0 :(得分: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
    }