2016-07-21 158 views
3

我想使用Kryo庫將任何給定的對象轉換爲byteArray並存儲在數據存儲或隊列中供以後使用。但是是否有可能序列化任何給定的對象,或者只有實現可序列化接口的對象才能被轉換。kryo序列化對不可序列化的類和類是否有不可序列化的屬性?

+0

如果它沒有實現java.io.Serialiable,那麼應該可以序列化一個bean,參見例如測試類BeanSerializerTest。 –

+0

@ John-Donn請給出答案並提供指定示例的鏈接。 –

+0

@MikePone添加了答案。 –

回答

1

如果它沒有實現java.io.Serialiable並且/或者它的屬性沒有實現Serializable(我用Kryo 2.10運行這個例子,那麼可以用Kryo序列化/反序列化一個bean;限制,即無參數的構造函數已被明確在此定義,因爲非默認的構造函數存在):

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 

import com.esotericsoftware.kryo.Kryo; 
import com.esotericsoftware.kryo.io.Input; 
import com.esotericsoftware.kryo.io.Output; 

public class KryoSerializerExample { 

    public static void main(String[] args) { 
     KryoHelper kryoHelper = new KryoHelper(); 
     ClassNotImplementingSerializable obj1 = new ClassNotImplementingSerializable(123, 456, "789"); 
     ClassNotImplementingSerializable obj2 = (ClassNotImplementingSerializable) kryoHelper.fromBytes(kryoHelper.toBytes(obj1)); 
     if (obj1.equals(obj2)) { 
      System.out.println("the object and its clone are equal as expected"); 
     } else { 
      System.out.println("the object and its clone are not equal, something went wrong"); 
     } 
    } 

    public static class KryoHelper { 

     Kryo kryo; 

     public KryoHelper() { 
      super(); 
      kryo=new Kryo(); 
     } 

     public byte[] toBytes(Object obj){ 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      try (Output output = new Output(baos)) { 
       kryo.writeClassAndObject(output, obj); 
      } 

      byte[] bytes = baos.toByteArray(); 
      return bytes; 
     } 

     public Object fromBytes(byte[] bytes){ 
      Object retrievedObject; 
      try (Input input = new Input(new ByteArrayInputStream(bytes))){ 
       retrievedObject = kryo.readClassAndObject(input); 
      } 

      return retrievedObject; 
     } 

    } 

    public static class ClassNotImplementingSerializable { 
     private int a; 
     private ClassNotImplementingSerializable2 b; 

     /** 
     * no-arg constructor is necessary 
     */ 
     public ClassNotImplementingSerializable() { 
     } 
     public ClassNotImplementingSerializable(int a, int b, String c) { 
      this.a = a; 
      this.b = new ClassNotImplementingSerializable2(b,c); 
     } 
     public int getA() { 
      return a; 
     } 
     public void setA(int a) { 
      this.a = a; 
     } 
     public ClassNotImplementingSerializable2 getB() { 
      return b; 
     } 
     public void setB(ClassNotImplementingSerializable2 b) { 
      this.b = b; 
     } 
     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + a; 
      result = prime * result + ((b == null) ? 0 : b.hashCode()); 
      return result; 
     } 
     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (getClass() != obj.getClass()) 
       return false; 
      ClassNotImplementingSerializable other = (ClassNotImplementingSerializable) obj; 
      if (a != other.a) 
       return false; 
      if (b == null) { 
       if (other.b != null) 
        return false; 
      } else if (!b.equals(other.b)) 
       return false; 
      return true; 
     } 



    } 

    public static class ClassNotImplementingSerializable2 { 
     private int a; 
     private String b; 

     /** 
     * no-arg constructor is necessary 
     */ 
     public ClassNotImplementingSerializable2() { 
     } 
     public ClassNotImplementingSerializable2(int a, String b) { 
      this.a = a; 
      this.b = b; 
     } 
     public int getA() { 
      return a; 
     } 
     public void setA(int a) { 
      this.a = a; 
     } 
     public String getB() { 
      return b; 
     } 
     public void setB(String b) { 
      this.b = b; 
     } 
     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + a; 
      result = prime * result + ((b == null) ? 0 : b.hashCode()); 
      return result; 
     } 
     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (getClass() != obj.getClass()) 
       return false; 
      ClassNotImplementingSerializable2 other = (ClassNotImplementingSerializable2) obj; 
      if (a != other.a) 
       return false; 
      if (b == null) { 
       if (other.b != null) 
        return false; 
      } else if (!b.equals(other.b)) 
       return false; 
      return true; 
     } 
    } 
} 

可能有問題,與一些特定的字段類型,如java.sql.Timestamp例如(周圍有這樣一個辦法,雖然),當然還有java.lang.Thread等等。

相關問題