gpt4 book ai didi

java - kryo 序列化是否适用于不可序列化的类并且类具有不可序列化的属性?

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:40 25 4
gpt4 key购买 nike

我正在尝试使用 Kryo 库将任何给定对象转换为 byteArray 并存储在数据存储或队列中供以后使用。但是是否可以序列化任何给定的对象,或者只能转换实现可序列化接口(interface)的对象。

最佳答案

如果 bean 没有实现 java.io.Serialiable 和/或它的属性没有实现 Serializable,也可以用 Kryo 序列化/反序列化一个 bean(我用 Kryo 2.10 运行这个例子;有限制因为存在非默认构造函数,所以不必在此处显式定义参数构造函数):

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 之类的。

关于java - kryo 序列化是否适用于不可序列化的类并且类具有不可序列化的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com