-
Notifications
You must be signed in to change notification settings - Fork 338
feat(java): custom class info serializer #2702
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
Open
mattjubb
wants to merge
1
commit into
apache:main
Choose a base branch
from
mattjubb:custom-classinfo-serializer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
java/fory-core/src/main/java/org/apache/fory/resolver/ClassInfoSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.apache.fory.resolver; | ||
|
|
||
| import org.apache.fory.memory.MemoryBuffer; | ||
|
|
||
| /** | ||
| * ClassInfoSerializer provides a mechanism to customize the recording of the ClassInfo | ||
| * default implementation uses a short value, however when the class registration needs to be customized | ||
| * this can be set of the ClassResolver to handle storing the ClassInfo is a customized manner | ||
| */ | ||
| public interface ClassInfoSerializer { | ||
| /** | ||
| * Writes the provided ClassInfo into the buffer in a customized manner which is consistent | ||
| * with the readClassInfo method | ||
| */ | ||
| void writeClassInfo(ClassResolver classResolver, MemoryBuffer buffer, ClassInfo classInfo); | ||
|
|
||
| /** | ||
| * Reads the ClassInfo from the provided buffer in a manner which is consistent with writeClassInfo | ||
| */ | ||
| ClassInfo readClassInfo(ClassResolver classResolver, MemoryBuffer buffer); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
java/fory-core/src/test/java/org/apache/fory/serializer/ClassInfoSerializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package org.apache.fory.serializer; | ||
|
|
||
|
|
||
| import lombok.Data; | ||
| import org.apache.fory.Fory; | ||
| import org.apache.fory.ForyTestBase; | ||
| import org.apache.fory.config.Language; | ||
| import org.apache.fory.memory.MemoryBuffer; | ||
| import org.apache.fory.resolver.ClassInfo; | ||
| import org.apache.fory.resolver.ClassInfoSerializer; | ||
| import org.apache.fory.resolver.ClassResolver; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Serializable; | ||
| import java.util.UUID; | ||
|
|
||
| public class ClassInfoSerializerTest extends ForyTestBase { | ||
|
|
||
| @Data | ||
| public static class JavaCustomClass implements Serializable { | ||
| public String name; | ||
| public transient int age; | ||
| public static final UUID UUID = new UUID(123L, 456L); | ||
|
|
||
| public JavaCustomClass(String name, int age) { | ||
| this.name = name; | ||
| this.age = age; | ||
| } | ||
|
|
||
| private void writeObject(java.io.ObjectOutputStream s) throws IOException { | ||
| s.defaultWriteObject(); | ||
| s.writeInt(age); | ||
| } | ||
|
|
||
| private void readObject(java.io.ObjectInputStream s) throws Exception { | ||
| s.defaultReadObject(); | ||
| this.age = s.readInt(); | ||
| } | ||
| } | ||
|
|
||
| @Data | ||
| public static class CustomClass { | ||
| public String name; | ||
| public static final UUID UUID = new UUID(444L, 555L); | ||
|
|
||
| public CustomClass(String name) { | ||
| this.name = name; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| static class Mapper implements ClassInfoSerializer { | ||
|
|
||
| @Override | ||
| public void writeClassInfo(ClassResolver classResolver, MemoryBuffer buffer, ClassInfo classInfo) { | ||
| UUID uuid; | ||
|
|
||
| if(classInfo.getCls().equals(JavaCustomClass.class)) | ||
| uuid = JavaCustomClass.UUID; | ||
| else if(classInfo.getCls().equals(CustomClass.class)) | ||
| uuid = CustomClass.UUID; | ||
| else | ||
| throw new RuntimeException("unknown class"); | ||
|
|
||
| buffer.writeVarUint64(uuid.getLeastSignificantBits()); | ||
| buffer.writeVarUint64(uuid.getMostSignificantBits()); | ||
| } | ||
|
|
||
| @Override | ||
| public ClassInfo readClassInfo(ClassResolver classResolver, MemoryBuffer buffer) { | ||
| long least = buffer.readVarUint64(); | ||
| long most = buffer.readVarUint64(); | ||
| UUID uuid = new UUID(most, least); | ||
|
|
||
| if(uuid.equals(JavaCustomClass.UUID)) | ||
| return classResolver.getClassInfo(JavaCustomClass.class); | ||
| else if(uuid.equals(CustomClass.UUID)) | ||
| return classResolver.getClassInfo(CustomClass.class); | ||
| else | ||
| throw new RuntimeException("unknown class"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testJavaObject() { | ||
| Fory fory = | ||
| Fory.builder() | ||
| .withLanguage(Language.JAVA) | ||
| .withRefTracking(false) | ||
| .requireClassRegistration(false) | ||
| .build(); | ||
|
|
||
| fory.getClassResolver().setClassMapper(new Mapper()); | ||
|
|
||
| JavaCustomClass deser = serDe(fory, new JavaCustomClass("bob", 10)); | ||
| Assert.assertEquals(deser.name, "bob"); | ||
| Assert.assertEquals(deser.age, 10); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testObject() { | ||
| Fory fory = Fory.builder() | ||
| .requireClassRegistration(false) | ||
| .build(); | ||
|
|
||
| fory.getClassResolver().setClassMapper(new Mapper()); | ||
|
|
||
| CustomClass deser = serDe(fory, new CustomClass("mike")); | ||
| Assert.assertEquals(deser.name, "mike"); | ||
| } | ||
|
|
||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will introduce another indirection, and incur virtual method call. The performance will degrade. Could you share some detais what will you implement using this ClassInfoSerializer. Maybe we could just merge the funtions of subclass of ClassInfoSerializer into class resolver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the PR description and the unit test show examples of how this would be used. Basically we maintain a large mapping of identifiers to classes, when an object is deserialized we want to control what ID is mapped to what class. I asked about this on slack a couple months ago, and you said that we cannot customize the class resolve since it final and you suggest instead to raise a PR that supports customized mapping logic. the intent of this feature is for it to be customizable by the calling code not something that is predefined within the class resolver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fory support register class by name. How about register class by uuid as name? Would this work for you? Your extension is in critical path. We are trying to keep callstack on this be as simple as we can. And you intercept whole class info serialization. Your class schema can't change anymore. The type forward/backward compatibility is implementes in current write/read classinfo method.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would require registering all classes upfront which means loading the .class, for large amounts of classes that is a very large upfront cost, lets say we have 10,000 classes and we only want to deserialize a single object with a single class - that would mean loading all 10,000 .class to just serialize a single object. if we could provide a fully qualified name (fqn) which could then be loaded when required via Class.forName then registering by name would probably meet our needs