Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala serialization exception with Enumeration Value

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))))
like image 284
pichsenmeister Avatar asked Jan 13 '23 18:01

pichsenmeister


2 Answers

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)
  }
}
like image 165
Yann Moisan Avatar answered Jan 20 '23 17:01

Yann Moisan


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.

like image 45
user363349 Avatar answered Jan 20 '23 17:01

user363349