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