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()
}
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With