diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java index fda9a913..7e89c05f 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java @@ -1,16 +1,21 @@ package com.fasterxml.jackson.dataformat.csv; -import java.util.Collection; - import com.fasterxml.jackson.core.type.TypeReference; - -import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.AnnotationIntrospector; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.cfg.MapperBuilder; import com.fasterxml.jackson.databind.cfg.MapperBuilderState; import com.fasterxml.jackson.databind.introspect.AnnotatedMember; import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; import com.fasterxml.jackson.databind.util.NameTransformer; import com.fasterxml.jackson.databind.util.SimpleLookupCache; +import com.fasterxml.jackson.databind.util.ViewMatcher; +import java.util.Collection; /** * Specialized {@link ObjectMapper}, with extended functionality to @@ -364,15 +369,27 @@ public CsvSchema schema() { * just defined to be exposed as String tokens). */ public CsvSchema schemaFor(JavaType pojoType) { - return _schemaFor(pojoType, _untypedSchemas, false); + return _schemaFor(pojoType, _untypedSchemas, false, null); + } + + public CsvSchema schemaForWithView(JavaType pojoType, Class view) { + return _schemaFor(pojoType, _untypedSchemas, false, view); } public final CsvSchema schemaFor(Class pojoType) { - return _schemaFor(constructType(pojoType), _untypedSchemas, false); + return _schemaFor(constructType(pojoType), _untypedSchemas, false, null); + } + + public final CsvSchema schemaForWithView(Class pojoType, Class view) { + return _schemaFor(constructType(pojoType), _untypedSchemas, false, view); } public final CsvSchema schemaFor(TypeReference pojoTypeRef) { - return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false); + return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false, null); + } + + public final CsvSchema schemaForWithView(TypeReference pojoTypeRef, Class view) { + return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false, view); } /** @@ -383,15 +400,27 @@ public final CsvSchema schemaFor(TypeReference pojoTypeRef) { * (especially for numeric types like java.lang.Integer). */ public CsvSchema typedSchemaFor(JavaType pojoType) { - return _schemaFor(pojoType, _typedSchemas, true); + return _schemaFor(pojoType, _typedSchemas, true, null); + } + + public CsvSchema typedSchemaForWithView(JavaType pojoType, Class view) { + return _schemaFor(pojoType, _typedSchemas, true, view); } public final CsvSchema typedSchemaFor(Class pojoType) { - return _schemaFor(constructType(pojoType), _typedSchemas, true); + return _schemaFor(constructType(pojoType), _typedSchemas, true, null); + } + + public final CsvSchema typedSchemaForWithView(Class pojoType, Class view) { + return _schemaFor(constructType(pojoType), _typedSchemas, true, view); } public final CsvSchema typedSchemaFor(TypeReference pojoTypeRef) { - return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true); + return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true, null); + } + + public final CsvSchema typedSchemaForWithView(TypeReference pojoTypeRef, Class view) { + return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true, view); } /* @@ -401,7 +430,7 @@ public final CsvSchema typedSchemaFor(TypeReference pojoTypeRef) { */ protected CsvSchema _schemaFor(JavaType pojoType, SimpleLookupCache schemas, - boolean typed) + boolean typed, Class view) { synchronized (schemas) { CsvSchema s = schemas.get(pojoType); @@ -412,7 +441,7 @@ protected CsvSchema _schemaFor(JavaType pojoType, SimpleLookupCache view) { // 09-Aug-2015, tatu: From [dataformat-csv#87], realized that one can not have // real schemas for primitive/wrapper @@ -460,6 +488,15 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b BeanDescription beanDesc = ctxt.introspectBeanDescription(pojoType); final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector(); for (BeanPropertyDefinition prop : beanDesc.findProperties()) { + if (view != null) { + Class[] views = prop.findViews(); + if (views == null) { + views = beanDesc.findDefaultViews(); + } + if (!ViewMatcher.construct(views).isVisibleForView(view)) { + continue; + } + } // ignore setter-only properties: if (!prop.couldSerialize()) { continue; @@ -474,7 +511,7 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b nextUnwrapper = NameTransformer.chainedTransformer(unwrapper, nextUnwrapper); } JavaType nextType = m.getType(); - _addSchemaProperties(ctxt, builder, typed, nextType, nextUnwrapper); + _addSchemaProperties(ctxt, builder, typed, nextType, nextUnwrapper, view); continue; } } diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java index 0f3458b8..c624efe5 100644 --- a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java @@ -1,15 +1,18 @@ package com.fasterxml.jackson.dataformat.csv.deser; -import java.io.BufferedReader; -import java.io.StringReader; -import java.util.*; - -import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonFilter; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.FilterExceptFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvMappingException; import com.fasterxml.jackson.dataformat.csv.CsvSchema; import com.fasterxml.jackson.dataformat.csv.ModuleTestBase; +import java.io.BufferedReader; +import java.io.StringReader; +import java.util.Arrays; +import java.util.List; public class TestFiltering extends ModuleTestBase { @@ -76,6 +79,41 @@ public void testWithJsonView() throws Exception assertEquals("2", result.aa); assertEquals("7", result.b); } + + public void testSchemaWithJsonViewSerialization() throws Exception + { + CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader(); + String actual = MAPPER.writer(schema).withView(ViewB.class).writeValueAsString(new Bean()); + MAPPER.writer(schema).withView(ViewB.class); + + BufferedReader br = new BufferedReader(new StringReader(actual.trim())); + assertEquals("a,b", br.readLine()); + assertEquals("1,3", br.readLine()); + assertNull(br.readLine()); + } + + public void testSchemaWithJsonViewDeserialization() throws Exception + { + CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader(); + final String input = "a,b\n5,7\n"; + Bean result = MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input); + + assertEquals("5", result.a); + // due to filtering, ought to use default + assertEquals("2", result.aa); + assertEquals("7", result.b); + } + + public void testSchemaWithJsonViewDeserializationFail() throws Exception + { + CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader(); + final String input = "a,aa,b\n5,6,7\n"; + try { + MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input); + fail(); + } catch (CsvMappingException ignore) { + } + } public void testWithJsonFilter() throws Exception {