Scala serialization exception with Enumeration Val

2019-04-09 14:13发布

I'm using the play 2.1 framework for scala and the MongoDB Salat plugin.

When I update an Enumeration.Value I got an exception:

java.lang.IllegalArgumentException: can't serialize class scala.Enumeration$Val
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270) ~[mongo-java-driver-2.11.1.jar:na]
    at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:295) ~[mongo-java-driver-2.11.1.jar:na]
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234) ~[mongo-java-driver-2.11.1.jar:na]
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174) ~[mongo-java-driver-2.11.1.jar:na]
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120) ~[mongo-java-driver-2.11.1.jar:na]
    at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27) ~[mongo-java-driver-2.11.1.jar:na]

Inserting the Enumeration.Value works fine. My case class looks like:

case class User(
    @Key("_id") id: ObjectId = new ObjectId,
    username: String,
    email: String,
    @EnumAs language: Language.Value = Language.DE,
    balance: Double,
    added: Date = new Date)

and my update code:

object UserDAO extends ModelCompanion[User, ObjectId] {

    val dao = new SalatDAO[User, ObjectId](collection = mongoCollection("users")) {}

    def update(): WriteResult = {
        UserDAO.dao.update(q = MongoDBObject("_id" -> new ObjectId(id)), o = MongoDBObject("$set" -> MongoDBObject("language" -> Language.EN))))
    }
}

Any ideas how to get that working?

EDIT:

workaround: it works if I cast the Enumeration.Value toString, but that's not how it should be...

UserDAO.dao.update(q = MongoDBObject("_id" -> new ObjectId(id)), o = MongoDBObject("$set" -> MongoDBObject("language" -> Language.EN.toString))))

3条回答
Bombasti
2楼-- · 2019-04-09 14:31

At the time of writing mongoDB doesn't place nice with scala enums, I use a decorator method as a work around.

Say you have this enum:

object EmployeeType extends Enumeration {
  type EmployeeType = Value
  val Manager, Worker = Value
}

and this mongodb record:

import EmployeeType._
case class Employee(
  id: ObjectId = new ObjectId
)

In your mongoDB, store the integer index of the enum instead of the enum itself:

case class Employee(
  id: ObjectId = new ObjectId,
  employeeTypeIndex: Integer = 0
){
  def employeeType = EmployeeType(employeeTypeIndex); /* getter */
  def employeeType_=(v : EmployeeType ) = { employeeTypeIndex= v.id} /* setter */
}

The extra methods implement getters and setters for the employee type enum.

查看更多
Bombasti
3楼-- · 2019-04-09 14:42

Salat only does its work when you serialize to and from your model object with the grater, not when you do queries with MongoDB-objects yourself. The mongo driver api knows nothing about the annotation @EnumAs. (In addition to that even if you could use salat for that, how would it be able to know that you are referring to User.language in a generic key->value MongoDBObject?)

So you have to do like you describe in your workaround. Provide the "value" of the enum yourself when you want to do queries.

查看更多
成全新的幸福
4楼-- · 2019-04-09 14:47

It is possible to add a BSON encoding for Enumeration. So, the conversion is done in a transparent manner.

Here is the code

RegisterConversionHelpers()
  custom()
  def custom() {
    val transformer = new Transformer {

      def transform(o: AnyRef): AnyRef = o match {
        case e: Enumeration$Val => e.toString
        case _ => o
      }
    }
    BSON.addEncodingHook(classOf[Enumeration$Val], transformer)
  }
}
查看更多
登录 后发表回答