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

[Java] auto generic cast for fury deserialization #982

Open
chaokunyang opened this issue Oct 6, 2023 · 2 comments · May be fixed by #983
Open

[Java] auto generic cast for fury deserialization #982

chaokunyang opened this issue Oct 6, 2023 · 2 comments · May be fixed by #983
Labels
enhancement New feature or request java

Comments

@chaokunyang
Copy link
Collaborator

Is your feature request related to a problem? Please describe.

Currently fury deserialize method return Object type, if users serialized an object of type Foo, users needs to cast to Foo manually:

byte[] d = fury.serialize(foo);
Object o = fury.deserialize(d);
Foo o1 = (Foo) o;

This is inconvenient, and if there are generics such as:

Foo<A> foo = xxx;
byte[] d = fury.serialize(foo);
Object o = fury.deserialize(d);
Foo<A> foo o1 = (Foo<A> foo) o;

the cast will issue a javac warnings.

Then users will need to warp an util like this:

@SuppressWarnings("unchecked")
public static <T> T deserialize(byte[] b) {
  Object o = fury.deserialize(b);
  return (T) o;
}

We should do this for users.

Describe the solution you'd like

Implement cast in fuyr:

@SuppressWarnings("unchecked")
public static <T> T deserialize(byte[] b) {
  Object o = fury.deserialize(b);
  return (T) o;
}

Note if users declare error type, the deserialization will raise ClassCastException:

Foo<A> foo = xxx;
byte[] d = fury.serialize(foo);
Bar o = fury.deserialize(d); // raise ClassCastException.
@chaokunyang chaokunyang added enhancement New feature or request java labels Oct 6, 2023
@chaokunyang chaokunyang changed the title [Java] add auto generic cast for fury deserialization [Java] auto generic cast for fury deserialization Oct 6, 2023
@chaokunyang chaokunyang linked a pull request Oct 6, 2023 that will close this issue
2 tasks
@chaokunyang
Copy link
Collaborator Author

chaokunyang commented Oct 6, 2023

This will make type infer ambigious
image

Maybe we should add a new method instead of update existing methods

@chaokunyang
Copy link
Collaborator Author

chaokunyang commented Oct 6, 2023

Maybe add a new mthod instead?

  public <T> T deserializeTyped(byte[] bytes) {
    return (T) deserialize(MemoryUtils.wrap(bytes), null);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request java
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant