From 3946f7d8767bf99a291191560fe21742ebcbb7bd Mon Sep 17 00:00:00 2001 From: "yang.yu" <1447829379@qq.com> Date: Mon, 12 Aug 2019 17:56:41 +0800 Subject: [PATCH 1/5] try to give a default registration if missing Class structure when reading --- .../kryo/serializers/CompatibleFieldSerializer.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java index 7fc928a2b..5b402d761 100644 --- a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java +++ b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java @@ -135,9 +135,14 @@ public T read (Kryo kryo, Input input, Class type) { } catch (KryoException ex) { String message = "Unable to read unknown data (unknown type). (" + getType().getName() + "#" + cachedField + ")"; if (!chunked) throw new KryoException(message, ex); - if (DEBUG) debug("kryo", message, ex); - inputChunked.nextChunk(); - continue; + try { + //try to give a default registration + registration = kryo.getRegistration(Object.class); + } catch (Exception e) { + if(DEBUG) trace("Unable to get default registration: " + e.getMessage()); + inputChunked.nextChunk(); + continue; + } } if (registration == null) { if (chunked) inputChunked.nextChunk(); From 3737c305cde54a3e9bd1b99497531544597f6fe5 Mon Sep 17 00:00:00 2001 From: maxisvest <1447829379@qq.com> Date: Sun, 17 May 2020 17:01:21 +0800 Subject: [PATCH 2/5] fix code format --- .../serializers/CompatibleFieldSerializer.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java index 5b402d761..80ddd4a76 100644 --- a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java +++ b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java @@ -135,14 +135,14 @@ public T read (Kryo kryo, Input input, Class type) { } catch (KryoException ex) { String message = "Unable to read unknown data (unknown type). (" + getType().getName() + "#" + cachedField + ")"; if (!chunked) throw new KryoException(message, ex); - try { - //try to give a default registration - registration = kryo.getRegistration(Object.class); - } catch (Exception e) { - if(DEBUG) trace("Unable to get default registration: " + e.getMessage()); - inputChunked.nextChunk(); - continue; - } + try { + //try to give a default registration + registration = kryo.getRegistration(Object.class); + } catch (Exception e) { + if(DEBUG) trace("Unable to get default registration: " + e.getMessage()); + inputChunked.nextChunk(); + continue; + } } if (registration == null) { if (chunked) inputChunked.nextChunk(); From 442d7fcfab6269025bd9c1a5e4d57f779c5a6d56 Mon Sep 17 00:00:00 2001 From: "yang.yu" <1447829379@qq.com> Date: Tue, 23 Jun 2020 22:05:31 +0800 Subject: [PATCH 3/5] Add test case --- .../kryo/serializers/ClassStructureTest.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java diff --git a/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java b/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java new file mode 100644 index 000000000..f7c06da41 --- /dev/null +++ b/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java @@ -0,0 +1,101 @@ +package com.esotericsoftware.kryo.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoTestCase; +import com.esotericsoftware.kryo.SerializerFactory; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * You need run encode() first. And then run decode() follow instruction. + * + * Create by maxisvest + * 2019/7/25 14:20 + */ +public class ClassStructureTest extends KryoTestCase { + + public static final Kryo kryo = new Kryo(); + + @Before + public void setUp () throws Exception { + super.setUp(); + SerializerFactory.CompatibleFieldSerializerFactory compatibleFieldSerializerFactory = new SerializerFactory.CompatibleFieldSerializerFactory(); + CompatibleFieldSerializer.CompatibleFieldSerializerConfig config = compatibleFieldSerializerFactory.getConfig(); + kryo.setDefaultSerializer(compatibleFieldSerializerFactory); + kryo.setRegistrationRequired(false); + + //this case use for enable references and chunked encoding + kryo.setReferences(true); + config.setChunkedEncoding(true); + } + + /** + * Run this method first, to write a full version instance of class DataBean. + * + * DataBean include SubData and SubData include the String message. + * If enable kryo.references function, String message in SubData will write first and this String in + * DataBean will write as a reference. + * + * Imagine this procedure is the server want to transfer data to client. + * @throws IOException + */ + @Test + public void encode() throws IOException { + String message = "Hello Kryo!"; + + SubData subData = new SubData(); //delete on decode test + subData.message = message; //delete on decode test + + DataBean dataBean = new DataBean(); + dataBean.subData = subData; //delete on decode test + dataBean.message = message; + + Output output = new Output(new FileOutputStream("file_y.bin")); + kryo.writeObject(output, dataBean); + output.close(); + } + + /** + * Before run this method, you need delete the line and Class SubData marked as "delete on decode test". The Class DataBean only + * have the field String message. + * + * Imagine this is the client want to deserialize the data come from server. But client use the old version of Class DataBean. + * + * Normally, decode() will throw an Exception because it can not find the referenced value of String message. The String message + * referenced value is in the chunked input but it has been skipped. + * Use PR #686 will solve this problem and recover the String message in DataBean. Therefore run decode() will pass now. + * + * @throws IOException + */ + @Test + public void decode() throws IOException { + File file = new File("file_y.bin"); + Input input = new Input(new FileInputStream(file)); + DataBean dataBean = kryo.readObject(input, DataBean.class); + file.delete(); + input.close(); + } + + + + static public class DataBean { + SubData subData; //delete on decode test + String message; + } + + //delete on decode test + static public class SubData { + String message; + } + + + + +} From 44a2b0911dba012da1f3723d596bc585432768e4 Mon Sep 17 00:00:00 2001 From: "yang.yu" <1447829379@qq.com> Date: Tue, 23 Jun 2020 22:17:52 +0800 Subject: [PATCH 4/5] fix ci --- .../kryo/serializers/ClassStructureTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java b/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java index f7c06da41..7880fddab 100644 --- a/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java +++ b/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java @@ -77,10 +77,12 @@ public void encode() throws IOException { @Test public void decode() throws IOException { File file = new File("file_y.bin"); - Input input = new Input(new FileInputStream(file)); - DataBean dataBean = kryo.readObject(input, DataBean.class); - file.delete(); - input.close(); + if (file.exists()) { + Input input = new Input(new FileInputStream(file)); + DataBean dataBean = kryo.readObject(input, DataBean.class); + file.delete(); + input.close(); + } } From 57dd52a0f05d562e5d54c16fe58f7d2e397ac563 Mon Sep 17 00:00:00 2001 From: "yang.yu" <1447829379@qq.com> Date: Sun, 12 Jul 2020 23:18:21 +0800 Subject: [PATCH 5/5] rename field name in test case --- .../esotericsoftware/kryo/serializers/ClassStructureTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java b/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java index 7880fddab..6289e4a87 100644 --- a/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java +++ b/test/com/esotericsoftware/kryo/serializers/ClassStructureTest.java @@ -54,7 +54,7 @@ public void encode() throws IOException { subData.message = message; //delete on decode test DataBean dataBean = new DataBean(); - dataBean.subData = subData; //delete on decode test + dataBean.aSubData = subData; //delete on decode test dataBean.message = message; Output output = new Output(new FileOutputStream("file_y.bin")); @@ -88,7 +88,7 @@ public void decode() throws IOException { static public class DataBean { - SubData subData; //delete on decode test + SubData aSubData; //delete on decode test String message; }