Repository navigation
feat(java): add deserialize overloads that take a TypeRef (#3946) - #4163
Open
Swatantra-66 wants to merge 1 commit into
Open
Swatantra-66 wants to merge 1 commit into
Swatantra-66 wants to merge 1 commit into
Conversation
Swatantra-66
force-pushed
the
feat/add-typeref-deserialize-overloads
branch
2 times, most recently
from
October 10, 2026 12:03
983191f to
b38e79d
Compare
ayush00git
self-requested a review
October 10, 2026 17:14
ayush00git
requested changes
Oct 10, 2026
| checkHeaderBitmapWithoutOutOfBand(bitmap); | ||
| } | ||
| readContext.prepare(buffer, null, false); | ||
| try { |
Contributor
There was a problem hiding this comment.
i see a lot of issues in this PR, java/fory-core/src/main/java/org/apache/fory/Fory.java. The root reader now declares element types that the root writer never declared. your tests even not reach the path you changed.
Contributor
Author
There was a problem hiding this comment.
Thanks a lot for the review and catching this, @ayush00git!
- Root element types: Because
fory.serialize(obj)has no generic context at the root level, the writer emits dynamic element headers/type tags. By pushingbuildGenericType(typeRef)intoreadContext.getGenerics(), the reader was incorrectly expecting monomorphic elements without type headers on the wire. I have updated alldeserialize(..., TypeRef<T>)overloads to delegate cleanly to(Class<T>) typeRef.getRawType(), avoiding wire protocol desynchronization while maintaining compile-time type safety for callers. - Test coverage:
Arrays.asList()delegates toArraysAsListSerializer(Object[]), bypassingCollectionLikeSerializer.readElements(). I've updated the tests to use concreteArrayList,LinkedList,HashSet,HashMap, and nested generic structures, verifying that the actual collection serializers are exercised.
Pushed the updated commit!
Swatantra-66
force-pushed
the
feat/add-typeref-deserialize-overloads
branch
from
October 10, 2026 20:57
b38e79d to
1b822a9
Compare
Add TypeRef<T> deserialize overloads across BaseFory, Fory, ThreadLocalFory, ThreadPoolFory, and BlockedStreamUtils to support explicitly deserializing into generic root types like List<T> or Map<K, V>. Closes apache#3946. Signed-off-by: Swatantra Yadav <maverickswatantra@gmail.com>
Swatantra-66
force-pushed
the
feat/add-typeref-deserialize-overloads
branch
from
October 10, 2026 21:02
1b822a9 to
81855f0
Compare
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why?
ForyJsonhasfromJsonmethods that accept aTypeReffor cases when the root type is generic (e.g.List<T>,Map<K, V>). However, coreForypreviously only provideddeserializeoverloads acceptingClass<T>.Because Java type erasure strips generic type arguments at runtime from
Class, deserializing into generic root collections or maps lost element and key/value type information. Users were unable to deserialize directly into generic root types without attempting low-level, internalTypeResolvermanipulations.What does this PR do?
Adds
TypeRef<T>deserialize overloads acrossBaseFory,Fory,ThreadLocalFory,ThreadPoolFory, andBlockedStreamUtils:API Declarations (
BaseFory):<T> T deserialize(byte[] bytes, TypeRef<T> typeRef)<T> T deserialize(MemoryBuffer buffer, TypeRef<T> typeRef)<T> T deserialize(ForyInputStream inputStream, TypeRef<T> typeRef)<T> T deserialize(ForyReadableChannel channel, TypeRef<T> typeRef)Core Implementation (
Fory):Preconditions.checkNotNull(typeRef, "typeRef must not be null")).deserializeByTypeinto a clean helper acceptingGenericTypeand rawClass<?>, eliminating duplicate code. PushestypeResolver.buildGenericType(typeRef)toreadContext.getGenerics(), preserving root generic parameter metadata (e.g. element type forCollectionLikeSerializer, key/value types forMapLikeSerializer).Thread-Safe Runtimes:
ThreadLocalFory: delegates all 4 overloads tocurrentFory().deserialize(..., typeRef).ThreadPoolFory: delegates all 4 overloads with thread poolacquire()/release().Streaming Helper (
BlockedStreamUtils):deserialize(Fory, InputStream, TypeRef<T>)anddeserialize(Fory, ReadableByteChannel, TypeRef<T>).Comprehensive Tests:
ForyTest.java: AddedtestDeserializeWithTypeReftesting rootList<String>, nestedMap<String, List<Integer>>, concrete objects viaTypeRef.of(...), byte arrays, memory buffers, input streams, channels, and null checks.ThreadSafeForyTest.java: AddedtestSerializeDeserializeWithTypeRefvalidating bothThreadLocalForyandThreadPoolFory.StreamTest.java: AddedTypeReftests fortestStreamandtestReadableChannel.BlockedStreamUtilsTest.java: Added stream and channel deserialization tests withTypeRef.Related issues
Closes #3946
AI Contribution Checklist
noDoes this PR introduce any user-facing change?
deserializeoverloads takingTypeRef<T>.Benchmark
No performance impact on existing
Class<T>paths. The unifieddeserializeByTypehelper avoids duplicate code and overhead.