mongodbscalasalat

SalatDAO does not deserialize BasicDBList properly


I have a case class who's one of the properties if of type JValue.

During serialization, it gets converted to BasicDBList (regardless of what was it's value, even it if was just a JString - this is odd, but set that aside...)

So saving the object to the database (Mongodb) works fine, but when I try to findOne(), I'm getting the following error:

argument type mismatch

          $anon$2(class com.example.data.Telemetry @ com.novus.salat.global.package$$anon$1@7376d6c3) toObject failed on:
          SYM: com.example.data.Telemetry
          CONSTRUCTOR
        public com.example.data.Telemetry(java.lang.String,org.json4s.JsonAST$JValue)

        ---------- CONSTRUCTOR EXPECTS FOR PARAM [1] --------------
        NAME:         location
        TYPE:         org.json4s.JsonAST$JValue
        DEFAULT ARG   [Missing, but unnecessary because input value was supplied]
        @Ignore       false
        ---------- CONSTRUCTOR INPUT ------------------------
        TYPE: com.mongodb.BasicDBList
        VALUE:
        [ [ [ "latitude" , [ 34.001]] , [ "longitude" , [ 32.511]]]]
        ------------------------------------------------------------

So it seems like the object does not get deserialized properly (from BasicDBList back to JValue). This works fine one what, but the other way seems broken.

Am I missing something?


Solution

  • Salat has no special handling / conversions for org.json4s classes. Because JString is a case class, ultimately Salat delegates to the default encoder in Casbah converts your JString to a java.util.List

    https://github.com/mongodb/casbah/blob/9f3ebe8e389a9cf91cc6ce1591b3950898e895a7/casbah-commons/src/main/scala/conversions/ScalaConversions.scala#L361

    Later, when Salat attempts to extract it from the DBObject, it similarly doesn't have any logic for converting it from a DBList to a JString.

    If you have to stay with a JString, I recommend you consider implementing a custom converter and registering it with your Salat context. Examples here:

    https://github.com/novus/salat/blob/master/salat-core/src/test/scala/com/novus/salat/test/custom/model.scala

    https://github.com/novus/salat/blob/master/salat-core/src/test/scala/com/novus/salat/test/CustomTransformerSpec.scala