51
51
import com .fasterxml .jackson .databind .jsontype .impl .StdTypeResolverBuilder ;
52
52
import com .fasterxml .jackson .databind .module .SimpleModule ;
53
53
import com .fasterxml .jackson .databind .node .TextNode ;
54
+ import com .fasterxml .jackson .databind .node .TreeTraversingParser ;
54
55
import com .fasterxml .jackson .databind .ser .SerializerFactory ;
55
56
import com .fasterxml .jackson .databind .ser .std .StdSerializer ;
56
57
import com .fasterxml .jackson .databind .type .TypeFactory ;
@@ -306,12 +307,21 @@ public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws Serializ
306
307
}
307
308
308
309
try {
309
- return (T ) reader .read (mapper , source , resolveType (source , type ));
310
+
311
+ TypeTuple typeTuple = resolveType (source , type );
312
+ try (JsonParser parser = createParser (source , typeTuple )) {
313
+ return (T ) reader .read (mapper , parser , typeTuple .type ());
314
+ }
315
+
310
316
} catch (Exception ex ) {
311
- throw new SerializationException ("Could not read JSON:%s " .formatted (ex .getMessage ()), ex );
317
+ throw new SerializationException ("Could not read JSON: %s " .formatted (ex .getMessage ()), ex );
312
318
}
313
319
}
314
320
321
+ private JsonParser createParser (byte [] source , TypeTuple typeTuple ) throws IOException {
322
+ return typeTuple .node () == null ? mapper .createParser (source ) : new TreeTraversingParser (typeTuple .node (), mapper );
323
+ }
324
+
315
325
/**
316
326
* Builder method used to configure and customize the internal Jackson {@link ObjectMapper} created by this
317
327
* {@link GenericJackson2JsonRedisSerializer} and used to de/serialize {@link Object objects} as {@literal JSON}.
@@ -332,15 +342,19 @@ public GenericJackson2JsonRedisSerializer configure(Consumer<ObjectMapper> objec
332
342
return this ;
333
343
}
334
344
335
- protected JavaType resolveType (byte [] source , Class <?> type ) throws IOException {
345
+ protected TypeTuple resolveType (byte [] source , Class <?> type ) throws IOException {
336
346
337
347
if (!type .equals (Object .class ) || !defaultTypingEnabled .get ()) {
338
- return typeResolver .constructType (type );
348
+ return new TypeTuple ( typeResolver .constructType (type ), null );
339
349
}
340
350
341
351
return typeResolver .resolveType (source , type );
342
352
}
343
353
354
+ protected record TypeTuple (JavaType type , @ Nullable JsonNode node ) {
355
+
356
+ }
357
+
344
358
/**
345
359
* @since 3.0
346
360
*/
@@ -361,16 +375,16 @@ protected JavaType constructType(Class<?> type) {
361
375
return typeFactory .get ().constructType (type );
362
376
}
363
377
364
- protected JavaType resolveType (byte [] source , Class <?> type ) throws IOException {
378
+ protected TypeTuple resolveType (byte [] source , Class <?> type ) throws IOException {
365
379
366
380
JsonNode root = readTree (source );
367
381
JsonNode jsonNode = root .get (hintName .get ());
368
382
369
383
if (jsonNode instanceof TextNode && jsonNode .asText () != null ) {
370
- return typeFactory .get ().constructFromCanonical (jsonNode .asText ());
384
+ return new TypeTuple ( typeFactory .get ().constructFromCanonical (jsonNode .asText ()), root );
371
385
}
372
386
373
- return constructType (type );
387
+ return new TypeTuple ( constructType (type ), root );
374
388
}
375
389
376
390
/**
0 commit comments