1
1
package com .fasterxml .jackson .dataformat .csv ;
2
2
3
- import java .util .Collection ;
4
-
5
3
import com .fasterxml .jackson .core .type .TypeReference ;
6
-
7
- import com .fasterxml .jackson .databind .*;
4
+ import com .fasterxml .jackson .databind .AnnotationIntrospector ;
5
+ import com .fasterxml .jackson .databind .BeanDescription ;
6
+ import com .fasterxml .jackson .databind .JavaType ;
7
+ import com .fasterxml .jackson .databind .ObjectMapper ;
8
+ import com .fasterxml .jackson .databind .ObjectReader ;
9
+ import com .fasterxml .jackson .databind .ObjectWriter ;
10
+ import com .fasterxml .jackson .databind .SerializerProvider ;
8
11
import com .fasterxml .jackson .databind .cfg .MapperBuilder ;
9
12
import com .fasterxml .jackson .databind .cfg .MapperBuilderState ;
10
13
import com .fasterxml .jackson .databind .introspect .AnnotatedMember ;
11
14
import com .fasterxml .jackson .databind .introspect .BeanPropertyDefinition ;
12
15
import com .fasterxml .jackson .databind .util .NameTransformer ;
13
16
import com .fasterxml .jackson .databind .util .SimpleLookupCache ;
17
+ import com .fasterxml .jackson .databind .util .ViewMatcher ;
18
+ import java .util .Collection ;
14
19
15
20
/**
16
21
* Specialized {@link ObjectMapper}, with extended functionality to
@@ -364,15 +369,27 @@ public CsvSchema schema() {
364
369
* just defined to be exposed as String tokens).
365
370
*/
366
371
public CsvSchema schemaFor (JavaType pojoType ) {
367
- return _schemaFor (pojoType , _untypedSchemas , false );
372
+ return _schemaFor (pojoType , _untypedSchemas , false , null );
373
+ }
374
+
375
+ public CsvSchema schemaForWithView (JavaType pojoType , Class <?> view ) {
376
+ return _schemaFor (pojoType , _untypedSchemas , false , view );
368
377
}
369
378
370
379
public final CsvSchema schemaFor (Class <?> pojoType ) {
371
- return _schemaFor (constructType (pojoType ), _untypedSchemas , false );
380
+ return _schemaFor (constructType (pojoType ), _untypedSchemas , false , null );
381
+ }
382
+
383
+ public final CsvSchema schemaForWithView (Class <?> pojoType , Class <?> view ) {
384
+ return _schemaFor (constructType (pojoType ), _untypedSchemas , false , view );
372
385
}
373
386
374
387
public final CsvSchema schemaFor (TypeReference <?> pojoTypeRef ) {
375
- return _schemaFor (constructType (pojoTypeRef .getType ()), _untypedSchemas , false );
388
+ return _schemaFor (constructType (pojoTypeRef .getType ()), _untypedSchemas , false , null );
389
+ }
390
+
391
+ public final CsvSchema schemaForWithView (TypeReference <?> pojoTypeRef , Class <?> view ) {
392
+ return _schemaFor (constructType (pojoTypeRef .getType ()), _untypedSchemas , false , view );
376
393
}
377
394
378
395
/**
@@ -383,15 +400,27 @@ public final CsvSchema schemaFor(TypeReference<?> pojoTypeRef) {
383
400
* (especially for numeric types like java.lang.Integer).
384
401
*/
385
402
public CsvSchema typedSchemaFor (JavaType pojoType ) {
386
- return _schemaFor (pojoType , _typedSchemas , true );
403
+ return _schemaFor (pojoType , _typedSchemas , true , null );
404
+ }
405
+
406
+ public CsvSchema typedSchemaForWithView (JavaType pojoType , Class <?> view ) {
407
+ return _schemaFor (pojoType , _typedSchemas , true , view );
387
408
}
388
409
389
410
public final CsvSchema typedSchemaFor (Class <?> pojoType ) {
390
- return _schemaFor (constructType (pojoType ), _typedSchemas , true );
411
+ return _schemaFor (constructType (pojoType ), _typedSchemas , true , null );
412
+ }
413
+
414
+ public final CsvSchema typedSchemaForWithView (Class <?> pojoType , Class <?> view ) {
415
+ return _schemaFor (constructType (pojoType ), _typedSchemas , true , view );
391
416
}
392
417
393
418
public final CsvSchema typedSchemaFor (TypeReference <?> pojoTypeRef ) {
394
- return _schemaFor (constructType (pojoTypeRef .getType ()), _typedSchemas , true );
419
+ return _schemaFor (constructType (pojoTypeRef .getType ()), _typedSchemas , true , null );
420
+ }
421
+
422
+ public final CsvSchema typedSchemaForWithView (TypeReference <?> pojoTypeRef , Class <?> view ) {
423
+ return _schemaFor (constructType (pojoTypeRef .getType ()), _typedSchemas , true , view );
395
424
}
396
425
397
426
/*
@@ -401,7 +430,7 @@ public final CsvSchema typedSchemaFor(TypeReference<?> pojoTypeRef) {
401
430
*/
402
431
403
432
protected CsvSchema _schemaFor (JavaType pojoType , SimpleLookupCache <JavaType ,CsvSchema > schemas ,
404
- boolean typed )
433
+ boolean typed , Class <?> view )
405
434
{
406
435
synchronized (schemas ) {
407
436
CsvSchema s = schemas .get (pojoType );
@@ -412,7 +441,7 @@ protected CsvSchema _schemaFor(JavaType pojoType, SimpleLookupCache<JavaType,Csv
412
441
// 15-Oct-2019, tatu: Since 3.0, need context for introspection
413
442
final SerializerProvider ctxt = _serializerProvider ();
414
443
CsvSchema .Builder builder = CsvSchema .builder ();
415
- _addSchemaProperties (ctxt , builder , typed , pojoType , null );
444
+ _addSchemaProperties (ctxt , builder , typed , pojoType , null , view );
416
445
CsvSchema result = builder .build ();
417
446
synchronized (schemas ) {
418
447
schemas .put (pojoType , result );
@@ -449,8 +478,7 @@ protected boolean _nonPojoType(JavaType t)
449
478
}
450
479
451
480
protected void _addSchemaProperties (SerializerProvider ctxt , CsvSchema .Builder builder ,
452
- boolean typed ,
453
- JavaType pojoType , NameTransformer unwrapper )
481
+ boolean typed , JavaType pojoType , NameTransformer unwrapper , Class <?> view )
454
482
{
455
483
// 09-Aug-2015, tatu: From [dataformat-csv#87], realized that one can not have
456
484
// real schemas for primitive/wrapper
@@ -460,6 +488,15 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b
460
488
BeanDescription beanDesc = ctxt .introspectBeanDescription (pojoType );
461
489
final AnnotationIntrospector intr = ctxt .getAnnotationIntrospector ();
462
490
for (BeanPropertyDefinition prop : beanDesc .findProperties ()) {
491
+ if (view != null ) {
492
+ Class <?>[] views = prop .findViews ();
493
+ if (views == null ) {
494
+ views = beanDesc .findDefaultViews ();
495
+ }
496
+ if (!ViewMatcher .construct (views ).isVisibleForView (view )) {
497
+ continue ;
498
+ }
499
+ }
463
500
// ignore setter-only properties:
464
501
if (!prop .couldSerialize ()) {
465
502
continue ;
@@ -474,7 +511,7 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b
474
511
nextUnwrapper = NameTransformer .chainedTransformer (unwrapper , nextUnwrapper );
475
512
}
476
513
JavaType nextType = m .getType ();
477
- _addSchemaProperties (ctxt , builder , typed , nextType , nextUnwrapper );
514
+ _addSchemaProperties (ctxt , builder , typed , nextType , nextUnwrapper , view );
478
515
continue ;
479
516
}
480
517
}
0 commit comments