开发者

notserializable exception when trying to serialize java map converted from scala

I have a method to serialize a java map Map<UUID,String>. It works fine. I can serialize and deserialize in java.

But I have to call this method from scala and this is my calling code.

def customSerialize:Unit = {
Serializer.serialize(modMap(scalaMap))

def modMap(oldMap : Map[UUID,SomeObject]) : java.util.Map[UUID,java.lang.String] = {
        oldMap map { case(k,v) => (k->v.name)}
}

The scala map is scala.collection.Map and I am using import sca开发者_如何学JAVAla.collection.JavaConversions._ for doing the conversion.

When I run this code I get the error

java.io.NotSerializableException: scala.collection.JavaConversions$MapWrapper
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)

It looks like I need one more conversion from javaconversions$MapWrapper to java.util.Map. Is this correct? Is there a way to do this?


As far as I can tell, you do need to copy the map because the MapWrapper is not serializable. Best would be for Scala to support this, but in the meantime a reasonable syntax is just to use the copy constructor for a java Map. Your call would then look like this:

Serializer.serialize(new java.util.HashMap(modMap(scalaMap)))


Personally, I'd open an enhancement issue requesting that these wrappers be serializable. Not that it would help you in the short term, but...

Have you tried the stuff in JavaConverters instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜