Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing maps which are initialized in constructors

I've just encountered an interesting problem related to Java serialization.

It seems that if my map is defined like this:

Map<String, String> params = new HashMap<String, String>() {{
  put("param1", "value1");
  put("param2", "value2");
}};

And I try to serialize it to a file with ObjectOutputStream:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outputFile));
oos.writeObject(params);

...I get java.io.NotSerializableException.

However, if instead I put values to the map the standard way:

Map<String, String> params = new HashMap<String, String>();
params.put("param1", "value1");
params.put("param2", "value2");

...then serialization work fine.

Can anybody tell me why it happens and what's the difference between these statements? I think they should work the same, but apparently I'm missing something.

like image 838
rmaruszewski Avatar asked Jul 31 '12 09:07

rmaruszewski


People also ask

What is the use of constructor in serialization?

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.

Which Java map is serializable?

Hashmap: A HashMap stores items in key/value pairs, and we can access them by an index of another type (such as a string). Now to serialize anything, you have to implement the java. io. Serializable interface and HashMap also implements the Serializable interface.

How is map serialized in Java?

Serialization converts a Java object into a stream of bytes, which can be persisted or shared as needed. Java Maps are collections that map a key Object to a value Object, and are often the least intuitive objects to serialize.


1 Answers

The first example is creating an anonymous inner class. How ?

Map<String, String> params = new HashMap<String, String>() {};

would create a new class derived from HashMap (note the following braces, in which you can put methods, members etc.)

Your map initialisation then declares an initialiser block thus:

Map<String, String> params = new HashMap<String, String>() { 
                                                             { // here } 
                                                           };

and in that you call your population methods.

This idiom is fine, but you have to be aware that you're creating a new class, not just a new object.

Because this class is an inner class, it'll have an implicit this pointer to the containing outer class. Your anonymous class would be serialisable due to its derivation from a serialisable class. However your outer class (referenced by the this pointer) isn't.

Tools like XStream, which serialise to XML via reflection, will discover the this pointer and attempt to serialise the surrounding object, which is similarly confusing.

like image 123
Brian Agnew Avatar answered Nov 15 '22 21:11

Brian Agnew