5

Is there is any way in java-world to serialize without need of no-arg constructors and implementation of Serializable?

asked Nov 8, 2010 at 14:50
5
  • 3
    Java serialization does not require a no argument constructor. Commented Nov 8, 2010 at 17:10
  • 2
    Yes it does. See the Object Serialization Specification and Tom Hawtin's comment. Commented Nov 9, 2010 at 9:19
  • 1
    EJP, that is not correct, and Steve Kuo is. See docs.oracle.com/javase/7/docs/platform/serialization/spec/… . Your serializable class must "Have access to the no-arg constructor of its first nonserializable superclass". In other words, if you look up the inheritance tree towards Object, the first non-serializable class you see must have a non-private noarg constructor. More to come... Commented Feb 2, 2012 at 7:47
  • 1
    Your own serializable class does not require a noarg constructor. See the description of the behaviour of readObject() at docs.oracle.com/javase/7/docs/api/java/io/… . "Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object and initialized to zero (NULL)...". readObject is about to deserialize data into the fields of the object, so it would be wasteful to execute any constructor that would set an incorrect value into those fields. Just zeroing out the memory makes sense. Commented Feb 2, 2012 at 7:51
  • @ConcreteGannet As I didn't say a Serializable class requires a no-arg constructor, and as I did say that Serialization requires a no-arg constructor, which your quote also states, I am indeed correct. Commented Mar 11, 2014 at 18:29

9 Answers 9

2
answered Nov 8, 2010 at 15:11
Sign up to request clarification or add additional context in comments.

Comments

2

JBoss Serialization is a drop-in replacement for standard java serialization, which does not require you to implement java.io.Serializable. Other than that (and the fact that it's much faster), it's the same as the standard serialization mechanism (it even uses the same ObjectInput and ObjectOutput interfaces.

P.S. It has no dependency on other JBoss stuff, it's just one of the JBoss low-level libraries broken out as a separate project.

answered Nov 8, 2010 at 15:16

Comments

1

A horrific way to do it would be to build a parallel hierarchy of classes, each one standing in for one of the classes in the third-party hierarchy, each of which implements Externalizable, and writes itself by writing the appropriate fields from the third-party object.

I wouldn't, though.

answered Nov 9, 2010 at 9:51

Comments

0

I believe in some cases you can force serialization despite the Type's declarations. However there is inherent risk as the class may have fields that are not serializable, which will cause runtime exceptions.

I'm curious though as you get no-arg default constructors for free, unless you have written custom constructors. Also implmenting Serializable takes 30 seconds of find/replace.

Is there a reason you are trying to avoid these?

answered Nov 8, 2010 at 14:53

1 Comment

Yes, there are many objects of 3-rd party classes(to source of which I have no access) in tree I want to serialize.
0

Eishay Smith has done a benchmarking of Java serializers which includes some information about each one, although it doesn't say whether they use no-arg constructors (and in many cases, they don't even work with arbitrary objects, so the question is moot). That might be worth a look.

answered Nov 8, 2010 at 18:52

2 Comments

I can't see anything about constructors there.
You're right, there's nothing about that on that page - edited accordingly. Somewhere associated with that project, there's a more detailed description of each tool, which includes things like that. I can't find it, though.
0

And Databoard, it can serialize bean-style, record-style and immutable-style classes. You can also write own class-external binding.

answered Nov 9, 2010 at 22:02

Comments

0

There are a range of alternatives as other people have said. If you want to stick with standard Java serialization with writeObject and readObject, you could write your own adapter class that inherits from a third party class, implement Serializable on your own class, and override writeObject and readObject, in other words, implement custom serialization for your class.

answered Feb 2, 2012 at 8:01

Comments

0

...and implementation of Serializable?

Unfortunately not. All Serializable objects must implement java.io.Serializable. As for your first part of the question, you can use ObjectInputStream/ObjectOutputStream to serialize objects to byte array and vice versa.

The following example shows how to:

public static byte[] toByteArray(Object object) throws IOException {
 if (!isSerializable(object)) {
 throw new IOException("Object '" + object.getClass().getName() + "' is not serializable.");
 }
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ObjectOutputStream oos = null;
 try {
 oos = new ObjectOutputStream(baos);
 oos.writeObject(object);
 oos.flush();
 } finally {
 if (oos != null) {
 try {
 oos.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 logger.error("Closing of ObjectOutputStream failed.", e);
 }
 }
 }
 return baos.toByteArray();
}
public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
 Object object = null;
 ObjectInputStream ois = null; 
 try {
 ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
 object = ois.readObject();
 } finally {
 if (ois != null) {
 try {
 ois.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 logger.error("Closing of ObjectInputStream failed.", e);
 }
 }
 }
 return object;
}
answered Nov 8, 2010 at 16:00

2 Comments

Elite, this code demonstrates how to serialize and deserialize, but the crux of the question is how to make your class serializable, so that when an instance of your class is passed to the toByteArray method you wrote above, there will not be a NotSerializableException.
@Concrete Gannet, as I said on my post (please read), it's only by extending Serializable that your class is able to enter serialization.
0

See http://www.jguru.com/faq/view.jsp?EID=251942 explanation.

The only requirement on the constructor for a class that implements Serializable is that the first non-serializable superclass in its inheritence hierarchy must have a no-argument constructor.

  1. Serializeble without no-argument constructor as extends Object with with no-argument constructor

     public class MySerializableClass implements Serializable {
     public MySerializableClass (...)...
     }
    
  2. Serializeble without no-argument constructor as extends MyFirstClass with with no-argument constructor

     public class MyFirstClass {
     }
     public class MySecondClass extends MyFirstClass implements Serializable {
     public MySecondClass (...)...
     }
    
  3. NOT serializeble as MyFirstClass is not implement Serializable AND have not default constructor.

     public class MyFirstClass {
     public MyFirstClass (...)...
     }
     public class MySecondClass extends MyFirstClass implements Serializable {
     public MySecondClass (...)...
     }
    
answered Jul 28, 2016 at 20:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.