public interface ISerializer { T byte[] serializer(T obj); T T deSerializer(byte[] data,ClassT clazz);} //java实现public class JavaSerializer implements ISerializer { @Override public T byte[] serializer(T obj) { ObjectOutputStream object
public interface ISerializer { <T> byte[] serializer(T obj); <T> T deSerializer(byte[] data,Class<T> clazz); }
//java实现 public class JavaSerializer implements ISerializer { @Override public <T> byte[] serializer(T obj) { ObjectOutputStream objectOutputStream=null; try { objectOutputStream=new ObjectOutputStream(new FileOutputStream(new File("user"))); objectOutputStream.writeObject(obj); } catch (IOException e) { e.printStackTrace(); }finally { if(objectOutputStream!=null){ try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override public <T> T deSerializer(byte[] data, Class<T> clazz) { ObjectInputStream objectInputStream=null; try { objectInputStream=new ObjectInputStream(new FileInputStream(new File("user"))); return (T)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if(objectInputStream!=null){ try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } }
fast
public class FastjsonSerializer implements ISerializer{ @Override public <T> byte[] serializer(T obj) { return JSON.toJSONString(obj).getBytes(); } @Override public <T> T deSerializer(byte[] data, Class<T> clazz) { return JSON.parseObject(new String(data),clazz); } }
XML
public class XmlSerializer implements ISerializer{ XStream xStream=new XStream(new DomDriver()); @Override public <T> byte[] serializer(T obj) { return xStream.toXML(obj).getBytes(); } @Override public <T> T deSerializer(byte[] data, Class<T> clazz) { return (T)xStream.fromXML(new String (data)); } }
protobuf实现较为麻烦,需要先生成对应的类。(可以百度如何使用protobuf进行序列化)
demo:
public class SuperClass implements Serializable { String sex; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
public class User extends SuperClass { public static int num=5; private String name; private int age; private transient String hobby; //序列化对象 private void writeObject(ObjectOutputStream objectOutputStream) throws IOException { objectOutputStream.defaultWriteObject(); objectOutputStream.writeObject(hobby); } //反序列化 private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { objectInputStream.defaultReadObject(); hobby=(String)objectInputStream.readObject(); } 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 getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public String toString() { return "User{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", hobby=‘" + hobby + ‘\‘‘ + ‘}‘; } }
public class App { public static void main( String[] args ) { ISerializer iSerializer=new FastjsonSerializer(); User user=new User(); user.setAge(18); user.setName("Mic"); user.setHobby("菲菲"); user.setSex("男"); byte[] rs=iSerializer.serializer(user); System.out.println(new String(rs)); User user1=iSerializer.deSerializer(rs,User.class); System.out.println(user1+"->"+user1.getSex()); } }
public class Email implements Serializable { private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
public class Person implements Cloneable,Serializable { private String name; private Email email; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Email getEmail() { return email; } public void setEmail(Email email) { this.email = email; } @Override protected Person clone() throws CloneNotSupportedException { return (Person)super.clone(); } public Person deepClone() throws IOException, ClassNotFoundException { ByteArrayOutputStream bos=new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream= new ObjectOutputStream(bos); objectOutputStream.writeObject(this); ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream objectInputStream=new ObjectInputStream(bis); return (Person) objectInputStream.readObject(); } }
public class CloneDemo { public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException { Email email=new Email(); email.setContent("今天晚上20:00有课程"); Person p1=new Person("Mic"); p1.setEmail(email); // Person p2=p1.clone(); Person p2=p1.deepClone(); p2.setName("黑白"); p2.getEmail().setContent("今天晚上是20:30上课"); System.out.println(p1.getName()+"->"+p1.getEmail().getContent()); System.out.println(p2.getName()+"->"+p2.getEmail().getContent()); } }
public class StoreRuleDemo { public static void main(String[] args) throws IOException { ObjectOutputStream outputStream= new ObjectOutputStream(new FileOutputStream(new File("user"))); User user=new User(); user.setAge(18); user.setName("Mic"); user.setHobby("菲菲"); user.setSex("男"); outputStream.flush(); outputStream.writeObject(user); System.out.println(new File("user").length()); outputStream.writeObject(user); outputStream.flush(); outputStream.close(); System.out.println(new File("user").length()); } }