-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): deserialize one pojo into another type (#2012)
<!-- **Thanks for contributing to Fury.** **If this is your first time opening a PR on fury, you can refer to [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).** Contribution Checklist - The **Apache Fury (incubating)** community has restrictions on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md). - Fury has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here. --> ## What does this PR do? replace class def if target class is different type with the actual serialized one, so it can be deserialized to another type #1998 <!-- Describe the purpose of this PR. --> ## Related issues <!-- Is there any related issue? Please attach here. - #xxxx0 - #xxxx1 - #xxxx2 --> ## 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. --> --------- Co-authored-by: chaokunyang <[email protected]>
- Loading branch information
1 parent
b4f5a2a
commit 8889203
Showing
7 changed files
with
161 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
103 changes: 103 additions & 0 deletions
103
...che/fury/serializer/compatible/DifferentPOJOCompatibleSerializerWithRegistrationTest.java
This file contains 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,103 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.fury.serializer.compatible; | ||
|
||
import org.apache.fury.Fury; | ||
import org.apache.fury.config.CompatibleMode; | ||
import org.apache.fury.config.Language; | ||
import org.apache.fury.serializer.compatible.classes.ClassCompleteField; | ||
import org.apache.fury.serializer.compatible.classes.ClassMissingField; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Test COMPATIBILITY mode that supports - same field type and name can be deserialized to other | ||
* also test generic class | ||
*/ | ||
public class DifferentPOJOCompatibleSerializerWithRegistrationTest extends Assert { | ||
|
||
Fury getFury(Class<?>... classes) { | ||
Fury instance = | ||
Fury.builder() | ||
.withLanguage(Language.JAVA) | ||
.withRefTracking(true) | ||
.withCompatibleMode(CompatibleMode.COMPATIBLE) | ||
.withMetaShare(true) | ||
.withScopedMetaShare(true) | ||
.requireClassRegistration(false) | ||
.withAsyncCompilation(true) | ||
.serializeEnumByName(true) | ||
.build(); | ||
if (classes != null) { | ||
for (Class<?> clazz : classes) { | ||
instance.register(clazz); | ||
} | ||
} | ||
; | ||
return instance; | ||
} | ||
|
||
@Test | ||
void testTargetHasLessFieldComparedToSourceClass() throws InterruptedException { | ||
|
||
ClassCompleteField<String> subclass = new ClassCompleteField<>("subclass", "subclass2"); | ||
ClassCompleteField<ClassCompleteField<String>> classCompleteField = | ||
new ClassCompleteField<>(subclass, subclass); | ||
byte[] serialized = getFury(ClassCompleteField.class).serializeJavaObject(classCompleteField); | ||
ClassMissingField<ClassMissingField<String>> classMissingField = | ||
getFury(ClassMissingField.class).deserializeJavaObject(serialized, ClassMissingField.class); | ||
|
||
assertEq(classCompleteField, classMissingField); | ||
} | ||
|
||
@Test | ||
void testTargetHasMoreFieldComparedToSourceClass() throws InterruptedException { | ||
|
||
ClassMissingField<String> subclass = new ClassMissingField<>("subclass"); | ||
ClassMissingField classMissingField = new ClassMissingField(subclass); | ||
byte[] serialized = getFury(ClassMissingField.class).serializeJavaObject(classMissingField); | ||
|
||
ClassCompleteField classCompleteField = | ||
getFury(ClassCompleteField.class) | ||
.deserializeJavaObject(serialized, ClassCompleteField.class); | ||
|
||
assertEq(classCompleteField, classMissingField); | ||
} | ||
|
||
void assertEq(ClassCompleteField classCompleteField, ClassMissingField classMissingField) { | ||
assertEqSubClass( | ||
(ClassCompleteField) classCompleteField.getPrivateFieldSubClass(), | ||
(ClassMissingField) classMissingField.getPrivateFieldSubClass()); | ||
assertEquals(classCompleteField.getPrivateMap(), classMissingField.getPrivateMap()); | ||
assertEquals(classCompleteField.getPrivateList(), classMissingField.getPrivateList()); | ||
assertEquals(classCompleteField.getPrivateString(), classMissingField.getPrivateString()); | ||
assertEquals(classCompleteField.getPrivateInt(), classMissingField.getPrivateInt()); | ||
} | ||
|
||
void assertEqSubClass( | ||
ClassCompleteField classCompleteField, ClassMissingField classMissingField) { | ||
assertEquals( | ||
classCompleteField.getPrivateFieldSubClass(), classMissingField.getPrivateFieldSubClass()); | ||
assertEquals(classCompleteField.getPrivateMap(), classMissingField.getPrivateMap()); | ||
assertEquals(classCompleteField.getPrivateList(), classMissingField.getPrivateList()); | ||
assertEquals(classCompleteField.getPrivateString(), classMissingField.getPrivateString()); | ||
assertEquals(classCompleteField.getPrivateInt(), classMissingField.getPrivateInt()); | ||
} | ||
} |