1 package com.fasterxml.jackson.databind.deser;
2
3 import java.io.IOException;
4 import java.util.*;
5
6 import com.fasterxml.jackson.core.*;
7 import com.fasterxml.jackson.databind.*;
8 import com.fasterxml.jackson.databind.deser.impl.*;
9 import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring;
10 import com.fasterxml.jackson.databind.util.NameTransformer;
11 import com.fasterxml.jackson.databind.util.TokenBuffer;
12
13
17 public class BeanDeserializer
18 extends BeanDeserializerBase
19 implements java.io.Serializable
20 {
21
31
32 private static final long serialVersionUID = 1L;
33
34
40 protected transient Exception _nullFromCreator;
41
42
48 private volatile transient NameTransformer _currentlyTransforming;
49
50
55
56
59 public BeanDeserializer(BeanDeserializerBuilder builder, BeanDescription beanDesc,
60 BeanPropertyMap properties, Map<String, SettableBeanProperty> backRefs,
61 HashSet<String> ignorableProps, boolean ignoreAllUnknown,
62 boolean hasViews)
63 {
64 super(builder, beanDesc, properties, backRefs,
65 ignorableProps, ignoreAllUnknown, hasViews);
66 }
67
68
72 protected BeanDeserializer(BeanDeserializerBase src) {
73 super(src, src._ignoreAllUnknown);
74 }
75
76 protected BeanDeserializer(BeanDeserializerBase src, boolean ignoreAllUnknown) {
77 super(src, ignoreAllUnknown);
78 }
79
80 protected BeanDeserializer(BeanDeserializerBase src, NameTransformer unwrapper) {
81 super(src, unwrapper);
82 }
83
84 public BeanDeserializer(BeanDeserializerBase src, ObjectIdReader oir) {
85 super(src, oir);
86 }
87
88 public BeanDeserializer(BeanDeserializerBase src, Set<String> ignorableProps) {
89 super(src, ignorableProps);
90 }
91
92 public BeanDeserializer(BeanDeserializerBase src, BeanPropertyMap props) {
93 super(src, props);
94 }
95
96 @Override
97 public JsonDeserializer<Object> unwrappingDeserializer(NameTransformer transformer)
98 {
99
100
101 if (getClass() != BeanDeserializer.class) {
102 return this;
103 }
104
105
106 if (_currentlyTransforming == transformer) {
107 return this;
108 }
109 _currentlyTransforming = transformer;
110 try {
111 return new BeanDeserializer(this, transformer);
112 } finally { _currentlyTransforming = null; }
113 }
114
115 @Override
116 public BeanDeserializer withObjectIdReader(ObjectIdReader oir) {
117 return new BeanDeserializer(this, oir);
118 }
119
120 @Override
121 public BeanDeserializer withIgnorableProperties(Set<String> ignorableProps) {
122 return new BeanDeserializer(this, ignorableProps);
123 }
124
125 @Override
126 public BeanDeserializerBase withIgnoreAllUnknown(boolean ignoreUnknown) {
127 return new BeanDeserializer(this, ignoreUnknown);
128 }
129
130 @Override
131 public BeanDeserializerBase withBeanProperties(BeanPropertyMap props) {
132 return new BeanDeserializer(this, props);
133 }
134
135 @Override
136 protected BeanDeserializerBase asArrayDeserializer() {
137 SettableBeanProperty[] props = _beanProperties.getPropertiesInInsertionOrder();
138 return new BeanAsArrayDeserializer(this, props);
139 }
140
141
146
147
150 @Override
151 public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
152 {
153
154 if (p.isExpectedStartObjectToken()) {
155 if (_vanillaProcessing) {
156 return vanillaDeserialize(p, ctxt, p.nextToken());
157 }
158
159
160 p.nextToken();
161 if (_objectIdReader != null) {
162 return deserializeWithObjectId(p, ctxt);
163 }
164 return deserializeFromObject(p, ctxt);
165 }
166 return _deserializeOther(p, ctxt, p.getCurrentToken());
167 }
168
169 protected final Object _deserializeOther(JsonParser p, DeserializationContext ctxt,
170 JsonToken t) throws IOException
171 {
172
173 if (t != null) {
174 switch (t) {
175 case VALUE_STRING:
176 return deserializeFromString(p, ctxt);
177 case VALUE_NUMBER_INT:
178 return deserializeFromNumber(p, ctxt);
179 case VALUE_NUMBER_FLOAT:
180 return deserializeFromDouble(p, ctxt);
181 case VALUE_EMBEDDED_OBJECT:
182 return deserializeFromEmbedded(p, ctxt);
183 case VALUE_TRUE:
184 case VALUE_FALSE:
185 return deserializeFromBoolean(p, ctxt);
186 case VALUE_NULL:
187 return deserializeFromNull(p, ctxt);
188 case START_ARRAY:
189
190 return _deserializeFromArray(p, ctxt);
191 case FIELD_NAME:
192 case END_OBJECT:
193 if (_vanillaProcessing) {
194 return vanillaDeserialize(p, ctxt, t);
195 }
196 if (_objectIdReader != null) {
197 return deserializeWithObjectId(p, ctxt);
198 }
199 return deserializeFromObject(p, ctxt);
200 default:
201 }
202 }
203 return ctxt.handleUnexpectedToken(getValueType(ctxt), p);
204 }
205
206 @Deprecated
207 protected Object _missingToken(JsonParser p, DeserializationContext ctxt) throws IOException {
208 throw ctxt.endOfInputException(handledType());
209 }
210
211
216 @Override
217 public Object deserialize(JsonParser p, DeserializationContext ctxt, Object bean) throws IOException
218 {
219
220 p.setCurrentValue(bean);
221 if (_injectables != null) {
222 injectValues(ctxt, bean);
223 }
224 if (_unwrappedPropertyHandler != null) {
225 return deserializeWithUnwrapped(p, ctxt, bean);
226 }
227 if (_externalTypeIdHandler != null) {
228 return deserializeWithExternalTypeId(p, ctxt, bean);
229 }
230 String propName;
231
232
233 if (p.isExpectedStartObjectToken()) {
234 propName = p.nextFieldName();
235 if (propName == null) {
236 return bean;
237 }
238 } else {
239 if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
240 propName = p.getCurrentName();
241 } else {
242 return bean;
243 }
244 }
245 if (_needViewProcesing) {
246 Class<?> view = ctxt.getActiveView();
247 if (view != null) {
248 return deserializeWithView(p, ctxt, bean, view);
249 }
250 }
251 do {
252 p.nextToken();
253 SettableBeanProperty prop = _beanProperties.find(propName);
254
255 if (prop != null) {
256 try {
257 prop.deserializeAndSet(p, ctxt, bean);
258 } catch (Exception e) {
259 wrapAndThrow(e, bean, propName, ctxt);
260 }
261 continue;
262 }
263 handleUnknownVanilla(p, ctxt, bean, propName);
264 } while ((propName = p.nextFieldName()) != null);
265 return bean;
266 }
267
268
273
274
278 private final Object vanillaDeserialize(JsonParser p,
279 DeserializationContext ctxt, JsonToken t)
280 throws IOException
281 {
282 final Object bean = _valueInstantiator.createUsingDefault(ctxt);
283
284 p.setCurrentValue(bean);
285 if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
286 String propName = p.getCurrentName();
287 do {
288 p.nextToken();
289 SettableBeanProperty prop = _beanProperties.find(propName);
290
291 if (prop != null) {
292 try {
293 prop.deserializeAndSet(p, ctxt, bean);
294 } catch (Exception e) {
295 wrapAndThrow(e, bean, propName, ctxt);
296 }
297 continue;
298 }
299 handleUnknownVanilla(p, ctxt, bean, propName);
300 } while ((propName = p.nextFieldName()) != null);
301 }
302 return bean;
303 }
304
305
308 @Override
309 public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException
310 {
311
318 if ((_objectIdReader != null) && _objectIdReader.maySerializeAsObject()) {
319 if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)
320 && _objectIdReader.isValidReferencePropertyName(p.getCurrentName(), p)) {
321 return deserializeFromObjectId(p, ctxt);
322 }
323 }
324 if (_nonStandardCreation) {
325 if (_unwrappedPropertyHandler != null) {
326 return deserializeWithUnwrapped(p, ctxt);
327 }
328 if (_externalTypeIdHandler != null) {
329 return deserializeWithExternalTypeId(p, ctxt);
330 }
331 Object bean = deserializeFromObjectUsingNonDefault(p, ctxt);
332
336
344 return bean;
345 }
346 final Object bean = _valueInstantiator.createUsingDefault(ctxt);
347
348 p.setCurrentValue(bean);
349 if (p.canReadObjectId()) {
350 Object id = p.getObjectId();
351 if (id != null) {
352 _handleTypedObjectId(p, ctxt, bean, id);
353 }
354 }
355 if (_injectables != null) {
356 injectValues(ctxt, bean);
357 }
358 if (_needViewProcesing) {
359 Class<?> view = ctxt.getActiveView();
360 if (view != null) {
361 return deserializeWithView(p, ctxt, bean, view);
362 }
363 }
364 if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
365 String propName = p.getCurrentName();
366 do {
367 p.nextToken();
368 SettableBeanProperty prop = _beanProperties.find(propName);
369 if (prop != null) {
370 try {
371 prop.deserializeAndSet(p, ctxt, bean);
372 } catch (Exception e) {
373 wrapAndThrow(e, bean, propName, ctxt);
374 }
375 continue;
376 }
377 handleUnknownVanilla(p, ctxt, bean, propName);
378 } while ((propName = p.nextFieldName()) != null);
379 }
380 return bean;
381 }
382
383
391 @Override
392 @SuppressWarnings("resource")
393 protected Object _deserializeUsingPropertyBased(final JsonParser p, final DeserializationContext ctxt)
394 throws IOException
395 {
396 final PropertyBasedCreator creator = _propertyBasedCreator;
397 PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
398 TokenBuffer unknown = null;
399 final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
400
401 JsonToken t = p.getCurrentToken();
402 List<BeanReferring> referrings = null;
403 for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
404 String propName = p.getCurrentName();
405 p.nextToken();
406
407 if (buffer.readIdProperty(propName)) {
408 continue;
409 }
410
411 SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
412 if (creatorProp != null) {
413
414 Object value;
415 if ((activeView != null) && !creatorProp.visibleInView(activeView)) {
416 p.skipChildren();
417 continue;
418 }
419 value = _deserializeWithErrorWrapping(p, ctxt, creatorProp);
420 if (buffer.assignParameter(creatorProp, value)) {
421 p.nextToken();
422 Object bean;
423 try {
424 bean = creator.build(ctxt, buffer);
425 } catch (Exception e) {
426 bean = wrapInstantiationProblem(e, ctxt);
427 }
428 if (bean == null) {
429 return ctxt.handleInstantiationProblem(handledType(), null,
430 _creatorReturnedNullException());
431 }
432
433 p.setCurrentValue(bean);
434
435
436 if (bean.getClass() != _beanType.getRawClass()) {
437 return handlePolymorphic(p, ctxt, bean, unknown);
438 }
439 if (unknown != null) {
440 bean = handleUnknownProperties(ctxt, bean, unknown);
441 }
442
443 return deserialize(p, ctxt, bean);
444 }
445 continue;
446 }
447
448 SettableBeanProperty prop = _beanProperties.find(propName);
449 if (prop != null) {
450 try {
451 buffer.bufferProperty(prop, _deserializeWithErrorWrapping(p, ctxt, prop));
452 } catch (UnresolvedForwardReference reference) {
453
454
455
456 BeanReferring referring = handleUnresolvedReference(ctxt,
457 prop, buffer, reference);
458 if (referrings == null) {
459 referrings = new ArrayList<BeanReferring>();
460 }
461 referrings.add(referring);
462 }
463 continue;
464 }
465
466 if (_ignorableProps != null && _ignorableProps.contains(propName)) {
467 handleIgnoredProperty(p, ctxt, handledType(), propName);
468 continue;
469 }
470
471 if (_anySetter != null) {
472 try {
473 buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
474 } catch (Exception e) {
475 wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
476 }
477 continue;
478 }
479
480 if (unknown == null) {
481 unknown = new TokenBuffer(p, ctxt);
482 }
483 unknown.writeFieldName(propName);
484 unknown.copyCurrentStructure(p);
485 }
486
487
488 Object bean;
489 try {
490 bean = creator.build(ctxt, buffer);
491 } catch (Exception e) {
492 wrapInstantiationProblem(e, ctxt);
493 bean = null;
494 }
495
496 if (_injectables != null) {
497 injectValues(ctxt, bean);
498 }
499
500 if (referrings != null) {
501 for (BeanReferring referring : referrings) {
502 referring.setBean(bean);
503 }
504 }
505 if (unknown != null) {
506
507 if (bean.getClass() != _beanType.getRawClass()) {
508 return handlePolymorphic(null, ctxt, bean, unknown);
509 }
510
511 return handleUnknownProperties(ctxt, bean, unknown);
512 }
513 return bean;
514 }
515
516
519 private BeanReferring handleUnresolvedReference(DeserializationContext ctxt,
520 SettableBeanProperty prop, PropertyValueBuffer buffer,
521 UnresolvedForwardReference reference)
522 throws JsonMappingException
523 {
524 BeanReferring referring = new BeanReferring(ctxt, reference,
525 prop.getType(), buffer, prop);
526 reference.getRoid().appendReferring(referring);
527 return referring;
528 }
529
530 protected final Object _deserializeWithErrorWrapping(JsonParser p,
531 DeserializationContext ctxt, SettableBeanProperty prop)
532 throws IOException
533 {
534 try {
535 return prop.deserialize(p, ctxt);
536 } catch (Exception e) {
537 wrapAndThrow(e, _beanType.getRawClass(), prop.getName(), ctxt);
538
539 return null;
540 }
541 }
542
543
551 protected Object deserializeFromNull(JsonParser p, DeserializationContext ctxt)
552 throws IOException
553 {
554
555
556
557 if (p.requiresCustomCodec()) {
558 @SuppressWarnings("resource")
559 TokenBuffer tb = new TokenBuffer(p, ctxt);
560 tb.writeEndObject();
561 JsonParser p2 = tb.asParser(p);
562 p2.nextToken();
563
564 Object ob = _vanillaProcessing ? vanillaDeserialize(p2, ctxt, JsonToken.END_OBJECT)
565 : deserializeFromObject(p2, ctxt);
566 p2.close();
567 return ob;
568 }
569 return ctxt.handleUnexpectedToken(getValueType(ctxt), p);
570 }
571
572 @Override
573 protected Object _deserializeFromArray(JsonParser p, DeserializationContext ctxt) throws IOException
574 {
575
576 JsonDeserializer<Object> delegateDeser = _arrayDelegateDeserializer;
577
578 if ((delegateDeser != null) || ((delegateDeser = _delegateDeserializer) != null)) {
579 Object bean = _valueInstantiator.createUsingArrayDelegate(ctxt,
580 delegateDeser.deserialize(p, ctxt));
581 if (_injectables != null) {
582 injectValues(ctxt, bean);
583 }
584 return bean;
585 }
586 if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
587 JsonToken t = p.nextToken();
588 if (t == JsonToken.END_ARRAY && ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
589 return null;
590 }
591 final Object value = deserialize(p, ctxt);
592 if (p.nextToken() != JsonToken.END_ARRAY) {
593 handleMissingEndArrayForSingle(p, ctxt);
594 }
595 return value;
596 }
597 if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
598 JsonToken t = p.nextToken();
599 if (t == JsonToken.END_ARRAY) {
600 return null;
601 }
602 return ctxt.handleUnexpectedToken(getValueType(ctxt), JsonToken.START_ARRAY, p, null);
603 }
604 return ctxt.handleUnexpectedToken(getValueType(ctxt), p);
605 }
606
607
612
613 protected final Object deserializeWithView(JsonParser p, DeserializationContext ctxt,
614 Object bean, Class<?> activeView)
615 throws IOException
616 {
617 if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
618 String propName = p.getCurrentName();
619 do {
620 p.nextToken();
621
622 SettableBeanProperty prop = _beanProperties.find(propName);
623 if (prop != null) {
624 if (!prop.visibleInView(activeView)) {
625 p.skipChildren();
626 continue;
627 }
628 try {
629 prop.deserializeAndSet(p, ctxt, bean);
630 } catch (Exception e) {
631 wrapAndThrow(e, bean, propName, ctxt);
632 }
633 continue;
634 }
635 handleUnknownVanilla(p, ctxt, bean, propName);
636 } while ((propName = p.nextFieldName()) != null);
637 }
638 return bean;
639 }
640
641
646
647
651 @SuppressWarnings("resource")
652 protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt)
653 throws IOException
654 {
655 if (_delegateDeserializer != null) {
656 return _valueInstantiator.createUsingDelegate(ctxt, _delegateDeserializer.deserialize(p, ctxt));
657 }
658 if (_propertyBasedCreator != null) {
659 return deserializeUsingPropertyBasedWithUnwrapped(p, ctxt);
660 }
661 TokenBuffer tokens = new TokenBuffer(p, ctxt);
662 tokens.writeStartObject();
663 final Object bean = _valueInstantiator.createUsingDefault(ctxt);
664
665
666 p.setCurrentValue(bean);
667
668 if (_injectables != null) {
669 injectValues(ctxt, bean);
670 }
671 final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
672 String propName = p.hasTokenId(JsonTokenId.ID_FIELD_NAME) ? p.getCurrentName() : null;
673
674 for (; propName != null; propName = p.nextFieldName()) {
675 p.nextToken();
676 SettableBeanProperty prop = _beanProperties.find(propName);
677 if (prop != null) {
678 if ((activeView != null) && !prop.visibleInView(activeView)) {
679 p.skipChildren();
680 continue;
681 }
682 try {
683 prop.deserializeAndSet(p, ctxt, bean);
684 } catch (Exception e) {
685 wrapAndThrow(e, bean, propName, ctxt);
686 }
687 continue;
688 }
689
690 if (_ignorableProps != null && _ignorableProps.contains(propName)) {
691 handleIgnoredProperty(p, ctxt, bean, propName);
692 continue;
693 }
694
695
696
697
698 if (_anySetter == null) {
699
700 tokens.writeFieldName(propName);
701 tokens.copyCurrentStructure(p);
702 continue;
703 }
704
705 TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
706 tokens.writeFieldName(propName);
707 tokens.append(b2);
708 try {
709 _anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
710 } catch (Exception e) {
711 wrapAndThrow(e, bean, propName, ctxt);
712 }
713 }
714 tokens.writeEndObject();
715 _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
716 return bean;
717 }
718
719 @SuppressWarnings("resource")
720 protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt,
721 Object bean)
722 throws IOException
723 {
724 JsonToken t = p.getCurrentToken();
725 if (t == JsonToken.START_OBJECT) {
726 t = p.nextToken();
727 }
728 TokenBuffer tokens = new TokenBuffer(p, ctxt);
729 tokens.writeStartObject();
730 final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
731 for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
732 String propName = p.getCurrentName();
733 SettableBeanProperty prop = _beanProperties.find(propName);
734 p.nextToken();
735 if (prop != null) {
736 if (activeView != null && !prop.visibleInView(activeView)) {
737 p.skipChildren();
738 continue;
739 }
740 try {
741 prop.deserializeAndSet(p, ctxt, bean);
742 } catch (Exception e) {
743 wrapAndThrow(e, bean, propName, ctxt);
744 }
745 continue;
746 }
747 if (_ignorableProps != null && _ignorableProps.contains(propName)) {
748 handleIgnoredProperty(p, ctxt, bean, propName);
749 continue;
750 }
751
752
753
754
755 if (_anySetter == null) {
756
757 tokens.writeFieldName(propName);
758 tokens.copyCurrentStructure(p);
759 } else {
760
761 TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
762 tokens.writeFieldName(propName);
763 tokens.append(b2);
764 try {
765 _anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
766 } catch (Exception e) {
767 wrapAndThrow(e, bean, propName, ctxt);
768 }
769 continue;
770 }
771 }
772 tokens.writeEndObject();
773 _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
774 return bean;
775 }
776
777 @SuppressWarnings("resource")
778 protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, DeserializationContext ctxt)
779 throws IOException
780 {
781
782
783
784
785 final PropertyBasedCreator creator = _propertyBasedCreator;
786 PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
787
788 TokenBuffer tokens = new TokenBuffer(p, ctxt);
789 tokens.writeStartObject();
790
791 JsonToken t = p.getCurrentToken();
792 for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
793 String propName = p.getCurrentName();
794 p.nextToken();
795
796 SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
797 if (creatorProp != null) {
798
799 if (buffer.assignParameter(creatorProp,
800 _deserializeWithErrorWrapping(p, ctxt, creatorProp))) {
801 t = p.nextToken();
802 Object bean;
803 try {
804 bean = creator.build(ctxt, buffer);
805 } catch (Exception e) {
806 bean = wrapInstantiationProblem(e, ctxt);
807 }
808
809 p.setCurrentValue(bean);
810
811 while (t == JsonToken.FIELD_NAME) {
812
813 tokens.copyCurrentStructure(p);
814 t = p.nextToken();
815 }
816
817
818 if (t != JsonToken.END_OBJECT) {
819 ctxt.reportWrongTokenException(this, JsonToken.END_OBJECT,
820 "Attempted to unwrap '%s' value",
821 handledType().getName());
822 }
823 tokens.writeEndObject();
824 if (bean.getClass() != _beanType.getRawClass()) {
825
826
827 ctxt.reportInputMismatch(creatorProp,
828 "Cannot create polymorphic instances with unwrapped values");
829 return null;
830 }
831 return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
832 }
833 continue;
834 }
835
836 if (buffer.readIdProperty(propName)) {
837 continue;
838 }
839
840 SettableBeanProperty prop = _beanProperties.find(propName);
841 if (prop != null) {
842 buffer.bufferProperty(prop, _deserializeWithErrorWrapping(p, ctxt, prop));
843 continue;
844 }
845
846 if (_ignorableProps != null && _ignorableProps.contains(propName)) {
847 handleIgnoredProperty(p, ctxt, handledType(), propName);
848 continue;
849 }
850
851
852
853
854 if (_anySetter == null) {
855
856 tokens.writeFieldName(propName);
857 tokens.copyCurrentStructure(p);
858 } else {
859
860 TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
861 tokens.writeFieldName(propName);
862 tokens.append(b2);
863 try {
864 buffer.bufferAnyProperty(_anySetter, propName,
865 _anySetter.deserialize(b2.asParserOnFirstToken(), ctxt));
866 } catch (Exception e) {
867 wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
868 }
869 continue;
870 }
871 }
872
873
874 Object bean;
875 try {
876 bean = creator.build(ctxt, buffer);
877 } catch (Exception e) {
878 wrapInstantiationProblem(e, ctxt);
879 return null;
880 }
881 return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
882 }
883
884
890
891 protected Object deserializeWithExternalTypeId(JsonParser p, DeserializationContext ctxt)
892 throws IOException
893 {
894 if (_propertyBasedCreator != null) {
895 return deserializeUsingPropertyBasedWithExternalTypeId(p, ctxt);
896 }
897 if (_delegateDeserializer != null) {
898
903 return _valueInstantiator.createUsingDelegate(ctxt,
904 _delegateDeserializer.deserialize(p, ctxt));
905 }
906
907 return deserializeWithExternalTypeId(p, ctxt, _valueInstantiator.createUsingDefault(ctxt));
908 }
909
910 protected Object deserializeWithExternalTypeId(JsonParser p, DeserializationContext ctxt,
911 Object bean)
912 throws IOException
913 {
914 final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
915 final ExternalTypeHandler ext = _externalTypeIdHandler.start();
916
917 for (JsonToken t = p.getCurrentToken(); t == JsonToken.FIELD_NAME; t = p.nextToken()) {
918 String propName = p.getCurrentName();
919 t = p.nextToken();
920 SettableBeanProperty prop = _beanProperties.find(propName);
921 if (prop != null) {
922
923 if (t.isScalarValue()) {
924 ext.handleTypePropertyValue(p, ctxt, propName, bean);
925 }
926 if (activeView != null && !prop.visibleInView(activeView)) {
927 p.skipChildren();
928 continue;
929 }
930 try {
931 prop.deserializeAndSet(p, ctxt, bean);
932 } catch (Exception e) {
933 wrapAndThrow(e, bean, propName, ctxt);
934 }
935 continue;
936 }
937
938 if (_ignorableProps != null && _ignorableProps.contains(propName)) {
939 handleIgnoredProperty(p, ctxt, bean, propName);
940 continue;
941 }
942
943 if (ext.handlePropertyValue(p, ctxt, propName, bean)) {
944 continue;
945 }
946
947 if (_anySetter != null) {
948 try {
949 _anySetter.deserializeAndSet(p, ctxt, bean, propName);
950 } catch (Exception e) {
951 wrapAndThrow(e, bean, propName, ctxt);
952 }
953 continue;
954 }
955
956 handleUnknownProperty(p, ctxt, bean, propName);
957 }
958
959 return ext.complete(p, ctxt, bean);
960 }
961
962 @SuppressWarnings("resource")
963 protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p, DeserializationContext ctxt)
964 throws IOException
965 {
966 final ExternalTypeHandler ext = _externalTypeIdHandler.start();
967 final PropertyBasedCreator creator = _propertyBasedCreator;
968 PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
969
970 TokenBuffer tokens = new TokenBuffer(p, ctxt);
971 tokens.writeStartObject();
972
973 JsonToken t = p.getCurrentToken();
974 for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
975 String propName = p.getCurrentName();
976 p.nextToken();
977
978 SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
979 if (creatorProp != null) {
980
981
982
983 if (ext.handlePropertyValue(p, ctxt, propName, null)) {
984 ;
985 } else {
986
987 if (buffer.assignParameter(creatorProp, _deserializeWithErrorWrapping(p, ctxt, creatorProp))) {
988 t = p.nextToken();
989 Object bean;
990 try {
991 bean = creator.build(ctxt, buffer);
992 } catch (Exception e) {
993 wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
994 continue;
995 }
996
997 while (t == JsonToken.FIELD_NAME) {
998 p.nextToken();
999 tokens.copyCurrentStructure(p);
1000 t = p.nextToken();
1001 }
1002 if (bean.getClass() != _beanType.getRawClass()) {
1003
1004
1005 return ctxt.reportBadDefinition(_beanType, String.format(
1006 "Cannot create polymorphic instances with external type ids (%s -> %s)",
1007 _beanType, bean.getClass()));
1008 }
1009 return ext.complete(p, ctxt, bean);
1010 }
1011 }
1012 continue;
1013 }
1014
1015 if (buffer.readIdProperty(propName)) {
1016 continue;
1017 }
1018
1019 SettableBeanProperty prop = _beanProperties.find(propName);
1020 if (prop != null) {
1021 buffer.bufferProperty(prop, prop.deserialize(p, ctxt));
1022 continue;
1023 }
1024
1025 if (ext.handlePropertyValue(p, ctxt, propName, null)) {
1026 continue;
1027 }
1028
1029 if (_ignorableProps != null && _ignorableProps.contains(propName)) {
1030 handleIgnoredProperty(p, ctxt, handledType(), propName);
1031 continue;
1032 }
1033
1034 if (_anySetter != null) {
1035 buffer.bufferAnyProperty(_anySetter, propName,
1036 _anySetter.deserialize(p, ctxt));
1037 continue;
1038 }
1039
1040 handleUnknownProperty(p, ctxt, _valueClass, propName);
1041 }
1042 tokens.writeEndObject();
1043
1044
1045 try {
1046 return ext.complete(p, ctxt, buffer, creator);
1047 } catch (Exception e) {
1048 return wrapInstantiationProblem(e, ctxt);
1049 }
1050 }
1051
1052
1058 protected Exception _creatorReturnedNullException() {
1059 if (_nullFromCreator == null) {
1060 _nullFromCreator = new NullPointerException("JSON Creator returned null");
1061 }
1062 return _nullFromCreator;
1063 }
1064
1065
1068 static class BeanReferring extends Referring
1069 {
1070 private final DeserializationContext _context;
1071 private final SettableBeanProperty _prop;
1072 private Object _bean;
1073
1074 BeanReferring(DeserializationContext ctxt, UnresolvedForwardReference ref,
1075 JavaType valueType, PropertyValueBuffer buffer, SettableBeanProperty prop)
1076 {
1077 super(ref, valueType);
1078 _context = ctxt;
1079 _prop = prop;
1080 }
1081
1082 public void setBean(Object bean) {
1083 _bean = bean;
1084 }
1085
1086 @Override
1087 public void handleResolvedForwardReference(Object id, Object value) throws IOException
1088 {
1089 if (_bean == null) {
1090 _context.reportInputMismatch(_prop,
1091 "Cannot resolve ObjectId forward reference using property '%s' (of type %s): Bean not yet resolved",
1092 _prop.getName(), _prop.getDeclaringClass().getName());
1093 }
1094 _prop.set(_bean, value);
1095 }
1096 }
1097 }
1098