Skip to content

Commit 8d6fa48

Browse files
committed
Fixed #359 for 2.4
1 parent 81017b9 commit 8d6fa48

File tree

5 files changed

+86
-83
lines changed

5 files changed

+86
-83
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Version: 2.4.0 (xx-xxx-2014)
1010
#335: Allow use of `@JsonProperytOrder(alphabetic=true)` for Map properties
1111
#353: Problems with polymorphic types, `JsonNode` (related to #88)
1212
(reported by cemo@github)
13+
#359: Converted object not using explicitly annotated serializer
14+
(reported by fschopp@github)
1315
#369: Incorrect comparison for renaming in `POJOPropertyBuilder`
1416
#375: Add `readValue()`/`readPropertyValue()` methods in `DeserializationContext`
1517
#381: Allow inlining/unwrapping of value from single-component JSON array

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,21 @@ public JsonSerializer<Object> createSerializer(SerializerProvider prov,
148148
}
149149
// Slight detour: do we have a Converter to consider?
150150
Converter<Object,Object> conv = beanDesc.findSerializationConverter();
151-
if (conv == null) { // no, simple:
151+
if (conv == null) { // no, simple
152152
return (JsonSerializer<Object>) _createSerializer2(prov, type, beanDesc, staticTyping);
153153
}
154154
JavaType delegateType = conv.getOutputType(prov.getTypeFactory());
155+
155156
// One more twist, as per [Issue#288]; probably need to get new BeanDesc
156157
if (!delegateType.hasRawClass(type.getRawClass())) {
157158
beanDesc = config.introspect(delegateType);
159+
// [#359]: explicitly check (again) for @JsonSerializer...
160+
ser = findSerializerFromAnnotation(prov, beanDesc.getClassInfo());
161+
}
162+
if (ser == null) {
163+
ser = _createSerializer2(prov, delegateType, beanDesc, true);
158164
}
159-
return new StdDelegatingSerializer(conv, delegateType,
160-
_createSerializer2(prov, delegateType, beanDesc, true));
165+
return new StdDelegatingSerializer(conv, delegateType, ser);
161166
}
162167

163168
protected JsonSerializer<?> _createSerializer2(SerializerProvider prov,

src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.fasterxml.jackson.databind.convert;
22

3+
import java.io.IOException;
34
import java.util.*;
45

6+
import com.fasterxml.jackson.core.JsonGenerator;
7+
import com.fasterxml.jackson.databind.JsonSerializer;
8+
import com.fasterxml.jackson.databind.SerializerProvider;
59
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
610
import com.fasterxml.jackson.databind.util.StdConverter;
711

@@ -90,6 +94,35 @@ public PointListWrapperMap(String key, int x, int y) {
9094
}
9195
}
9296

97+
// [Issue#359]
98+
static class Bean359 {
99+
@JsonSerialize(as = List.class, contentAs = Source.class)
100+
public List<Source> stuff = Arrays.asList(new Source());
101+
}
102+
103+
@JsonSerialize(using = TargetSerializer.class)
104+
static class Target {
105+
public String unexpected = "Bye.";
106+
}
107+
108+
@JsonSerialize(converter = SourceToTargetConverter.class)
109+
static class Source { }
110+
111+
static class SourceToTargetConverter extends StdConverter<Source, Target> {
112+
@Override
113+
public Target convert(Source value) {
114+
return new Target();
115+
}
116+
}
117+
118+
static class TargetSerializer extends JsonSerializer<Target>
119+
{
120+
@Override
121+
public void serialize(Target a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
122+
jsonGenerator.writeString("Target");
123+
}
124+
}
125+
93126
/*
94127
/**********************************************************
95128
/* Test methods
@@ -129,4 +162,10 @@ public void testPropertyAnnotationForMaps() throws Exception {
129162
String json = objectWriter().writeValueAsString(new PointListWrapperMap("a", 1, 2));
130163
assertEquals("{\"values\":{\"a\":[1,2]}}", json);
131164
}
165+
166+
// [Issue#359]
167+
public void testIssue359() throws Exception {
168+
String json = objectWriter().writeValueAsString(new Bean359());
169+
assertEquals("{\"stuff\":[\"Target\"]}", json);
170+
}
132171
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
6+
import com.fasterxml.jackson.databind.util.StdConverter;
7+
8+
public class TestConvertingSerializer357
9+
extends com.fasterxml.jackson.databind.BaseMapTest
10+
{
11+
// [Issue#357]
12+
static class A { }
13+
14+
static class B {
15+
@JsonSerialize(contentConverter = AToStringConverter.class)
16+
public List<A> list = Arrays.asList(new A());
17+
}
18+
19+
static class AToStringConverter extends StdConverter<A, List<String>> {
20+
@Override
21+
public List<String> convert(A value) {
22+
return Arrays.asList("Hello world!");
23+
}
24+
}
25+
26+
/*
27+
/**********************************************************
28+
/* Test methods
29+
/**********************************************************
30+
*/
31+
32+
// [Issue#357]
33+
public void testConverterForList357() throws Exception {
34+
String json = objectWriter().writeValueAsString(new B());
35+
assertEquals("{\"list\":[[\"Hello world!\"]]}", json);
36+
}
37+
}

src/test/java/com/fasterxml/jackson/failing/TestConvertingSerializer357And359.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)