I am trying to save a generic JSONObject to a Domain object using Grail's MongoDB plugin. Basically it doesn't work with regular properties (not surprising). So I added a dynamic property which saves the JSONObject to the DB and pulls it back out (yay). However, since I've replaced the default JSON serializer (due to bad behavior with MongoDB Plugin and Domain objects) with Jackson serializer the dynamic property is not being serialized.
What API can I use to get all dynamic properties added to a Domain to serialize it? Object.properties() doesn't return it. I can't find any other method to return it. I have to modify Jackson to serialize Grails objects now. Any ideas how that might be easiest?
Here is my object:
class ProblemAttempt {
static final STEP_GUIDED = 'guided'
static final STEP_INDEPENDENT = 'independent'
static constraints = {
timeSpent nullable: true
}
static mapWith="mongo"
static mapping = {
lessonAttemptId index: true
}
static embedded = ['tags']
ObjectId id
ObjectId problemId
ObjectId userId
String lessonExternalId
ObjectId lessonAttemptId
Date timeAnswered
String stepName
Boolean correct
Integer timeSpent
String lessonStatus
List<String> tags
ProblemAttempt(User user, String lessonExternalId, LessonAttempt attempt, String stepName, ObjectId problemId, boolean correct, Object answer) {
this.userId = user.id
this.lessonExternalId = lessonExternalId
this.correct = correct
this.lessonAttemptId = attempt.id
this.lessonStatus = LearningStatus.INCOMPLETE
this.problemId = problemId
this.stepName = stepName
this.timeAnswered = new Date()
this['answer'] = answer // have to use dynamic properties to persist generic JSON objects
tags = []
user.aspects.each {ProfileAspect aspect ->
tags << aspect.class.simpleName
}
user.groups.each {DomainReference group ->
tags << group.name
}
}
public Object getUserAnswered() {
return this['answer'] // this was added to handle serializing into json
}
}