Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 3c604e2

Browse files
committed
Implemented #15, support for @JsonUnwrapper, with auto-generated schemas
1 parent fc6f246 commit 3c604e2

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

release-notes/VERSION

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
Version: 2.1.4 (26-Feb-2013)
1+
Project: jackson-dataformat-csv
2+
Version: 2.2.0 (xx-xxx-2013)
23

3-
No functional changes
4+
New minor version.
5+
6+
Changes:
7+
8+
#15: Ensure that `@JsonUnwrapper` works, even with auto-schema
49

510
------------------------------------------------------------------------
611
=== History: ===
712
------------------------------------------------------------------------
813

14+
2.1.4 (26-Feb-2013)
15+
16+
No functional changes
17+
918
2.1.2 (04-Dec-2012)
1019

1120
* [Issue#10]: Embedded linefeeds not properly handled

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

+39-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.fasterxml.jackson.dataformat.csv;
22

33
import com.fasterxml.jackson.databind.*;
4+
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
45
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
6+
import com.fasterxml.jackson.databind.util.NameTransformer;
57
import com.fasterxml.jackson.core.type.TypeReference;
68

79
import com.fasterxml.jackson.dataformat.csv.impl.LRUMap;
@@ -190,34 +192,59 @@ public ObjectReader readerWithTypedSchemaFor(Class<?> pojoType)
190192
/**********************************************************************
191193
*/
192194

193-
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas, boolean typed)
195+
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas,
196+
boolean typed)
194197
{
195198
synchronized (schemas) {
196199
CsvSchema s = schemas.get(pojoType);
197200
if (s != null) {
198201
return s;
199202
}
200203
}
201-
BeanDescription beanDesc = getSerializationConfig().introspect(pojoType);
204+
final AnnotationIntrospector intr = _deserializationConfig.getAnnotationIntrospector();
202205
CsvSchema.Builder builder = CsvSchema.builder();
206+
_addSchemaProperties(builder, intr, typed, pojoType, null);
207+
CsvSchema result = builder.build();
208+
synchronized (schemas) {
209+
schemas.put(pojoType, result);
210+
}
211+
return result;
212+
}
213+
214+
protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrospector intr,
215+
boolean typed,
216+
JavaType pojoType, NameTransformer unwrapper)
217+
{
218+
BeanDescription beanDesc = getSerializationConfig().introspect(pojoType);
203219
for (BeanPropertyDefinition prop : beanDesc.findProperties()) {
204220
// ignore setter-only properties:
205221
if (!prop.couldSerialize()) {
206222
continue;
207223
}
208-
// TODO: [Issue#15]: need to handle unwrapped props?
209-
210-
if (typed) {
211-
builder.addColumn(prop.getName(), _determineType(prop.getAccessor().getRawType()));
224+
// [Issue#15]: handle unwrapped props
225+
AnnotatedMember m = prop.getPrimaryMember();
226+
if (m != null) {
227+
NameTransformer nextUnwrapper = intr.findUnwrappingNameTransformer(prop.getPrimaryMember());
228+
if (nextUnwrapper != null) {
229+
if (unwrapper != null) {
230+
nextUnwrapper = NameTransformer.chainedTransformer(unwrapper, nextUnwrapper);
231+
}
232+
JavaType nextType = m.getType(beanDesc.bindingsForBeanType());
233+
_addSchemaProperties(builder, intr, typed, nextType, nextUnwrapper);
234+
continue;
235+
}
236+
}
237+
// Then name wrapping/unwrapping
238+
String name = prop.getName();
239+
if (unwrapper != null) {
240+
name = unwrapper.transform(name);
241+
}
242+
if (typed && m != null) {
243+
builder.addColumn(name, _determineType(m.getRawType()));
212244
} else {
213-
builder.addColumn(prop.getName());
245+
builder.addColumn(name);
214246
}
215247
}
216-
CsvSchema result = builder.build();
217-
synchronized (schemas) {
218-
schemas.put(pojoType, result);
219-
}
220-
return result;
221248
}
222249

223250
// should not be null since couldSerialize() returned true, so:

src/test/java/com/fasterxml/jackson/dataformat/csv/TestUnwrappingWithCSV.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Location(int x, int y) {
2222

2323
static class Unwrapping {
2424
public String name;
25-
@JsonUnwrapped
25+
@JsonUnwrapped(prefix="loc.")
2626
public Location location;
2727

2828
public Unwrapping() { }
@@ -48,8 +48,8 @@ public void testSimpleUnwrappingRoundtrip() throws Exception
4848
ObjectMapper mapper = mapperForCsv();
4949
CsvSchema schema = CsvSchema.builder()
5050
.addColumn("name")
51-
.addColumn("x")
52-
.addColumn("y")
51+
.addColumn("loc.x")
52+
.addColumn("loc.y")
5353
.build();
5454
Unwrapping wrapper = mapper.reader(schema).withType(Unwrapping.class).readValue(CSV);
5555
assertNotNull(wrapper);
@@ -71,22 +71,19 @@ public void testSimpleUnwrappingRoundtrip() throws Exception
7171
* available via BeanProperty/POJOPropertyBuilder. But it needs to be
7272
* made; and when this occurs, we can handle this case reasonably well.
7373
*/
74-
/*
7574
public void testSimpleWithAutoSchema() throws Exception
7675
{
77-
final String CSV = "Henry,1,2\n";
76+
final String CSV = "Henry,28,12\n";
7877
CsvMapper mapper = mapperForCsv();
7978
CsvSchema schema = mapper.schemaFor(Unwrapping.class);
80-
System.err.println("SChema/auto -> "+schema);
8179

8280
Unwrapping wrapper = mapper.reader(schema).withType(Unwrapping.class).readValue(CSV);
8381
assertNotNull(wrapper);
8482
assertNotNull(wrapper.location);
85-
assertEquals(15, wrapper.location.x);
86-
assertEquals(27, wrapper.location.y);
83+
assertEquals(28, wrapper.location.x);
84+
assertEquals(12, wrapper.location.y);
8785

8886
// should also write out the same way
8987
assertEquals(CSV, mapper.writer(schema).writeValueAsString(wrapper));
9088
}
91-
*/
9289
}

0 commit comments

Comments
 (0)