Skip to content

Commit

Permalink
add autocast doc and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Oct 6, 2023
1 parent d31732e commit 686cdfd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 17 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 @@ -618,7 +618,23 @@ 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 deserialize(byte[] bytes) {
return deserialize(MemoryUtils.wrap(bytes), null);
}
Expand Down
15 changes: 15 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 @@ -16,6 +16,7 @@

package io.fury;

import static io.fury.FuryTestBase.getJavaFury;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
Expand Down Expand Up @@ -535,4 +536,18 @@ public void testJavaOutputStream() throws IOException {
assertEquals(newObj, beanA);
}
}

@Test
public void testDeserializationAutotypeCast() {
Fury fury = getJavaFury();
byte[] bytes = fury.serialize(1);
Integer i = fury.deserialize(bytes);
Assert.assertEquals(i, 1);
Assert.assertThrows(
ClassCastException.class,
() -> {
String str = fury.deserialize(bytes);
System.out.println(str);
});
}
}

0 comments on commit 686cdfd

Please sign in to comment.