This is an automated email from the ASF dual-hosted git repository. chaokunyang pushed a commit to branch releases-0.10 in repository https://gitbox.apache.org/repos/asf/fury.git
commit 7ddfaf52e79463176212422040d832a150b734f7 Author: Shawn Yang <[email protected]> AuthorDate: Tue Dec 10 08:53:43 2024 +0800 docs(java): add object mapping example and tests (#1974) ## What does this PR do? add object mapping example and tests ## Related issues #1973 ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fury/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. --> --- docs/guide/java_serialization_guide.md | 51 ++++++++++++++++++++++ .../src/test/java/org/apache/fury/FuryTest.java | 34 +++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/docs/guide/java_serialization_guide.md b/docs/guide/java_serialization_guide.md index cd9ac57c..52703901 100644 --- a/docs/guide/java_serialization_guide.md +++ b/docs/guide/java_serialization_guide.md @@ -398,6 +398,57 @@ losing any information. If metadata sharing is not enabled, the new class data will be skipped and an `NonexistentSkipClass` stub object will be returned. +### Coping/Mapping object from one type to another type + +Fury support mapping object from one type to another type. +> Notes: +> +> 1. This mapping will execute a deep copy, all mapped fields are serialized into binary and +deserialized from that binary to map into another type. +> 2. All struct types must be registered with same ID, otherwise Fury can not mapping to correct struct type. +> Be careful when you use `Fury#register(Class)`, because fury will allocate an auto-grown ID which might be +> inconsistent if you register classes with different order between Fury instance. + +```java +public class StructMappingExample { + static class Struct1 { + int f1; + String f2; + + public Struct1(int f1, String f2) { + this.f1 = f1; + this.f2 = f2; + } + } + + static class Struct2 { + int f1; + String f2; + double f3; + } + + static ThreadSafeFury fury1 = Fury.builder() + .withCompatibleMode(CompatibleMode.COMPATIBLE).buildThreadSafeFury(); + static ThreadSafeFury fury2 = Fury.builder() + .withCompatibleMode(CompatibleMode.COMPATIBLE).buildThreadSafeFury(); + + static { + fury1.register(Struct1.class); + fury2.register(Struct2.class); + } + + public static void main(String[] args) { + Struct1 struct1 = new Struct1(10, "abc"); + Struct2 struct2 = (Struct2) fury2.deserialize(fury1.serialize(struct1)); + Assert.assertEquals(struct2.f1, struct1.f1); + Assert.assertEquals(struct2.f2, struct1.f2); + struct1 = (Struct1) fury1.deserialize(fury2.serialize(struct2)); + Assert.assertEquals(struct1.f1, struct2.f1); + Assert.assertEquals(struct1.f2, struct2.f2); + } +} +``` + ## Migration ### JDK migration diff --git a/java/fury-core/src/test/java/org/apache/fury/FuryTest.java b/java/fury-core/src/test/java/org/apache/fury/FuryTest.java index b5aaa0ff..30e845f1 100644 --- a/java/fury-core/src/test/java/org/apache/fury/FuryTest.java +++ b/java/fury-core/src/test/java/org/apache/fury/FuryTest.java @@ -55,6 +55,7 @@ import lombok.EqualsAndHashCode; import org.apache.fury.annotation.Expose; import org.apache.fury.annotation.Ignore; import org.apache.fury.builder.Generated; +import org.apache.fury.config.CompatibleMode; import org.apache.fury.config.FuryBuilder; import org.apache.fury.config.Language; import org.apache.fury.exception.FuryException; @@ -636,4 +637,37 @@ public class FuryTest extends FuryTestBase { fury.deserialize(serializedLarge); assertEquals(fury.getBuffer().size(), limitInBytes); } + + static class Struct1 { + int f1; + String f2; + + public Struct1(int f1, String f2) { + this.f1 = f1; + this.f2 = f2; + } + } + + static class Struct2 { + int f1; + String f2; + double f3; + } + + @Test + public void testStructMapping() { + ThreadSafeFury fury1 = + Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).buildThreadSafeFury(); + ThreadSafeFury fury2 = + Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).buildThreadSafeFury(); + fury1.register(Struct1.class); + fury2.register(Struct2.class); + Struct1 struct1 = new Struct1(10, "abc"); + Struct2 struct2 = (Struct2) fury2.deserialize(fury1.serialize(struct1)); + Assert.assertEquals(struct2.f1, struct1.f1); + Assert.assertEquals(struct2.f2, struct1.f2); + struct1 = (Struct1) fury1.deserialize(fury2.serialize(struct2)); + Assert.assertEquals(struct1.f1, struct2.f1); + Assert.assertEquals(struct1.f2, struct2.f2); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
