我正在尝试使用 Grail 的 MongoDB 插件将通用 JSONObject 保存到 Domain 对象。基本上它不适用于常规属性(不足为奇)。所以我添加了一个动态属性,它将 JSONObject 保存到数据库并将其拉回(耶)。但是,由于我已经用 Jackson 序列化器替换了默认的 JSON 序列化器(由于 MongoDB 插件和域对象的不良行为),因此动态属性没有被序列化。
我可以使用什么 API 将所有动态属性添加到域以对其进行序列化?Object.properties() 不返回它。我找不到任何其他方法来退货。我现在必须修改 Jackson 以序列化 Grails 对象。任何想法如何可能是最简单的?
这是我的对象:
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
}
}