Kryo 是一个快速高效的 Java 对象图形序列化框架,主要特点是性能、高效和易用。该项目用来序列化对象到文件、数据库或者网络。
代码地址:https://github.com/EsotericSoftware/kryo
样例代码地址:https://github.com/nettm/public
Kryo的序列化及反序列速度很快,据说很多大公司都在用。我在把对象序列化都转换成了字符串形式,是为了把对象存储到缓存中。我们日常项目中使用的数据形式包括对象、List、Set和Map,因此主要把这几种类型的数据进行了序列化及反序列化,支持对象中包含List、Set和Map。
首先在项目的pom文件中引入:
1
2
3
4
5
|
<dependency> <groupId>com.esotericsoftware</groupId> <artifactId>kryo-shaded</artifactId> <version> 3.0 . 3 </version> </dependency> |
代码KryoTest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
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序列化及反序列化用法示例的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编一定竭尽所能,答您所问。
原文链接:http://nettm.blog.51cto.com/4841905/1702453