Casbah 2.5.0 SNAPSHOT - Converting nested structures to DBObject

186 views
Skip to first unread message

Arun

unread,
Dec 18, 2012, 12:39:55 PM12/18/12
to mongodb-ca...@googlegroups.com
Hi Ross,

First off thanks for all your hard work in getting the SNAPSHOT out for scala 2.10. While testing, I ran into an issue converting a nested structure to DBObject. Find below a simple snippet that would reproduce the problem (this used to work in casbah 2.9.2_2.4.1)

val m = Map("a" -> 1, "b" -> 2, "c" -> List(Map("x" -> 1), Map("y" -> 2)))
val dbo = MongoDBObject(m.toList)
println(dbo)

Exception in thread "main" java.lang.RuntimeException: json can't serialize type : class scala.Tuple2
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
at com.mongodb.util.JSONSerializers$IterableSerializer.serialize(JSONSerializers.java:280)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSONSerializers$IterableSerializer.serialize(JSONSerializers.java:280)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:307)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSON.serialize(JSON.java:56)
at com.mongodb.util.JSON.serialize(JSON.java:41)
at com.mongodb.BasicDBObject.toString(BasicDBObject.java:83)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at scala.Console$.println(Console.scala:240)
at scala.Predef$.println(Predef.scala:287)
at persist.PersistTest$.main(PersistTest.scala:56)
at persist.PersistTest.main(PersistTest.scala)

Note: Nested lists and maps work fine. I run into this issue only with nested list that have nested maps.
For Example:

val m2 = Map("a" -> 1, "b" -> List(1, 2)) //... Works
val m3 = Map("a" -> 1, "b" -> Map("bb" -> 1, "cc" -> 2)) //... Works

Thanks,
Arun

ross....@gmail.com

unread,
Dec 18, 2012, 1:22:26 PM12/18/12
to mongodb-ca...@googlegroups.com, arunk...@gmail.com
Hi Arun,

Thanks for the reply - seems theres some difference between:

scalaj.collection.Imports._ and scala.collection.JavaConverters._

I'll let you know when I have a fix for that.

Regards,

Ross

ross....@gmail.com

unread,
Dec 19, 2012, 6:04:40 AM12/19/12
to mongodb-ca...@googlegroups.com, arunk...@gmail.com, ross....@gmail.com
Hi Arun,

This is now fixed and I've pushed a new snapshot.

Seems we have to be more explicit with the transformations under scala.collection.JavaConverters as you could get caught in an infinite loop - commit url: https://github.com/mongodb/casbah/commit/152b60cd844e0e2a290e6d9fc81304fa8123e620#diff-1

Ross

arunk...@gmail.com

unread,
Dec 20, 2012, 2:38:23 PM12/20/12
to mongodb-ca...@googlegroups.com, arunk...@gmail.com, ross....@gmail.com
Hi Ross,

I tested your fix and nested collections are getting transformed as expected.
Thanks for quick turnaround.

Regards,
Arun

Reply all
Reply to author
Forward
0 new messages