开发者

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

I开发者_高级运维s there is any way in java-world to serialize without need of no-arg constructors and implementation of Serializable?


Look at XStream, JSX or Google Protocol Buffers.


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.


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.


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?


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.


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


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜