BasicDBList not transformed back to JValue during deserialization

32 views
Skip to first unread message

shain...@gmail.com

unread,
Mar 8, 2015, 11:04:54 AM3/8/15
to scala...@googlegroups.com
I'll try to make it short:
I have a class who's one of it's properties is a JValue. No matter what I put into this JValue (even a JString) - salat serializes it as a BasicDBList (which is weird imo in the first place..)

So saving an object to the database works fine, but when I try to findOne(), It seems like salat is unable to deserialize the object properly, as I'm getting:

  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]]]]


If it knows how to serialize a JValue (and turn it into a BasicDBList for it's own reasons I presume), why does the way back does not deserialize properly?
Am I missing something?

Noah Zucker

unread,
Mar 13, 2015, 1:11:55 PM3/13/15
to scala...@googlegroups.com
Hello!

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

Regards,

Noah Zucker
Novus Partners
Reply all
Reply to author
Forward
0 new messages