Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): support auto generic cast for fury deserialization #983

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
29 changes: 28 additions & 1 deletion java/fury-core/src/main/java/io/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @author chaokunyang
*/
@NotThreadSafe
@SuppressWarnings("unchecked")
public final class Fury {
private static final Logger LOG = LoggerFactory.getLogger(Fury.class);

Expand Down Expand Up @@ -617,7 +618,33 @@ public long readLong(MemoryBuffer buffer) {
return LongSerializer.readLong(buffer, longEncoding);
}

/** Deserialize <code>obj</code> from a byte array. */
/**
* Deserialize <code>obj</code> 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.
*
* <p>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}.
*
* <pre>{@code
* byte[] bytes = fury.serialize(1);
* String o = fury.deserialize(bytes); // throw ClassCastException.
* }</pre>
*
* @param bytes serialized data.
* @param <T> result type of the data.
* @return deserialized object.
*/
public <T> T deserializeTyped(byte[] bytes) {
return (T) deserialize(MemoryUtils.wrap(bytes), null);
}

/**
* Deserialize <code>obj</code> from a byte array.
*
* @param bytes serialized data.
* @return deserialized object.
*/
public Object deserialize(byte[] bytes) {
return deserialize(MemoryUtils.wrap(bytes), null);
}
Expand Down
4 changes: 4 additions & 0 deletions java/fury-core/src/main/java/io/fury/ThreadLocalFury.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public MemoryBuffer serialize(MemoryBuffer buffer, Object obj) {
return bindingThreadLocal.get().get().serialize(buffer, obj);
}

public <T> T deserializeTyped(byte[] bytes) {
return bindingThreadLocal.get().get().deserializeTyped(bytes);
}

public Object deserialize(byte[] bytes) {
return bindingThreadLocal.get().get().deserialize(bytes);
}
Expand Down
2 changes: 2 additions & 0 deletions java/fury-core/src/main/java/io/fury/ThreadSafeFury.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface ThreadSafeFury {

MemoryBuffer serialize(MemoryBuffer buffer, Object obj);

<T> T deserializeTyped(byte[] bytes);

Object deserialize(byte[] bytes);

Object deserialize(long address, int size);
Expand Down
10 changes: 10 additions & 0 deletions java/fury-core/src/main/java/io/fury/pool/ThreadPoolFury.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public MemoryBuffer serialize(MemoryBuffer buffer, Object obj) {
}
}

public <T> 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 {
Expand Down
14 changes: 14 additions & 0 deletions java/fury-core/src/test/java/io/fury/FuryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down