Objective:
Java serialization is an important issue in the Java technology architecture, and the significance of serialization is in the Exchange and storage of information , usually related to Io, persistence, RMI technology (eg: some ORM frameworks require a persistent object type to implement the Serializable interface).
This article will provide a comparison of the Java self-serialization mechanism and the serialization of Protostuff (just as a data format), from the contents and characteristics of the serialization.
Conclusion: 1,java serializes an object without the need for a property's get set method or other unrelated serialization methods (such as Readobject,writeobject is a built-in serialization method), serialization does not require the Get Set method support, Deserialization is a means of constructing an object .
The type must exactly match (full path class name + serialization ID) when 2,java serialization.
3,Protostuff does not require type matching when deserializing, such as package name, class name, or even field name, it simply needs to serialize type A and deserialize type B's field type convertible (for example, int can be converted to long) .
Java.io.Serializable
Identifying an object requires serialization, and the object type needs to implement the Serializable interface. For the understanding of serialization, you can refer to the IBM Community's article, "Advanced knowledge of Java serialization," This article directly takes the conclusion of the document.
1, the serialization ID of the serialized type and the deserialized type must be the same (when remote information is exchanged).
2, the static data is not serialized, and the Transient keyword-Modified field is not serialized.
3, objects that store the same value two times when the object is serialized are optimized (the second object write stores only references).
Serialization Technology
The purpose of serialization is to store and exchange data, and according to this concept, Xml,json is also a serialized technique, except that they seem to be an easy-to-distinguish serialization technique, and the serialization techniques similar to Protostuff or Java itself seem somewhat cryptic.
So what is the difference between Java serialization technology and other serialization technologies? The following compares the serialization techniques of Java itself with Protostuff and makes a peep at one or two.
Protostuff
Official website: https://code.google.com/p/protostuff/.
Protostuff is a Java version based on the famous Google Protobuff technology, the direct use of native Protobuff is a pre-compilation process that requires data structures, The. proto format configuration file needs to be written and translated into the target language code by the tools provided by Protobuf, while Protostuff dynamically supports the Protobuff precompilation process.
The following example code is used to prove the conclusion of the preamble
1, the entity used for testing (the attribute is represented in brackets)
org.wit.ff.testmodel.serializableusera[string name, int age];
org.wit.ff.testmodel.serializableuserb[string name, int age];
org.wit.ff.testmodel.ch.serializableusera[string name, int age];
Org.wit.ff.testmodel.ch.serializableuserc[string Nonename, double noneage];
org.wit.ff.testmodel.ch1.serializableusera[string name, int age, String firstName];
org.wit.ff.testmodel.ch1.serializableuserb[string nonename, int noneage];
org.wit.ff.testmodel.ch1.serializableuserc[string nonename, int noneage];
2, test case:
PackageOrg.wit.ff;Import Staticorg.junit.Assert.assertEquals;Import StaticOrg.junit.Assert.assertFalse;Import StaticOrg.junit.Assert.assertNotNull;Import Staticorg.junit.Assert.assertTrue;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importorg.junit.Test;ImportOrg.wit.ff.testmodel.SerializableUserA;ImportOrg.wit.ff.testmodel.SerializableUserB;ImportOrg.wit.ff.util.ProtoStuffSerializerUtil;/*** * <pre> * Java Native serialization mechanism. * Independent of method. * * safe. * * * * * * * * *@authorF.fang *@version$Id: Javaserializabledemo.java, v 0.1 October 29, 2014 AM 12:48:11 f.fang Exp $*/ Public classJavaserializabledemo {/*** * <pre> * Java self-serialization mechanism: detects the contents of object serialization. * Serialization is independent of the method, and the assignment of the property does not pass the method. * </pre> **/@Test Public voidtest1 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (1024); Try{objectoutputstream Oos=NewObjectOutputStream (BAOs); Oos.writeobject (UserA); Oos.close (); } Catch(IOException e) {e.printstacktrace (); } byte[] Userabytes =Baos.tobytearray (); Bytearrayinputstream Bais=NewBytearrayinputstream (userabytes); Try{ObjectInputStream Ois=NewObjectInputStream (Bais); Serializableusera Useras=(Serializableusera) ois.readobject (); //System.out.println (Useras);assertequals (Usera,useras); } Catch(IOException e) {e.printstacktrace (); Assertfalse (true); } Catch(ClassNotFoundException e) {e.printstacktrace (); Assertfalse (true); } } /*** * <pre> * Java self-serialization mechanism: Different classes are serialized and deserialized. * (includes package and class name differ) * Java.lang.ClassCastException. * </pre> **/@Test Public voidtest2 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (1024); Try{objectoutputstream Oos=NewObjectOutputStream (BAOs); Oos.writeobject (UserA); } Catch(IOException e) {e.printstacktrace (); } byte[] Userabytes =Baos.tobytearray (); Bytearrayinputstream Bais=NewBytearrayinputstream (userabytes); Try{ObjectInputStream Ois=NewObjectInputStream (Bais); Org.wit.ff.testmodel.ch.SerializableUserA userA1=(Org.wit.ff.testmodel.ch.SerializableUserA) ois. ReadObject (); System.out.println (userA1); } Catch(IOException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(java.lang.ClassCastException e) {e.printstacktrace (); Asserttrue (true); } } /*** * <pre> * use Protobuff to perform serialization. * </pre> **/@Test Public voidtest3 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); byte[] arr =protostuffserializerutil.serialize (UserA); Serializableusera Useras= Protostuffserializerutil.deserialize (arr, Serializableusera.class); //System.out.println (Useras);Assertnotnull (Useras); Assertequals (Usera.getage (), Useras.getage ()); Assertequals (Usera.getname (), Useras.getname ()); } /*** * <pre> * use Protobuff for serialization. * Serialization of classes and deserialization of the class package path is different. * Deserialization succeeded. * </pre> **/@Test Public voidtest4 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); byte[] arr =protostuffserializerutil.serialize (UserA); Org.wit.ff.testmodel.ch.SerializableUserA Useras= Protostuffserializerutil.deserialize (arr, Org.wit.ff.testmodel.ch.SerializableUserA.class); //System.out.println (Useras);Assertequals (18, Useras.getage ()); Assertequals ("Nobody", Useras.getname ()); } /*** * <pre> * use Protobuff for serialization. * The class and deserialization class properties are the same when serializing, and the class names are different. * Deserialization succeeded. * </pre> **/@Test Public voidTest5 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); byte[] arr =protostuffserializerutil.serialize (UserA); Serializableuserb Userbs= Protostuffserializerutil.deserialize (arr, Serializableuserb.class); //System.out.println (userbs);Assertequals (18, Userbs.getage ()); Assertequals ("Nobody", Userbs.getname ()); } /*** * <pre> * use Protobuff for serialization. * The properties of the class at the time of serialization are included in the deserialized class properties. * Deserialization succeeded. * </pre> **/@Test Public voidTest6 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); byte[] arr =protostuffserializerutil.serialize (UserA); Org.wit.ff.testmodel.ch1.SerializableUserA Useras= Protostuffserializerutil.deserialize (arr, Org.wit.ff.testmodel.ch1.SerializableUserA.class); //System.out.println (Useras);Assertequals (18, Useras.getage ()); Assertequals ("Nobody", Useras.getname ()); } /*** * <pre> * use Protobuff for serialization. * The classes at serialization are completely different from the deserialized classes, and the property names are not the same, and the types are identical. * Deserialization succeeded. * </pre> **/@Test Public voidtest7 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); byte[] arr =protostuffserializerutil.serialize (UserA); Org.wit.ff.testmodel.ch1.SerializableUserB Userbs= Protostuffserializerutil.deserialize (arr, Org.wit.ff.testmodel.ch1.SerializableUserB.class); System.out.println (Userbs); Assertequals ("Nobody", Userbs.getnonename ()); Assertequals (18, Userbs.getnoneage ()); } /*** * <pre> * use Protobuff for serialization. * Classes at serialization and deserialization classes are completely different and property names are not the same. * Each attribute type does not match. * For Attributes * If the serialization type is int 8 * The deserialization type is long * serialization succeeded. * Deserialization type is double * deserialization is unsuccessful. * EG1: * serializableusera:age int,name String * serializableuserc:noneage double, nonename string * deserialization not Serializableuserc:noneage Long, Nonename String * deserialized successfully. * </pre> **/@Test Public voidTest8 () {Serializableusera UserA=NewSerializableusera ("Nobody", 18); byte[] arr =protostuffserializerutil.serialize (UserA); Org.wit.ff.testmodel.ch1.SerializableUserC Usercs= Protostuffserializerutil.deserialize (arr, Org.wit.ff.testmodel.ch1.SerializableUserC.class); System.out.println (USERCS); Assertnotnull (USERCS); //An exception occurred when the property type does not match! Try{protostuffserializerutil.deserialize (arr, Org.wit.ff.testmodel.ch.SerializableUserC.class); }Catch(Exception e) {e.printstacktrace (); Asserttrue (true); } }}
Java Serialization Technology and Protobuff