Repository navigation
feat(java): add deserialize overloads that take a TypeRef - #4163
Swatantra-66 wants to merge 1 commit into
Conversation
983191f to
b38e79d
Compare
| checkHeaderBitmapWithoutOutOfBand(bitmap); | ||
| } | ||
| readContext.prepare(buffer, null, false); | ||
| try { |
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.
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!
81855f0 to
fc21d71
Compare
|
|
||
| @Override | ||
| public <T> T deserialize(MemoryBuffer buffer, TypeRef<T> typeRef) { | ||
| PooledEntry entry = acquire(); |
There was a problem hiding this comment.
Because the TypeRef overloads just delegate to the Class overloads, four default methods on BaseFory could replace the twelve implementations across Fory, ThreadLocalFory and ThreadPoolFory.
There was a problem hiding this comment.
@ayush00git! Great suggestion. I have replaced all 12 subclass implementations with 4 default methods on BaseFory that delegate to the respective Class overloads. All tests continue to pass.
Add TypeRef<T> deserialize overloads via default methods on BaseFory 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 <[email protected]>
03f4a59 to
b061343
Compare
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.