Skip to content

Commit a9d7e31

Browse files
Qnzvnacowtowncoder
authored andcommitted
Adds schema creating csv schema with View
1 parent a6eb9ab commit a9d7e31

File tree

2 files changed

+95
-20
lines changed

2 files changed

+95
-20
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java

+52-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.fasterxml.jackson.dataformat.csv;
22

3-
import java.util.Collection;
4-
53
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;
811
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
912
import com.fasterxml.jackson.databind.cfg.MapperBuilderState;
1013
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
1114
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
1215
import com.fasterxml.jackson.databind.util.NameTransformer;
1316
import com.fasterxml.jackson.databind.util.SimpleLookupCache;
17+
import com.fasterxml.jackson.databind.util.ViewMatcher;
18+
import java.util.Collection;
1419

1520
/**
1621
* Specialized {@link ObjectMapper}, with extended functionality to
@@ -364,15 +369,27 @@ public CsvSchema schema() {
364369
* just defined to be exposed as String tokens).
365370
*/
366371
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);
368377
}
369378

370379
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);
372385
}
373386

374387
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);
376393
}
377394

378395
/**
@@ -383,15 +400,27 @@ public final CsvSchema schemaFor(TypeReference<?> pojoTypeRef) {
383400
* (especially for numeric types like java.lang.Integer).
384401
*/
385402
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);
387408
}
388409

389410
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);
391416
}
392417

393418
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);
395424
}
396425

397426
/*
@@ -401,7 +430,7 @@ public final CsvSchema typedSchemaFor(TypeReference<?> pojoTypeRef) {
401430
*/
402431

403432
protected CsvSchema _schemaFor(JavaType pojoType, SimpleLookupCache<JavaType,CsvSchema> schemas,
404-
boolean typed)
433+
boolean typed, Class<?> view)
405434
{
406435
synchronized (schemas) {
407436
CsvSchema s = schemas.get(pojoType);
@@ -412,7 +441,7 @@ protected CsvSchema _schemaFor(JavaType pojoType, SimpleLookupCache<JavaType,Csv
412441
// 15-Oct-2019, tatu: Since 3.0, need context for introspection
413442
final SerializerProvider ctxt = _serializerProvider();
414443
CsvSchema.Builder builder = CsvSchema.builder();
415-
_addSchemaProperties(ctxt, builder, typed, pojoType, null);
444+
_addSchemaProperties(ctxt, builder, typed, pojoType, null, view);
416445
CsvSchema result = builder.build();
417446
synchronized (schemas) {
418447
schemas.put(pojoType, result);
@@ -449,8 +478,7 @@ protected boolean _nonPojoType(JavaType t)
449478
}
450479

451480
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)
454482
{
455483
// 09-Aug-2015, tatu: From [dataformat-csv#87], realized that one can not have
456484
// real schemas for primitive/wrapper
@@ -460,6 +488,15 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b
460488
BeanDescription beanDesc = ctxt.introspectBeanDescription(pojoType);
461489
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
462490
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+
}
463500
// ignore setter-only properties:
464501
if (!prop.couldSerialize()) {
465502
continue;
@@ -474,7 +511,7 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b
474511
nextUnwrapper = NameTransformer.chainedTransformer(unwrapper, nextUnwrapper);
475512
}
476513
JavaType nextType = m.getType();
477-
_addSchemaProperties(ctxt, builder, typed, nextType, nextUnwrapper);
514+
_addSchemaProperties(ctxt, builder, typed, nextType, nextUnwrapper, view);
478515
continue;
479516
}
480517
}

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.fasterxml.jackson.dataformat.csv.deser;
22

3-
import java.io.BufferedReader;
4-
import java.io.StringReader;
5-
import java.util.*;
6-
7-
import com.fasterxml.jackson.annotation.*;
3+
import com.fasterxml.jackson.annotation.JsonFilter;
4+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
5+
import com.fasterxml.jackson.annotation.JsonView;
86
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.FilterExceptFilter;
97
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
108
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
9+
import com.fasterxml.jackson.dataformat.csv.CsvMappingException;
1110
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
1211
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
12+
import java.io.BufferedReader;
13+
import java.io.StringReader;
14+
import java.util.Arrays;
15+
import java.util.List;
1316

1417
public class TestFiltering extends ModuleTestBase
1518
{
@@ -76,6 +79,41 @@ public void testWithJsonView() throws Exception
7679
assertEquals("2", result.aa);
7780
assertEquals("7", result.b);
7881
}
82+
83+
public void testSchemaWithJsonViewSerialization() throws Exception
84+
{
85+
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
86+
String actual = MAPPER.writer(schema).withView(ViewB.class).writeValueAsString(new Bean());
87+
MAPPER.writer(schema).withView(ViewB.class);
88+
89+
BufferedReader br = new BufferedReader(new StringReader(actual.trim()));
90+
assertEquals("a,b", br.readLine());
91+
assertEquals("1,3", br.readLine());
92+
assertNull(br.readLine());
93+
}
94+
95+
public void testSchemaWithJsonViewDeserialization() throws Exception
96+
{
97+
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
98+
final String input = "a,b\n5,7\n";
99+
Bean result = MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input);
100+
101+
assertEquals("5", result.a);
102+
// due to filtering, ought to use default
103+
assertEquals("2", result.aa);
104+
assertEquals("7", result.b);
105+
}
106+
107+
public void testSchemaWithJsonViewDeserializationFail() throws Exception
108+
{
109+
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
110+
final String input = "a,aa,b\n5,6,7\n";
111+
try {
112+
MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input);
113+
fail();
114+
} catch (CsvMappingException ignore) {
115+
}
116+
}
79117

80118
public void testWithJsonFilter() throws Exception
81119
{

0 commit comments

Comments
 (0)