Kryo序列化及反序列化用法示例
程序员文章站
2024-02-26 09:57:28
kryo 是一个快速高效的 java 对象图形序列化框架,主要特点是性能、高效和易用。该项目用来序列化对象到文件、数据库或者网络。
 ...
kryo 是一个快速高效的 java 对象图形序列化框架,主要特点是性能、高效和易用。该项目用来序列化对象到文件、数据库或者网络。
代码地址:https://github.com/esotericsoftware/kryo
样例代码地址:
kryo的序列化及反序列速度很快,据说很多大公司都在用。我在把对象序列化都转换成了字符串形式,是为了把对象存储到缓存中。我们日常项目中使用的数据形式包括对象、list、set和map,因此主要把这几种类型的数据进行了序列化及反序列化,支持对象中包含list、set和map。
首先在项目的pom文件中引入:
<dependency> <groupid>com.esotericsoftware</groupid> <artifactid>kryo-shaded</artifactid> <version>3.0.3</version> </dependency>
代码kryotest:
package com.nettm.serializable; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.serializable; import java.util.arraylist; import java.util.hashmap; import java.util.hashset; import java.util.list; import java.util.map; import java.util.set; import org.apache.commons.codec.binary.base64; import org.testng.assert; import org.testng.annotations.aftertest; import org.testng.annotations.beforetest; import org.testng.annotations.test; import com.esotericsoftware.kryo.kryo; import com.esotericsoftware.kryo.io.input; import com.esotericsoftware.kryo.io.output; import com.esotericsoftware.kryo.serializers.collectionserializer; import com.esotericsoftware.kryo.serializers.javaserializer; import com.esotericsoftware.kryo.serializers.mapserializer; public class kryotest { private long time; @beforetest public void beforetest() { time = system.currenttimemillis(); } @aftertest public void aftertest() { system.out.println(system.currenttimemillis() - time); } @test(invocationcount = 1, threadpoolsize = 1) public void testobject() { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(1))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("张金"); val.setitemprice(89.02); val.setsort(10); string a = serializationobject(val); customitemdto newvalue = deserializationobject(a, customitemdto.class); assert.assertequals(val.getid(), newvalue.getid()); } @test(invocationcount = 1, threadpoolsize = 1) public void testlist() { list<customitemdto> lst = new arraylist<customitemdto>(); for (int i = 0; i < 10; i++) { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(i))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("张金"); val.setitemprice(89.02); val.setsort(10); lst.add(val); } string a = serializationlist(lst, customitemdto.class); list<customitemdto> newvalue = deserializationlist(a, customitemdto.class); assert.assertequals(lst.size(), newvalue.size()); } @test(invocationcount = 1, threadpoolsize = 1) public void testbean() { list<customcategorydto> lst = new arraylist<customcategorydto>(); for (int j = 0; j < 10; j++) { customcategorydto dto = new customcategorydto(); dto.setcategorycode("abcd_001"); dto.setcategoryname("呼吸系统"); for (int i = 0; i < 10; i++) { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(i))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("张金"); val.setitemprice(89.02); val.setsort(10); dto.getcustomitemlist().add(val); } for (int i = 0; i < 10; i++) { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(i))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("张金"); val.setitemprice(89.02); val.setsort(10); dto.getcustomitemset().add(val); } for (int i = 0; i < 10; i++) { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(i))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("张金"); val.setitemprice(89.02); val.setsort(10); dto.getcustomitemmap().put(string.valueof(i), val); } lst.add(dto); } string a = serializationlist(lst, customcategorydto.class); list<customcategorydto> newvalue = deserializationlist(a, customcategorydto.class); assert.assertequals(lst.size(), newvalue.size()); } @test(invocationcount = 1, threadpoolsize = 1) public void testmap() { map<string, customitemdto> map = new hashmap<string, customitemdto>(); for (int i = 0; i < 10; i++) { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(i))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("张金"); val.setitemprice(89.02); val.setsort(10); map.put(new objectid().tostring(), val); } string a = serializationmap(map, customitemdto.class); map<string, customitemdto> newvalue = deserializationmap(a, customitemdto.class); assert.assertequals(map.size(), newvalue.size()); } @test(invocationcount = 1, threadpoolsize = 1) public void testset() { set<customitemdto> set = new hashset<customitemdto>(); for (int i = 0; i < 10; i++) { customitemdto val = new customitemdto(); val.setid(long.parselong(string.valueof(i))); val.setitemcode(""); val.setitemdespositprice(32.45); val.setitemmemo(null); val.setitemname("金星"); val.setitemprice(89.02); val.setsort(10); set.add(val); } string a = serializationset(set, customitemdto.class); set<customitemdto> newvalue = deserializationset(a, customitemdto.class); assert.assertequals(set.size(), newvalue.size()); } private <t extends serializable> string serializationobject(t obj) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.register(obj.getclass(), new javaserializer()); bytearrayoutputstream baos = new bytearrayoutputstream(); output output = new output(baos); kryo.writeclassandobject(output, obj); output.flush(); output.close(); byte[] b = baos.tobytearray(); try { baos.flush(); baos.close(); } catch (ioexception e) { e.printstacktrace(); } return new string(new base64().encode(b)); } @suppresswarnings("unchecked") private <t extends serializable> t deserializationobject(string obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.register(clazz, new javaserializer()); bytearrayinputstream bais = new bytearrayinputstream( new base64().decode(obj)); input input = new input(bais); return (t) kryo.readclassandobject(input); } private <t extends serializable> string serializationlist(list<t> obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.setregistrationrequired(true); collectionserializer serializer = new collectionserializer(); serializer.setelementclass(clazz, new javaserializer()); serializer.setelementscanbenull(false); kryo.register(clazz, new javaserializer()); kryo.register(arraylist.class, serializer); bytearrayoutputstream baos = new bytearrayoutputstream(); output output = new output(baos); kryo.writeobject(output, obj); output.flush(); output.close(); byte[] b = baos.tobytearray(); try { baos.flush(); baos.close(); } catch (ioexception e) { e.printstacktrace(); } return new string(new base64().encode(b)); } @suppresswarnings("unchecked") private <t extends serializable> list<t> deserializationlist(string obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.setregistrationrequired(true); collectionserializer serializer = new collectionserializer(); serializer.setelementclass(clazz, new javaserializer()); serializer.setelementscanbenull(false); kryo.register(clazz, new javaserializer()); kryo.register(arraylist.class, serializer); bytearrayinputstream bais = new bytearrayinputstream( new base64().decode(obj)); input input = new input(bais); return (list<t>) kryo.readobject(input, arraylist.class, serializer); } private <t extends serializable> string serializationmap( map<string, t> obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.setregistrationrequired(true); mapserializer serializer = new mapserializer(); serializer.setkeyclass(string.class, new javaserializer()); serializer.setkeyscanbenull(false); serializer.setvalueclass(clazz, new javaserializer()); serializer.setvaluescanbenull(true); kryo.register(clazz, new javaserializer()); kryo.register(hashmap.class, serializer); bytearrayoutputstream baos = new bytearrayoutputstream(); output output = new output(baos); kryo.writeobject(output, obj); output.flush(); output.close(); byte[] b = baos.tobytearray(); try { baos.flush(); baos.close(); } catch (ioexception e) { e.printstacktrace(); } return new string(new base64().encode(b)); } @suppresswarnings("unchecked") private <t extends serializable> map<string, t> deserializationmap( string obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.setregistrationrequired(true); mapserializer serializer = new mapserializer(); serializer.setkeyclass(string.class, new javaserializer()); serializer.setkeyscanbenull(false); serializer.setvalueclass(clazz, new javaserializer()); serializer.setvaluescanbenull(true); kryo.register(clazz, new javaserializer()); kryo.register(hashmap.class, serializer); bytearrayinputstream bais = new bytearrayinputstream( new base64().decode(obj)); input input = new input(bais); return (map<string, t>) kryo.readobject(input, hashmap.class, serializer); } public static <t extends serializable> string serializationset(set<t> obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.setregistrationrequired(true); collectionserializer serializer = new collectionserializer(); serializer.setelementclass(clazz, new javaserializer()); serializer.setelementscanbenull(false); kryo.register(clazz, new javaserializer()); kryo.register(hashset.class, serializer); bytearrayoutputstream baos = new bytearrayoutputstream(); output output = new output(baos); kryo.writeobject(output, obj); output.flush(); output.close(); byte[] b = baos.tobytearray(); try { baos.flush(); baos.close(); } catch (ioexception e) { e.printstacktrace(); } return new string(new base64().encode(b)); } @suppresswarnings("unchecked") public static <t extends serializable> set<t> deserializationset( string obj, class<t> clazz) { kryo kryo = new kryo(); kryo.setreferences(false); kryo.setregistrationrequired(true); collectionserializer serializer = new collectionserializer(); serializer.setelementclass(clazz, new javaserializer()); serializer.setelementscanbenull(false); kryo.register(clazz, new javaserializer()); kryo.register(hashset.class, serializer); bytearrayinputstream bais = new bytearrayinputstream( new base64().decode(obj)); input input = new input(bais); return (set<t>) kryo.readobject(input, hashset.class, serializer); } }
总结
以上就是本文关于kryo序列化及反序列化用法示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:java原生序列化和kryo序列化性能实例对比分析 、 kryo框架使用方法代码示例等,有什么问题可以随时留言,小编一定竭尽所能,答您所问。
上一篇: 备份mariadb数据库
下一篇: Yii2主题(Theme)用法详解