diff --git a/java/fury-benchmark/src/main/java/io/fury/benchmark/state/JsonbState.java b/java/fury-benchmark/src/main/java/io/fury/benchmark/state/JsonbState.java index 1d943eade9..c4c98401b9 100644 --- a/java/fury-benchmark/src/main/java/io/fury/benchmark/state/JsonbState.java +++ b/java/fury-benchmark/src/main/java/io/fury/benchmark/state/JsonbState.java @@ -18,7 +18,6 @@ import com.alibaba.fastjson2.JSONB; import com.alibaba.fastjson2.JSONFactory; -import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.reader.ObjectReaderProvider; @@ -239,12 +238,5 @@ public static void main(String[] args) { state.setup(); state.bufferType = BufferType.directBuffer; state.setup(); - - ; - JSONObject json = new JSONObject(); - json.put("k", 1); - Fury fury = Fury.builder().requireClassRegistration(false).build(); - byte[] bytes = fury.serialize(json); - System.out.println(fury.deserialize(bytes)); } } diff --git a/java/fury-core/src/main/java/io/fury/Fury.java b/java/fury-core/src/main/java/io/fury/Fury.java index 563353b0db..3030847d3e 100644 --- a/java/fury-core/src/main/java/io/fury/Fury.java +++ b/java/fury-core/src/main/java/io/fury/Fury.java @@ -70,6 +70,7 @@ * @author chaokunyang */ @NotThreadSafe +@SuppressWarnings("unchecked") public final class Fury { private static final Logger LOG = LoggerFactory.getLogger(Fury.class); @@ -617,7 +618,33 @@ public long readLong(MemoryBuffer buffer) { return LongSerializer.readLong(buffer, longEncoding); } - /** Deserialize obj from a byte array. */ + /** + * Deserialize obj from a byte array. Note declared result type must be a class or + * superclass/interface of the serialized object, otherwise a {@link ClassCastException} will be + * thrown. + * + *

For example, if you serialized an object of type `Integer`, then your declared deserialized + * type is `String`, the deserialization will just throw a {@link ClassCastException}. + * + *

{@code
+   * byte[] bytes = fury.serialize(1);
+   * String o = fury.deserialize(bytes); // throw ClassCastException.
+   * }
+ * + * @param bytes serialized data. + * @param result type of the data. + * @return deserialized object. + */ + public T deserializeTyped(byte[] bytes) { + return (T) deserialize(MemoryUtils.wrap(bytes), null); + } + + /** + * Deserialize obj from a byte array. + * + * @param bytes serialized data. + * @return deserialized object. + */ public Object deserialize(byte[] bytes) { return deserialize(MemoryUtils.wrap(bytes), null); } diff --git a/java/fury-core/src/main/java/io/fury/ThreadLocalFury.java b/java/fury-core/src/main/java/io/fury/ThreadLocalFury.java index ad0e0436e9..a87e967047 100644 --- a/java/fury-core/src/main/java/io/fury/ThreadLocalFury.java +++ b/java/fury-core/src/main/java/io/fury/ThreadLocalFury.java @@ -69,6 +69,10 @@ public MemoryBuffer serialize(MemoryBuffer buffer, Object obj) { return bindingThreadLocal.get().get().serialize(buffer, obj); } + public T deserializeTyped(byte[] bytes) { + return bindingThreadLocal.get().get().deserializeTyped(bytes); + } + public Object deserialize(byte[] bytes) { return bindingThreadLocal.get().get().deserialize(bytes); } diff --git a/java/fury-core/src/main/java/io/fury/ThreadSafeFury.java b/java/fury-core/src/main/java/io/fury/ThreadSafeFury.java index 530d663c13..0eebc8c863 100644 --- a/java/fury-core/src/main/java/io/fury/ThreadSafeFury.java +++ b/java/fury-core/src/main/java/io/fury/ThreadSafeFury.java @@ -39,6 +39,8 @@ public interface ThreadSafeFury { MemoryBuffer serialize(MemoryBuffer buffer, Object obj); + T deserializeTyped(byte[] bytes); + Object deserialize(byte[] bytes); Object deserialize(long address, int size); diff --git a/java/fury-core/src/main/java/io/fury/pool/ThreadPoolFury.java b/java/fury-core/src/main/java/io/fury/pool/ThreadPoolFury.java index 2a7634a0a8..8591cd0379 100644 --- a/java/fury-core/src/main/java/io/fury/pool/ThreadPoolFury.java +++ b/java/fury-core/src/main/java/io/fury/pool/ThreadPoolFury.java @@ -71,6 +71,16 @@ public MemoryBuffer serialize(MemoryBuffer buffer, Object obj) { } } + public T deserializeTyped(byte[] bytes) { + Fury fury = null; + try { + fury = furyPooledObjectFactory.getFury(); + return fury.deserializeTyped(bytes); + } finally { + furyPooledObjectFactory.returnFury(fury); + } + } + public Object deserialize(byte[] bytes) { Fury fury = null; try { diff --git a/java/fury-core/src/test/java/io/fury/FuryTest.java b/java/fury-core/src/test/java/io/fury/FuryTest.java index bcbd90bc72..8f02ffa267 100644 --- a/java/fury-core/src/test/java/io/fury/FuryTest.java +++ b/java/fury-core/src/test/java/io/fury/FuryTest.java @@ -535,4 +535,18 @@ public void testJavaOutputStream() throws IOException { assertEquals(newObj, beanA); } } + + @Test + public void testDeserializationAutotypeCast() { + Fury fury = getJavaFury(); + byte[] bytes = fury.serialize(1); + Integer i = fury.deserializeTyped(bytes); + Assert.assertEquals(i, 1); + Assert.assertThrows( + ClassCastException.class, + () -> { + String str = fury.deserializeTyped(bytes); + System.out.println(str); + }); + } } diff --git a/java/fury-core/src/test/java/io/fury/ThreadSafeFuryTest.java b/java/fury-core/src/test/java/io/fury/ThreadSafeFuryTest.java index a21ef9a787..9bc0297eb1 100644 --- a/java/fury-core/src/test/java/io/fury/ThreadSafeFuryTest.java +++ b/java/fury-core/src/test/java/io/fury/ThreadSafeFuryTest.java @@ -86,6 +86,7 @@ public void testSerialize() throws Exception { try { fury.setClassLoader(beanA.getClass().getClassLoader()); assertEquals(fury.deserialize(fury.serialize(beanA)), beanA); + assertEquals(fury.deserializeTyped(fury.serialize(beanA)), beanA); } catch (Exception e) { hasException = true; e.printStackTrace();