Created
June 11, 2015 18:52
-
-
Save franzejr/c02a900142b7ada98bc4 to your computer and use it in GitHub Desktop.
Serialize java objects by using Apache Common langs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.io.Serializable; | |
public class SampleTest implements Serializable { | |
int age; | |
String name; | |
Object o; | |
public SampleTest(String name, int age, Object o) { | |
this.name = name; | |
this.age = age; | |
this.o = o; | |
} | |
@Override | |
public String toString() { | |
// TODO Auto-generated method stub | |
return "[age= " + String.valueOf(age) + " name= "+ name + " object= "+ o + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.Arrays; | |
import org.apache.commons.lang3.SerializationUtils; | |
import test.SampleTest; | |
public class TestApacheCommonLangs { | |
public static void main(String[] args){ | |
Object[] o = {1,2,3}; | |
SampleTest sampleTest = new SampleTest("Test", 33, o); | |
byte[] data = SerializationUtils.serialize(sampleTest); | |
System.out.println("SERIALIZED DATA"); | |
System.out.println(Arrays.toString(data)); | |
SampleTest deserializedObject = (SampleTest) SerializationUtils.deserialize(data); | |
System.out.println("AFTER\n\n\n"); | |
System.out.println(deserializedObject.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment