3

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 scala.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?

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • for now I am adding a extra conversion method inside my java code where I iterate through the Map provided by scala and create a new Java Map out of it. I hope there is a better way to do this. – Scala_beginer Jul 21 '11 at 17:56
  • This is still a problem, so I opened an issue: https://issues.scala-lang.org/browse/SI-8911 – Josh Rosen Oct 15 '14 at 01:01

2 Answers2

2

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)))
Dave DeCaprio
  • 2,051
  • 17
  • 31
1

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?

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681