66import java .lang .reflect .Field ;
77import java .util .HashMap ;
88
9+ import com .fasterxml .jackson .annotation .JsonApplyView ;
910import com .fasterxml .jackson .annotation .JsonInclude ;
1011
1112import tools .jackson .core .JacksonException ;
@@ -177,6 +178,13 @@ public class BeanPropertyWriter
177178 */
178179 protected final Class <?>[] _includeInViews ;
179180
181+ /**
182+ * View to apply for this property when applyView is available for the Bean.
183+ *
184+ * @since 3.2
185+ */
186+ protected final Class <?> _applyView ;
187+
180188 /**
181189 * Inclusion settings for this property, pre-computed in {@code PropertyBuilder}
182190 * by merging global defaults, type defaults, and property-level annotations,
@@ -214,21 +222,39 @@ public BeanPropertyWriter(BeanPropertyDefinition propDef,
214222 {
215223 this (propDef , member , contextAnnotations , declaredType ,
216224 ser , typeSer , serType , suppressNulls , suppressableValue ,
217- includeInViews , null );
225+ includeInViews , null , null );
226+ }
227+
228+ /**
229+ * @deprecated Since 3.2 use {@link #BeanPropertyWriter(BeanPropertyDefinition,
230+ * AnnotatedMember, Annotations, JavaType, ValueSerializer, TypeSerializer,
231+ * JavaType, boolean, Object, Class[], JsonInclude.Value, Class)} instead.
232+ */
233+ @ Deprecated // @since 3.2
234+ public BeanPropertyWriter (BeanPropertyDefinition propDef ,
235+ AnnotatedMember member , Annotations contextAnnotations ,
236+ JavaType declaredType ,
237+ ValueSerializer <?> ser , TypeSerializer typeSer , JavaType serType ,
238+ boolean suppressNulls , Object suppressableValue ,
239+ Class <?>[] includeInViews , JsonInclude .Value inclusion )
240+ {
241+ this (propDef , member , contextAnnotations , declaredType ,
242+ ser , typeSer , serType , suppressNulls , suppressableValue ,
243+ includeInViews , inclusion , null );
218244 }
219245
220246 /**
221- * Constructor with additional inclusion parameter.
247+ * Constructor with additional inclusion and applyView parameter.
222248 *
223- * @since 3.1
249+ * @since 3.2
224250 */
225251 @ SuppressWarnings ("unchecked" )
226252 public BeanPropertyWriter (BeanPropertyDefinition propDef ,
227253 AnnotatedMember member , Annotations contextAnnotations ,
228254 JavaType declaredType ,
229255 ValueSerializer <?> ser , TypeSerializer typeSer , JavaType serType ,
230256 boolean suppressNulls , Object suppressableValue ,
231- Class <?>[] includeInViews , JsonInclude .Value inclusion )
257+ Class <?>[] includeInViews , JsonInclude .Value inclusion , Class <?> applyView )
232258 {
233259 super (propDef );
234260 _member = member ;
@@ -260,6 +286,7 @@ public BeanPropertyWriter(BeanPropertyDefinition propDef,
260286 // this will be resolved later on, unless nulls are to be suppressed
261287 _nullSerializer = null ;
262288 _includeInViews = includeInViews ;
289+ _applyView = applyView ;
263290 _inclusion = (inclusion == null ) ? JsonInclude .Value .empty () : inclusion ;
264291 }
265292
@@ -276,6 +303,7 @@ protected BeanPropertyWriter() {
276303 _name = null ;
277304 _wrapperName = null ;
278305 _includeInViews = null ;
306+ _applyView = null ;
279307
280308 _declaredType = null ;
281309 _serializer = null ;
@@ -323,6 +351,7 @@ protected BeanPropertyWriter(BeanPropertyWriter base, PropertyName name) {
323351 _suppressNulls = base ._suppressNulls ;
324352 _suppressableValue = base ._suppressableValue ;
325353 _includeInViews = base ._includeInViews ;
354+ _applyView = base ._applyView ;
326355 _typeSerializer = base ._typeSerializer ;
327356 _nonTrivialBaseType = base ._nonTrivialBaseType ;
328357 _inclusion = base ._inclusion ;
@@ -347,6 +376,7 @@ protected BeanPropertyWriter(BeanPropertyWriter base, SerializedString name) {
347376 _suppressNulls = base ._suppressNulls ;
348377 _suppressableValue = base ._suppressableValue ;
349378 _includeInViews = base ._includeInViews ;
379+ _applyView = base ._applyView ;
350380 _typeSerializer = base ._typeSerializer ;
351381 _nonTrivialBaseType = base ._nonTrivialBaseType ;
352382 _inclusion = base ._inclusion ;
@@ -660,10 +690,12 @@ public void serializeAsProperty(Object bean, JsonGenerator g, SerializationConte
660690 }
661691 }
662692 g .writeName (_name );
663- if (_typeSerializer == null ) {
664- ser . serialize (value , g , ctxt );
693+ if (_applyView == null ) {
694+ _serialize (value , g , ctxt , ser );
665695 } else {
666- ser .serializeWithType (value , g , ctxt , _typeSerializer );
696+ ValueSerializer <Object > actualSer = ser ;
697+ ctxt .withActiveView (_applyView != JsonApplyView .NONE .class ? _applyView : null ,
698+ () -> _serialize (value , g , ctxt , actualSer ));
667699 }
668700 }
669701
@@ -728,10 +760,13 @@ public void serializeAsElement(Object bean, JsonGenerator g, SerializationContex
728760 return ;
729761 }
730762 }
731- if (_typeSerializer == null ) {
732- ser .serialize (value , g , ctxt );
763+
764+ if (_applyView == null ) {
765+ _serialize (value , g , ctxt , ser );
733766 } else {
734- ser .serializeWithType (value , g , ctxt , _typeSerializer );
767+ ValueSerializer <Object > actualSer = ser ;
768+ ctxt .withActiveView (_applyView != JsonApplyView .NONE .class ? _applyView : null ,
769+ () -> _serialize (value , g , ctxt , actualSer ));
735770 }
736771 }
737772
@@ -753,6 +788,16 @@ public void serializeAsOmittedElement(Object bean, JsonGenerator g,
753788 }
754789 }
755790
791+ // @since 3.2
792+ private void _serialize (Object value , JsonGenerator g , SerializationContext ctxt ,
793+ ValueSerializer <Object > ser ) {
794+ if (_typeSerializer == null ) {
795+ ser .serialize (value , g , ctxt );
796+ } else {
797+ ser .serializeWithType (value , g , ctxt , _typeSerializer );
798+ }
799+ }
800+
756801 /*
757802 /**********************************************************************
758803 /* PropertyWriter methods (schema generation)
0 commit comments