The serialization and deserialization of objects is actually to save the state of the object, usually to a file, but in fact, it is more commonly used to serialize the object into a string and save it to the database, and then read the object when it is required. The following deserializes a string to an object.
A serializable class must implement the Serializabe interface. It should be noted here that if a property does not need to be serialized, the transient keyword can be added.
Here is a class that can be serialized:
package demo1; import java.io.Serializable; public class Person implements Serializable{ private static final long serialVersionUID = 1L; private int id; private String name; private int age; private transient String sex; public Person(int id, String name, int age,String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return this.id+" "+this.name+" "+this.age+" "+this.sex; } }
Here is the class that encapsulates serialization:
package demo1; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtils { public static String serialize(Object obj) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream; objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj); String string = byteArrayOutputStream.toString("ISO-8859-1"); objectOutputStream.close(); byteArrayOutputStream.close(); return string; } public static Object serializeToObject(String str) throws IOException, ClassNotFoundException { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes("ISO-8859-1")); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); Object object = objectInputStream.readObject(); objectInputStream.close(); byteArrayInputStream.close(); return object; } }
The following is a simple process of using redis to serialize the object, save it to the database, and then retrieve the data from the database for deserialization:
public class DemoAtomic { public static void main(String [] args) throws IOException, ClassNotFoundException { Jedis jedis = new Jedis("localhost",6379); jedis.setex("test1", 60*60*24,SerializeUtils.serialize(new Person(1, "测试1", 21,"男"))); jedis.setex("test2", 60*60*24,SerializeUtils.serialize(new Person(2, "测试2", 21,"女"))); jedis.setex("test3", 60*60*24,SerializeUtils.serialize(new Person(3, "测试3", 21,"男"))); jedis.setex("test4", 60*60*24,SerializeUtils.serialize(new Person(4, "测试4", 21,"女"))); Set<String> keys = jedis.keys("*"); for (String s:keys) { System.out.println(s); Person person = (Person)SerializeUtils.serializeToObject(jedis.get(s)); System.out.println(person.toString()); } } }
The following is the print data:
test4 4 test4 21 null test2 2 test 2 21 null test3 3 test 3 21 null test1 1 test 1 21 null