Notserializable exception when trying to serialize a java map converted from scala

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

But I have to call this method from scala, and this is my call 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 use import scala.collection.JavaConversions._ to do the conversion.

When I run this code, I get an 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 another conversion from javaconversions$MapWrapper to java.util.Map . It's right? Is there any way to do this?

+3
source share
2 answers

Personally, I would open the improvement problem by requiring these wrappers to be serialized. Not that it helps you in the short term, but ...

Have you tried stuff in JavaConverters instead?

+1
source

As far as I can tell, you need to copy the map because MapWrapper is not serializable. It would be best for Scala to support this, but at the same time, a reasonable syntax is to simply use the copy constructor for the java map. Your call will look like this:

 Serializer.serialize(new java.util.HashMap(modMap(scalaMap))) 
+1
source

Source: https://habr.com/ru/post/972489/


All Articles