编程语言
首页 > 编程语言> > java – kryo序列化对非序列化类和类的工作是否具有非可序列化属性?

java – kryo序列化对非序列化类和类的工作是否具有非可序列化属性?

作者:互联网

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

解决方法:

如果它没有实现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,serialization,kryo
来源: https://codeday.me/bug/20190702/1353514.html