Vladimir
Vladimir

Reputation: 13163

Java serialization library without need of no-arg constructors and implementation of Serializable

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

Upvotes: 5

Views: 3292

Answers (9)

Grigory Kislin
Grigory Kislin

Reputation: 18020

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 (...)...
     }
    

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89189

...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;
}

Upvotes: 0

Concrete Gannet
Concrete Gannet

Reputation: 580

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.

Upvotes: 0

Tag
Tag

Reputation: 296

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

Upvotes: 0

Tom Anderson
Tom Anderson

Reputation: 47233

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.

Upvotes: 1

Tom Anderson
Tom Anderson

Reputation: 47233

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.

Upvotes: 0

skaffman
skaffman

Reputation: 403551

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.

Upvotes: 2

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

Look at XStream, JSX or Google Protocol Buffers.

Upvotes: 2

Eddie
Eddie

Reputation: 10148

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?

Upvotes: 0

Related Questions