Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom JSON marshaller in Grails with deep option

Tags:

json

grails

Is it possible to use custom JSON marshallers using the deep option of JSON?

I've been trying some things and without the deep option, my custom marshallers are working fine. But when I add JSON.use('deep') my custom marshallers are never called...

Is there something else that I need to do?

What I have tried thus far (all this in Bootstrap and also with and without priority param (values 1 and 999999)). Also I've tried putting the JSON.use('deep') call at the beginning and the end with the same results.

OPTION 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')

OPTION 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')

And also tried the Config option, but has no effect, the deep conversion is not triggered...

UPDATE: An example One mission has many achievements and I want to retrieve a mission and its achievements

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

}

And this is one example of the marshaller I was writing

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()
    }

}
like image 623
Eylen Avatar asked Oct 14 '13 12:10

Eylen


Video Answer


1 Answers

This will render the achievements with the mission. Does this answer your question?

   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
    }
like image 67
James Kleeh Avatar answered Sep 28 '22 00:09

James Kleeh