Grail の MongoDB プラグインを使用して、一般的な JSONObject を Domain オブジェクトに保存しようとしています。基本的に、通常のプロパティでは機能しません (驚くべきことではありません)。そこで、JSONObject を DB に保存して戻す動的プロパティを追加しました (yay)。ただし、デフォルトの JSON シリアライザー (MongoDB プラグインおよびドメイン オブジェクトの動作が悪いため) を Jackson シリアライザーに置き換えたため、動的プロパティはシリアライズされていません。
ドメインに追加されたすべての動的プロパティを取得してシリアル化するには、どの 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
}
}