-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathBeanDeserializerBase.java
More file actions
1986 lines (1803 loc) · 82.5 KB
/
Copy pathBeanDeserializerBase.java
File metadata and controls
1986 lines (1803 loc) · 82.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tools.jackson.databind.deser.bean;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.annotation.*;
import tools.jackson.core.*;
import tools.jackson.core.JsonParser.NumberType;
import tools.jackson.databind.*;
import tools.jackson.databind.cfg.ConfigOverride;
import tools.jackson.databind.deser.*;
import tools.jackson.databind.deser.impl.*;
import tools.jackson.databind.deser.std.StdConvertingDeserializer;
import tools.jackson.databind.deser.std.StdDeserializer;
import tools.jackson.databind.exc.IgnoredPropertyException;
import tools.jackson.databind.introspect.*;
import tools.jackson.databind.jsontype.TypeDeserializer;
import tools.jackson.databind.type.ClassKey;
import tools.jackson.databind.type.LogicalType;
import tools.jackson.databind.util.*;
/**
* Base class for <code>BeanDeserializer</code>.
*/
public abstract class BeanDeserializerBase
extends StdDeserializer<Object>
implements ValueInstantiator.Gettable
{
protected final static PropertyName TEMP_PROPERTY_NAME = new PropertyName("#temporary-name");
/*
/**********************************************************************
/* Information regarding type being deserialized
/**********************************************************************
*/
/**
* Declared type of the bean this deserializer handles.
*/
protected final JavaType _beanType;
/**
* Requested shape from bean class annotations.
*/
protected final JsonFormat.Shape _serializationShape;
/*
/**********************************************************************
/* Configuration for creating value instance
/**********************************************************************
*/
/**
* Object that handles details of constructing initial
* bean value (to which bind data to), unless instance
* is passed (via updateValue())
*/
protected final ValueInstantiator _valueInstantiator;
/**
* Deserializer that is used iff delegate-based creator is
* to be used for deserializing from JSON Object.
*<p>
* NOTE: cannot be {@code final} because we need to get it during
* {@code resolve()} method (and not contextualization).
*/
protected ValueDeserializer<Object> _delegateDeserializer;
/**
* Deserializer that is used iff array-delegate-based creator
* is to be used for deserializing from JSON Object.
*<p>
* NOTE: cannot be {@code final} because we need to get it during
* {@code resolve()} method (and not contextualization).
*/
protected ValueDeserializer<Object> _arrayDelegateDeserializer;
/**
* If the bean needs to be instantiated using constructor
* or factory method
* that takes one or more named properties as argument(s),
* this creator is used for instantiation.
* This value gets resolved during general resolution.
*/
protected PropertyBasedCreator _propertyBasedCreator;
/**
* Flag that is set to mark cases where deserialization from Object value
* using otherwise "standard" property binding will need to use non-default
* creation method: namely, either "full" delegation (array-delegation does
* not apply), or properties-based Creator method is used.
*<p>
* Note that flag is somewhat mis-named as it is not affected by scalar-delegating
* creators; it only has effect on Object Value binding.
*/
protected boolean _nonStandardCreation;
/**
* Flag that indicates that no "special features" whatsoever
* are enabled, so the simplest processing is possible.
*/
protected boolean _vanillaProcessing;
/*
/**********************************************************************
/* Property information, setters
/**********************************************************************
*/
/**
* Mapping of property names to properties, built when all properties
* to use have been successfully resolved.
*/
protected final BeanPropertyMap _beanProperties;
/**
* List of {@link ValueInjector}s, if any injectable values are
* expected by the bean; otherwise null.
* This includes injectors used for injecting values via setters
* and fields, but not ones passed through constructor parameters.
*/
protected final ValueInjector[] _injectables;
/**
* Fallback setter used for handling any properties that are not
* mapped to regular setters. If setter is not null, it will be
* called once for each such property.
*/
protected SettableAnyProperty _anySetter;
/**
* In addition to properties that are set, we will also keep
* track of recognized but ignorable properties: these will
* be skipped without errors or warnings.
*/
protected final Set<String> _ignorableProps;
/**
* Keep track of the properties that need to be specifically included.
*/
protected final Set<String> _includableProps;
/**
* Flag that can be set to ignore and skip unknown properties.
* If set, will not throw an exception for unknown properties.
*/
protected final boolean _ignoreAllUnknown;
/**
* Flag that indicates that some aspect of deserialization depends
* on active view used (if any)
*/
protected final boolean _needViewProcesing;
/**
* We may also have one or more back reference fields (usually
* zero or one).
*/
protected final Map<String, SettableBeanProperty> _backRefs;
/*
/**********************************************************************
/* Related handlers
/**********************************************************************
*/
/**
* Lazily constructed map used to contain deserializers needed
* for polymorphic subtypes.
* Note that this is <b>only needed</b> for polymorphic types,
* that is, when the actual type is not statically known.
* For other types this remains null.
*/
protected transient ConcurrentHashMap<ClassKey, ValueDeserializer<Object>> _subDeserializers;
/**
* If one of properties has "unwrapped" value, we need separate
* helper object
*/
protected UnwrappedPropertyHandler _unwrappedPropertyHandler;
/**
* Handler that we need if any of properties uses external
* type id.
*/
protected ExternalTypeHandler _externalTypeIdHandler;
/**
* If an Object Id is to be used for value handled by this
* deserializer, this reader is used for handling.
*/
protected final ObjectIdReader _objectIdReader;
/*
/**********************************************************************
/* Life-cycle, construction, initialization
/**********************************************************************
*/
/**
* Constructor used when initially building a deserializer
* instance, given a {@link BeanDeserializerBuilder} that
* contains configuration.
*/
protected BeanDeserializerBase(BeanDeserializerBuilder builder,
BeanDescription.Supplier beanDescRef,
BeanPropertyMap properties, Map<String, SettableBeanProperty> backRefs,
Set<String> ignorableProps, boolean ignoreAllUnknown,
Set<String> includableProps,
boolean hasViews)
{
super(beanDescRef.getType());
_beanType = beanDescRef.getType();
_valueInstantiator = builder.getValueInstantiator();
_delegateDeserializer = null;
_arrayDelegateDeserializer = null;
_propertyBasedCreator = null;
_beanProperties = properties;
_backRefs = backRefs;
_ignorableProps = ignorableProps;
_ignoreAllUnknown = ignoreAllUnknown;
_includableProps = includableProps;
_anySetter = builder.getAnySetter();
List<ValueInjector> injectables = builder.getInjectables();
_injectables = (injectables == null || injectables.isEmpty()) ? null
: injectables.toArray(new ValueInjector[0]);
_objectIdReader = builder.getObjectIdReader();
// 02-May-2020, tatu: This boolean setting is only used when binding from
// Object value, and hence does not consider "array-delegating" or various
// scalar-delegation cases. It is set when default (0-argument) constructor
// is NOT to be used when binding an Object value (or in case of
// POJO-as-array, Array value).
_nonStandardCreation = (_unwrappedPropertyHandler != null)
|| _valueInstantiator.canCreateUsingDelegate()
// [databind#2486]: as per above, array-delegating creator should not be considered
// as doing so will prevent use of Array-or-standard-Object deserialization
// || _valueInstantiator.canCreateUsingArrayDelegate()
|| _valueInstantiator.canCreateFromObjectWith()
|| !_valueInstantiator.canCreateUsingDefault()
;
// Any transformation we may need to apply?
_serializationShape = beanDescRef.findExpectedFormat(_beanType.getRawClass()).getShape();
_needViewProcesing = hasViews;
_vanillaProcessing = !_nonStandardCreation
&& (_injectables == null)
&& !_needViewProcesing
// also, may need to reorder stuff if we expect Object Id:
&& (_objectIdReader == null)
;
}
protected BeanDeserializerBase(BeanDeserializerBase src) {
this(src, src._ignoreAllUnknown);
}
protected BeanDeserializerBase(BeanDeserializerBase src, boolean ignoreAllUnknown)
{
super(src._beanType);
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_arrayDelegateDeserializer = src._arrayDelegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;
_beanProperties = src._beanProperties;
_backRefs = src._backRefs;
_ignorableProps = src._ignorableProps;
_ignoreAllUnknown = ignoreAllUnknown;
_includableProps = src._includableProps;
_anySetter = src._anySetter;
_injectables = src._injectables;
_objectIdReader = src._objectIdReader;
_nonStandardCreation = src._nonStandardCreation;
_unwrappedPropertyHandler = src._unwrappedPropertyHandler;
_needViewProcesing = src._needViewProcesing;
_serializationShape = src._serializationShape;
_vanillaProcessing = src._vanillaProcessing;
_externalTypeIdHandler = src._externalTypeIdHandler;
}
/**
* Constructor used in cases where unwrapping-with-name-change has been
* invoked and lookup indices need to be updated.
*/
protected BeanDeserializerBase(BeanDeserializerBase src,
UnwrappedPropertyHandler unwrapHandler, PropertyBasedCreator propertyBasedCreator,
BeanPropertyMap renamedProperties, boolean ignoreAllUnknown)
{
super(src._beanType);
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_arrayDelegateDeserializer = src._arrayDelegateDeserializer;
_backRefs = src._backRefs;
_ignorableProps = src._ignorableProps;
_ignoreAllUnknown = ignoreAllUnknown;
_includableProps = src._includableProps;
_anySetter = src._anySetter;
_injectables = src._injectables;
_objectIdReader = src._objectIdReader;
_nonStandardCreation = src._nonStandardCreation;
_unwrappedPropertyHandler = unwrapHandler;
_propertyBasedCreator = propertyBasedCreator;
_beanProperties = renamedProperties;
_needViewProcesing = src._needViewProcesing;
_serializationShape = src._serializationShape;
// probably adds a twist, so:
_vanillaProcessing = false;
_externalTypeIdHandler = src._externalTypeIdHandler;
}
protected BeanDeserializerBase(BeanDeserializerBase src, ObjectIdReader oir)
{
super(src._beanType);
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_arrayDelegateDeserializer = src._arrayDelegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;
_backRefs = src._backRefs;
_ignorableProps = src._ignorableProps;
_ignoreAllUnknown = src._ignoreAllUnknown;
_includableProps = src._includableProps;
_anySetter = src._anySetter;
_injectables = src._injectables;
_nonStandardCreation = src._nonStandardCreation;
_unwrappedPropertyHandler = src._unwrappedPropertyHandler;
_needViewProcesing = src._needViewProcesing;
_serializationShape = src._serializationShape;
// then actual changes:
_objectIdReader = oir;
if (oir == null) {
_beanProperties = src._beanProperties;
_vanillaProcessing = src._vanillaProcessing;
} else {
// 18-Nov-2012, tatu: May or may not have annotations for id property;
// but no easy access. But hard to see id property being optional,
// so let's consider required at this point.
ObjectIdValueProperty idProp = new ObjectIdValueProperty(oir, PropertyMetadata.STD_REQUIRED);
_beanProperties = src._beanProperties.withProperty(idProp);
_vanillaProcessing = false;
}
_externalTypeIdHandler = src._externalTypeIdHandler;
}
/**
* @since 2.12
*/
public BeanDeserializerBase(BeanDeserializerBase src,
Set<String> ignorableProps, Set<String> includableProps)
{
super(src._beanType);
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_arrayDelegateDeserializer = src._arrayDelegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;
_backRefs = src._backRefs;
_ignorableProps = ignorableProps;
_ignoreAllUnknown = src._ignoreAllUnknown;
_includableProps = includableProps;
_anySetter = src._anySetter;
_injectables = src._injectables;
_nonStandardCreation = src._nonStandardCreation;
_unwrappedPropertyHandler = src._unwrappedPropertyHandler;
_needViewProcesing = src._needViewProcesing;
_serializationShape = src._serializationShape;
_vanillaProcessing = src._vanillaProcessing;
_objectIdReader = src._objectIdReader;
// 01-May-2016, tatu: [databind#1217]: Remove properties from mapping,
// to avoid them being deserialized
_beanProperties = src._beanProperties.withoutProperties(ignorableProps, includableProps);
_externalTypeIdHandler = src._externalTypeIdHandler;
}
protected BeanDeserializerBase(BeanDeserializerBase src, BeanPropertyMap beanProps)
{
super(src._beanType);
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_arrayDelegateDeserializer = src._arrayDelegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;
_beanProperties = beanProps;
_backRefs = src._backRefs;
_ignorableProps = src._ignorableProps;
_ignoreAllUnknown = src._ignoreAllUnknown;
_includableProps = src._includableProps;
_anySetter = src._anySetter;
_injectables = src._injectables;
_objectIdReader = src._objectIdReader;
_nonStandardCreation = src._nonStandardCreation;
_unwrappedPropertyHandler = src._unwrappedPropertyHandler;
_needViewProcesing = src._needViewProcesing;
_serializationShape = src._serializationShape;
_vanillaProcessing = src._vanillaProcessing;
_externalTypeIdHandler = src._externalTypeIdHandler;
}
public abstract BeanDeserializerBase withObjectIdReader(ObjectIdReader oir);
public abstract BeanDeserializerBase withByNameInclusion(Set<String> ignorableProps, Set<String> includableProps);
public abstract BeanDeserializerBase withIgnoreAllUnknown(boolean ignoreUnknown);
/**
* Mutant factory method that custom sub-classes must override; not left as
* abstract to prevent more drastic backwards compatibility problems.
*/
public BeanDeserializerBase withBeanProperties(BeanPropertyMap props) {
throw new UnsupportedOperationException("Class "+getClass().getName()
+" does not override `withBeanProperties()`, needs to");
}
@Override
public abstract ValueDeserializer<Object> unwrappingDeserializer(DeserializationContext ctxt,
NameTransformer unwrapper);
/**
* Fluent factory for creating a variant that can handle
* POJO output as a JSON Array. Implementations may ignore this request
* if no such input is possible.
*/
protected abstract BeanDeserializerBase asArrayDeserializer();
// @since 3.0
protected abstract void initNameMatcher(DeserializationContext ctxt);
/*
/**********************************************************************
/* Validation, post-processing
/**********************************************************************
*/
/**
* Method called to finalize setup of this deserializer,
* after deserializer itself has been registered.
* This is needed to handle recursive and transitive dependencies.
*/
@Override
public void resolve(DeserializationContext ctxt)
{
ExternalTypeHandler.Builder extTypes = null;
// if ValueInstantiator can use "creator" approach, need to resolve it here...
SettableBeanProperty[] creatorProps;
if (_valueInstantiator.canCreateFromObjectWith()) {
creatorProps = _valueInstantiator.getFromObjectArguments(ctxt.getConfig());
// 22-Jan-2018, tatu: May need to propagate "ignorable" status (from `Access.READ_ONLY`
// or perhaps class-ignorables) into Creator properties too. Cannot just delete,
// at this point, but is needed for further processing down the line
if (_ignorableProps != null || _includableProps != null) {
for (int i = 0, end = creatorProps.length; i < end; ++i) {
SettableBeanProperty prop = creatorProps[i];
if (IgnorePropertiesUtil.shouldIgnore(prop.getName(), _ignorableProps, _includableProps)) {
creatorProps[i].markAsIgnorable();
}
}
}
} else {
creatorProps = null;
}
UnwrappedPropertyHandler unwrapped = null;
// 24-Mar-2017, tatu: Looks like we may have to iterate over
// properties twice, to handle potential issues with recursive
// types (see [databind#1575] f.ex).
// First loop: find deserializer if not yet known, but do not yet
// contextualize (since that can lead to problems with self-references)
// 22-Jan-2018, tatu: NOTE! Need not check for `isIgnorable` as that can
// only happen for props in `creatorProps`
for (SettableBeanProperty prop : _beanProperties) {
// [databind#962]: no eager lookup for inject-only [creator] properties
if (prop.hasValueDeserializer() || prop.isInjectionOnly()) {
continue;
}
// [databind#125]: allow use of converters
ValueDeserializer<?> deser = _findConvertingDeserializer(ctxt, prop);
if (deser == null) {
deser = ctxt.findNonContextualValueDeserializer(prop.getType());
}
SettableBeanProperty newProp = prop.withValueDeserializer(deser);
if (prop != newProp) {
_replaceProperty(_beanProperties, creatorProps, prop, newProp);
}
}
// Second loop: contextualize, find other pieces
for (SettableBeanProperty origProp : _beanProperties) {
SettableBeanProperty prop = origProp;
ValueDeserializer<?> deser = prop.getValueDeserializer();
deser = ctxt.handlePrimaryContextualization(deser, prop, prop.getType());
prop = prop.withValueDeserializer(deser);
// Need to link managed references with matching back references
prop = _resolveManagedReferenceProperty(ctxt, prop);
// [databind#351]: need to wrap properties that require object id resolution.
if (!(prop instanceof ManagedReferenceProperty)) {
prop = _resolvedObjectIdProperty(ctxt, prop);
}
// Support unwrapped values (via @JsonUnwrapped)
NameTransformer xform = _findPropertyUnwrapper(ctxt, prop);
if (xform != null) {
ValueDeserializer<Object> orig = prop.getValueDeserializer();
ValueDeserializer<Object> unwrapping = orig.unwrappingDeserializer(ctxt, xform);
if ((unwrapping != orig) && (unwrapping != null)) {
prop = prop.withValueDeserializer(unwrapping);
if (unwrapped == null) {
unwrapped = new UnwrappedPropertyHandler();
}
if (prop.isCreatorProperty()) {
unwrapped.addCreatorProperty(prop);
} else {
unwrapped.addProperty(prop);
}
// 12-Dec-2014, tatu: As per [databind#647], we will have problems if
// the original property is left in place. So let's remove it now.
// 25-Mar-2017, tatu: Wonder if this could be problematic wrt creators?
// (that is, should we remove it from creator too)
_beanProperties.remove(prop);
continue;
}
}
// 26-Oct-2016, tatu: Need to have access to value deserializer to know if
// merging needed, and now seems to be reasonable time to do that.
final PropertyMetadata md = prop.getMetadata();
prop = _resolveMergeAndNullSettings(ctxt, prop, md);
// non-static inner classes too:
prop = _resolveInnerClassValuedProperty(ctxt, prop);
if (prop != origProp) {
_replaceProperty(_beanProperties, creatorProps, origProp, prop);
}
// one more thing: if this property uses "external property" type inclusion,
// it needs different handling altogether
if (prop.hasValueTypeDeserializer()) {
TypeDeserializer typeDeser = prop.getValueTypeDeserializer();
if (typeDeser.getTypeInclusion() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
if (extTypes == null) {
extTypes = ExternalTypeHandler.builder(_beanType);
}
extTypes.addExternal(prop, typeDeser);
// In fact, remove from list of known properties to simplify later handling
_beanProperties.remove(prop);
continue;
}
}
}
// "any setter" may also need to be resolved now
if ((_anySetter != null) && !_anySetter.hasValueDeserializer()) {
_anySetter = _anySetter.withValueDeserializer(findDeserializer(ctxt,
_anySetter.getType(), _anySetter.getProperty()));
}
// as well as delegate-based constructor:
if (_valueInstantiator.canCreateUsingDelegate()) {
JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
if (delegateType == null) {
ctxt.reportBadDefinition(_beanType, String.format(
"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'",
ClassUtil.getTypeDescription(_beanType), ClassUtil.classNameOf(_valueInstantiator)));
}
_delegateDeserializer = _findDelegateDeserializer(ctxt, delegateType,
_valueInstantiator.getDelegateCreator());
}
// and array-delegate-based constructor:
if (_valueInstantiator.canCreateUsingArrayDelegate()) {
JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
if (delegateType == null) {
ctxt.reportBadDefinition(_beanType, String.format(
"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'",
ClassUtil.getTypeDescription(_beanType), ClassUtil.classNameOf(_valueInstantiator)));
}
_arrayDelegateDeserializer = _findDelegateDeserializer(ctxt, delegateType,
_valueInstantiator.getArrayDelegateCreator());
}
// And now that we know CreatorProperty instances are also resolved can finally create the creator:
if (creatorProps != null) {
_propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator,
creatorProps, _beanProperties);
}
if (extTypes != null) {
// 21-Jun-2016, tatu: related to [databind#999], may need to link type ids too,
// so need to pass collected properties
_externalTypeIdHandler = extTypes.build(_beanProperties);
// we consider this non-standard, to offline handling
_nonStandardCreation = true;
}
if (unwrapped != null) { // we consider this non-standard, to offline handling
_nonStandardCreation = true;
// [databind#650]: Initialize unwrapped property names for hasUnwrappedProperty()
_unwrappedPropertyHandler = unwrapped.initializeUnwrappedPropertyNames();
} else {
_unwrappedPropertyHandler = null;
}
// may need to disable vanilla processing, if unwrapped handling was enabled...
_vanillaProcessing = _vanillaProcessing && !_nonStandardCreation;
}
protected void _replaceProperty(BeanPropertyMap props, SettableBeanProperty[] creatorProps,
SettableBeanProperty origProp, SettableBeanProperty newProp)
{
props.replace(origProp, newProp);
// [databind#795]: Make sure PropertyBasedCreator's properties stay in sync
if (creatorProps != null) {
// 18-May-2015, tatu: _Should_ start with consistent set. But can we really
// fully count on this? May need to revisit in future; seems to hold for now.
for (int i = 0, len = creatorProps.length; i < len; ++i) {
if (creatorProps[i] == origProp) {
creatorProps[i] = newProp;
return;
}
}
/*
// ... as per above, it is possible we'd need to add this as fallback
// if (but only if) identity check fails?
for (int i = 0, len = creatorProps.length; i < len; ++i) {
if (creatorProps[i].getName().equals(origProp.getName())) {
creatorProps[i] = newProp;
return;
}
}
*/
}
}
@SuppressWarnings("unchecked")
private ValueDeserializer<Object> _findDelegateDeserializer(DeserializationContext ctxt,
JavaType delegateType, AnnotatedWithParams delegateCreator)
{
// 27-Nov-2023, tatu: [databind#4200] Need to resolve PropertyMetadata.
// And all we have is the actual Creator method; but for annotations
// we actually need the one parameter -- if there is one
// (NOTE! This would not work for case of more than one parameter with
// delegation, others injected)
final BeanProperty property;
if ((delegateCreator != null) && (delegateCreator.getParameterCount() == 1)) {
AnnotatedMember delegator = delegateCreator.getParameter(0);
PropertyMetadata propMd = _getSetterInfo(ctxt, delegator, delegateType);
property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
delegateType, null, delegator, propMd);
} else {
// No creator indicated; or Zero, or more than 2 arguments (since we don't
// know which one is the "real" delegating parameter. Although could possibly
// figure it out if someone provides actual use case
property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
delegateType, null, delegateCreator,
PropertyMetadata.STD_OPTIONAL);
}
TypeDeserializer td = (TypeDeserializer) delegateType.getTypeHandler();
if (td == null) {
td = ctxt.findTypeDeserializer(delegateType);
}
// 04-May-2018, tatu: [databind#2021] check if there's custom deserializer attached
// to type (resolved from parameter)
ValueDeserializer<Object> dd = (ValueDeserializer<Object>) delegateType.getValueHandler();
if (dd == null) {
dd = findDeserializer(ctxt, delegateType, property);
} else {
dd = (ValueDeserializer<Object>) ctxt.handleSecondaryContextualization(dd, property, delegateType);
}
if (td != null) {
td = td.forProperty(property);
return new TypeWrappedDeserializer(td, dd);
}
return dd;
}
/**
* Method essentially copied from {@code BasicDeserializerFactory},
* needed to find {@link PropertyMetadata} for Delegating Creator,
* for access to annotation-derived info.
*/
protected PropertyMetadata _getSetterInfo(DeserializationContext ctxt,
AnnotatedMember accessor, JavaType type)
{
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
final DeserializationConfig config = ctxt.getConfig();
PropertyMetadata metadata = PropertyMetadata.STD_OPTIONAL;
boolean needMerge = true;
Nulls valueNulls = null;
Nulls contentNulls = null;
// NOTE: compared to `POJOPropertyBuilder`, we only have access to creator
// parameter, not other accessors, so code bit simpler
// Ok, first: does property itself have something to say?
if (intr != null) {
JsonSetter.Value setterInfo = intr.findSetterInfo(config, accessor);
if (setterInfo != null) {
valueNulls = setterInfo.nonDefaultValueNulls();
contentNulls = setterInfo.nonDefaultContentNulls();
}
}
// If not, config override?
if (needMerge || (valueNulls == null) || (contentNulls == null)) {
ConfigOverride co = config.getConfigOverride(type.getRawClass());
JsonSetter.Value setterInfo = co.getNullHandling();
if (setterInfo != null) {
if (valueNulls == null) {
valueNulls = setterInfo.nonDefaultValueNulls();
}
if (contentNulls == null) {
contentNulls = setterInfo.nonDefaultContentNulls();
}
}
}
if (needMerge || (valueNulls == null) || (contentNulls == null)) {
JsonSetter.Value setterInfo = config.getDefaultNullHandling();
if (valueNulls == null) {
valueNulls = setterInfo.nonDefaultValueNulls();
}
if (contentNulls == null) {
contentNulls = setterInfo.nonDefaultContentNulls();
}
}
if ((valueNulls != null) || (contentNulls != null)) {
metadata = metadata.withNulls(valueNulls, contentNulls);
}
return metadata;
}
/**
* Helper method that can be used to see if specified property is annotated
* to indicate use of a converter for property value (in case of container types,
* it is container type itself, not key or content type).
*<p>
* NOTE: returned deserializer is NOT yet contextualized, caller needs to take
* care to do that.
*/
protected ValueDeserializer<Object> _findConvertingDeserializer(DeserializationContext ctxt,
SettableBeanProperty prop)
{
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
if (intr != null) {
Object convDef = intr.findDeserializationConverter(ctxt.getConfig(), prop.getMember());
if (convDef != null) {
Converter<Object,Object> conv = ctxt.converterInstance(prop.getMember(), convDef);
JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());
// 25-Mar-2017, tatu: should not yet contextualize
// ValueDeserializer<?> deser = ctxt.findContextualValueDeserializer(delegateType, prop);
ValueDeserializer<?> deser = ctxt.findNonContextualValueDeserializer(delegateType);
return new StdConvertingDeserializer<Object>(conv, delegateType, deser);
}
}
return null;
}
/**
* Although most of post-processing is done in resolve(), we only get
* access to referring property's annotations here; and this is needed
* to support per-property ObjectIds.
* We will also consider Shape transformations (read from Array) at this
* point, since it may come from either Class definition or property.
*/
@Override
public ValueDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
{
ObjectIdReader oir = _objectIdReader;
// First: may have an override for Object Id:
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
final DeserializationConfig config = ctxt.getConfig();
final AnnotatedMember accessor = _neitherNull(property, intr) ? property.getMember() : null;
if (accessor != null) {
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(config, accessor);
if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
// 2.1: allow modifications by "id ref" annotations as well:
objectIdInfo = intr.findObjectReferenceInfo(config, accessor, objectIdInfo);
Class<?> implClass = objectIdInfo.getGeneratorType();
// Property-based generator is trickier
JavaType idType;
SettableBeanProperty idProp;
ObjectIdGenerator<?> idGen;
ObjectIdResolver resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
PropertyName propName = objectIdInfo.getPropertyName();
idProp = findProperty(propName);
if (idProp == null) {
return ctxt.reportBadDefinition(_beanType, String.format(
"Invalid Object Id definition for %s: cannot find property with name %s",
ClassUtil.nameOf(handledType()), ClassUtil.name(propName)));
}
idType = idProp.getType();
idGen = new PropertyBasedObjectIdGenerator(objectIdInfo.getScope());
} else { // other types are to be simpler
JavaType type = ctxt.constructType(implClass);
idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
idProp = null;
idGen = ctxt.objectIdGeneratorInstance(accessor, objectIdInfo);
}
ValueDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
oir = ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(),
idGen, deser, idProp, resolver);
}
}
// either way, need to resolve serializer:
BeanDeserializerBase contextual = this;
if (oir != null && oir != _objectIdReader) {
contextual = contextual.withObjectIdReader(oir);
}
// And possibly add more properties to ignore
if (accessor != null) {
contextual = _handleByNameInclusion(ctxt, intr, contextual, accessor);
}
// One more thing: are we asked to serialize POJO as array?
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
JsonFormat.Shape shape = null;
if (format != null) {
if (format.hasShape()) {
shape = format.getShape();
}
// 16-May-2016, tatu: How about per-property case-insensitivity?
Boolean B = format.getFeature(JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
if (B != null) {
// [databind#5962]: must rebuild from the (possibly filtered) contextual
// BeanPropertyMap so that per-property @JsonIgnoreProperties exclusions
// applied by _handleByNameInclusion() above are preserved.
BeanPropertyMap propsOrig = contextual._beanProperties;
BeanPropertyMap props = propsOrig.withCaseInsensitivity(B.booleanValue());
if (props != propsOrig) {
contextual = contextual.withBeanProperties(props);
}
}
}
contextual.initNameMatcher(ctxt);
if (shape == null) {
shape = _serializationShape;
}
if (shape == JsonFormat.Shape.ARRAY) {
// [databind#4277]: Detect incompatible combination of ARRAY shape with EXTERNAL_PROPERTY
if (contextual._externalTypeIdHandler != null) {
return (BeanDeserializerBase) ctxt.reportBadDefinition(_beanType,
"""
Cannot use `@JsonFormat(shape=JsonFormat.Shape.ARRAY)` with `@JsonTypeInfo(include=JsonTypeInfo.As.EXTERNAL_PROPERTY)` for type %s: `EXTERNAL_PROPERTY` requires object-style JSON with named properties, but `ARRAY` shape uses positional JSON arrays.
Working alternatives:
1. Use `@JsonTypeInfo(include=JsonTypeInfo.As.PROPERTY)` - type ID inside value object
2. Use `@JsonTypeInfo(include=JsonTypeInfo.As.WRAPPER_ARRAY)` - wrap value with type
3. Implement a custom deserializer for protocol-specific requirements
""".formatted(ClassUtil.getTypeDescription(_beanType)));
}
contextual = contextual.asArrayDeserializer();
}
return contextual;
}
protected BeanDeserializerBase _handleByNameInclusion(DeserializationContext ctxt,
AnnotationIntrospector intr,
BeanDeserializerBase contextual,
AnnotatedMember accessor)
{
final DeserializationConfig config = ctxt.getConfig();
JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnoralByName(config, accessor);
// 30-Mar-2020, tatu: As per [databind#2627], need to also allow
// per-property override to "ignore all unknown".
// NOTE: there is no way to override with `false` because annotation
// defaults to `false` (i.e. cannot know if `false` is explicit value)
if (ignorals.getIgnoreUnknown() && !_ignoreAllUnknown) {
contextual = contextual.withIgnoreAllUnknown(true);
}
final Set<String> namesToIgnore = ignorals.findIgnoredForDeserialization();
final Set<String> prevNamesToIgnore = contextual._ignorableProps;
final Set<String> newNamesToIgnore;
if (namesToIgnore.isEmpty()) {
newNamesToIgnore = prevNamesToIgnore;
} else if ((prevNamesToIgnore == null) || prevNamesToIgnore.isEmpty()) {
newNamesToIgnore = namesToIgnore;
} else {
newNamesToIgnore = new HashSet<String>(prevNamesToIgnore);
newNamesToIgnore.addAll(namesToIgnore);
}
final Set<String> prevNamesToInclude = contextual._includableProps;
final Set<String> newNamesToInclude = IgnorePropertiesUtil.combineNamesToInclude(prevNamesToInclude,
intr.findPropertyInclusionByName(config, accessor).getIncluded());
if ((newNamesToIgnore != prevNamesToIgnore)
|| (newNamesToInclude != prevNamesToInclude)) {
contextual = contextual.withByNameInclusion(newNamesToIgnore, newNamesToInclude);
}
return contextual;
}
/**
* Helper method called to see if given property is part of 'managed' property
* pair (managed + back reference), and if so, handle resolution details.
*/
protected SettableBeanProperty _resolveManagedReferenceProperty(DeserializationContext ctxt,
SettableBeanProperty prop)
{
String refName = prop.getManagedReferenceName();
if (refName == null) {
return prop;
}
ValueDeserializer<?> valueDeser = prop.getValueDeserializer();
SettableBeanProperty backProp = valueDeser.findBackReference(refName);
if (backProp == null) {
// [databind#3304]: Provide more informative error message when the property
// is a container type with abstract/interface content type -- common case is
// when @JsonBackReference only exists on a subtype, not on the declared
// (abstract/interface) element type
JavaType propType = prop.getType();
String msg;
JavaType ct;
if (propType.isContainerType()
&& (ct = propType.getContentType()) != null
&& ct.isAbstract()) {
msg = String.format(
"Cannot handle managed/back reference %s: no back reference property found from type %s"
+" (content type %s is abstract: `@JsonBackReference` must be defined on the abstract"
+" type or interface, not just on its subtypes)",
ClassUtil.name(refName), ClassUtil.getTypeDescription(propType),
ClassUtil.getTypeDescription(ct));
} else {
msg = String.format(
"Cannot handle managed/back reference %s: no back reference property found from type %s",
ClassUtil.name(refName), ClassUtil.getTypeDescription(propType));
}
return ctxt.reportBadDefinition(_beanType, msg);
}
// also: verify that type is compatible
JavaType referredType = _beanType;
JavaType backRefType = backProp.getType();
boolean isContainer = prop.getType().isContainerType();
if (!backRefType.getRawClass().isAssignableFrom(referredType.getRawClass())) {
ctxt.reportBadDefinition(_beanType, String.format(
"Cannot handle managed/back reference %s: back reference type (%s) not compatible with managed type (%s)",
ClassUtil.name(refName), ClassUtil.getTypeDescription(backRefType),
referredType.getRawClass().getName()));
}
return new ManagedReferenceProperty(prop, refName, backProp, isContainer);
}
/**
* Method that wraps given property with {@link ObjectIdReferenceProperty}
* in case where object id resolution is required.
*/
protected SettableBeanProperty _resolvedObjectIdProperty(DeserializationContext ctxt,
SettableBeanProperty prop)
{
ObjectIdInfo objectIdInfo = prop.getObjectIdInfo();
ValueDeserializer<Object> valueDeser = prop.getValueDeserializer();
ObjectIdReader objectIdReader = (valueDeser == null) ? null : valueDeser.getObjectIdReader(ctxt);
if (objectIdInfo == null && objectIdReader == null) {