1

次のコード スニペットがあります。

val serializedClasses: List[MongoMetaRecord[MongoRecord[_]]] = List(User, Email)

残念ながら、要素から正確な型情報を取得できず、次のコードは失敗します。

serializedClasses.map(c => new RecordSerializer(c))

[error] JsonFormats.scala:19: inferred type arguments [_$1] do not conform to class RecordSerializer's type parameter bounds [T <: net.liftweb.mongodb.record.MongoRecord[T]]
[error]   implicit val jsonformats = DefaultFormats + (new ObjectIdSerializer)  + (new RecordSerializer(DirectMessage)) + (new RecordSerializer(User)) ++ serializedClasses.map(c => new RecordSerializer(c))

ここはRecordSerializer.scala

object RecordSerializer {
  def apply[T <: MongoRecord[T]](meta: MongoMetaRecord[T]) = new RecordSerializer(meta)
}

class RecordSerializer[T <: MongoRecord[T]](meta: MongoMetaRecord[T]) extends Serializer[MongoRecord[T]] {

  def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), MongoRecord[T]] = {
case (TypeInfo(clazz, parametrizedType), json: JValue) if(JsonFormats.serializedClassesNames.contains(clazz.getSimpleName)) => {
  meta.fromJValue(json).openOr(throw new MappingException("Couldn't convert"))
    }
  }

  def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case rec: MongoRecord[_] => rec.asJValue
  }
}
4

1 に答える 1