Skip to content

Instantly share code, notes, and snippets.

@franzejr
Created June 11, 2015 18:52
Show Gist options
  • Save franzejr/c02a900142b7ada98bc4 to your computer and use it in GitHub Desktop.
Save franzejr/c02a900142b7ada98bc4 to your computer and use it in GitHub Desktop.
Serialize java objects by using Apache Common langs
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 + "]";
}
}
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