Skip to content

Commit ce457af

Browse files
committed
Fixed #358
1 parent 96dd040 commit ce457af

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
7474
<dependency>
7575
<groupId>com.fasterxml.jackson.core</groupId>
7676
<artifactId>jackson-core</artifactId>
77-
<version>2.3.0</version>
77+
<version>2.3.1-SNAPSHOT</version>
7878
</dependency>
7979

8080
<!-- and for testing, JUnit is needed, as well as quite a few

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ David Phillips:
9494
Seth Pellegrino (jivesoft):
9595
* Contributed #317: Fix `JsonNode` support for nulls bound to `ObjectNode`, `ArrayNode`
9696
(2.3.0)
97+
98+
Florian Schoppmann (fschopp@github)
99+
* Reported #358: `IterableSerializer` ignoring annotated content serializer
100+
(2.3.1)

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Version: 2.3.1 (xx-Dec-2013)
44
#346: Fix problem deserializing `ObjectNode`, with @JsonCreator, empty
55
JSON Object
66
(reported by gaff78@github)
7+
#358: `IterableSerializer` ignoring annotated content serializer
8+
(reported by Florian S)
79

810
------------------------------------------------------------------------
911
=== History: ===

src/main/java/com/fasterxml/jackson/databind/ser/std/IterableSerializer.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ public boolean isEmpty(Iterable<?> value) {
4848

4949
@Override
5050
public boolean hasSingleElement(Iterable<?> value) {
51-
// no really good way to determine (without consuming iterator), so:
51+
// we can do it actually (fixed in 2.3.1)
52+
if (value != null) {
53+
Iterator<?> it = value.iterator();
54+
if (it.hasNext()) {
55+
it.next();
56+
if (!it.hasNext()) {
57+
return true;
58+
}
59+
}
60+
}
5261
return false;
5362
}
5463

@@ -66,22 +75,24 @@ public void serializeContents(Iterable<?> value, JsonGenerator jgen, SerializerP
6675
Object elem = it.next();
6776
if (elem == null) {
6877
provider.defaultSerializeNull(jgen);
69-
} else {
78+
continue;
79+
}
80+
JsonSerializer<Object> currSerializer = _elementSerializer;
81+
if (currSerializer == null) {
7082
// Minor optimization to avoid most lookups:
7183
Class<?> cc = elem.getClass();
72-
JsonSerializer<Object> currSerializer;
7384
if (cc == prevClass) {
7485
currSerializer = prevSerializer;
7586
} else {
7687
currSerializer = provider.findValueSerializer(cc, _property);
7788
prevSerializer = currSerializer;
7889
prevClass = cc;
7990
}
80-
if (typeSer == null) {
81-
currSerializer.serialize(elem, jgen, provider);
82-
} else {
83-
currSerializer.serializeWithType(elem, jgen, provider, typeSer);
84-
}
91+
}
92+
if (typeSer == null) {
93+
currSerializer.serialize(elem, jgen, provider);
94+
} else {
95+
currSerializer.serializeWithType(elem, jgen, provider, typeSer);
8596
}
8697
} while (it.hasNext());
8798
}

src/test/java/com/fasterxml/jackson/databind/ser/TestCollectionSerialization.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,25 @@ public void remove() { }
127127

128128
public int getX() { return 13; }
129129
}
130-
130+
131+
// [Issue#358]
132+
static class A {
133+
public String unexpected = "Bye.";
134+
}
135+
136+
static class B {
137+
@JsonSerialize(as = Iterable.class, contentUsing = ASerializer.class)
138+
public List<A> list = Arrays.asList(new A());
139+
}
140+
static class ASerializer extends JsonSerializer<A> {
141+
@Override
142+
public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
143+
jsonGenerator.writeStartArray();
144+
jsonGenerator.writeString("Hello world.");
145+
jsonGenerator.writeEndArray();
146+
}
147+
}
148+
131149
/*
132150
/**********************************************************
133151
/* Test methods
@@ -349,4 +367,10 @@ public void testWithIterable() throws IOException
349367
assertEquals("[1,2,3]",
350368
MAPPER.writeValueAsString(new IntIterable()));
351369
}
370+
371+
// [Issue#358]
372+
public void testIterable358() throws Exception {
373+
String json = MAPPER.writeValueAsString(new B());
374+
assertEquals("{\"list\":[[\"Hello world.\"]]}", json);
375+
}
352376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.io.IOException;
4+
import java.util.*;
5+
6+
import com.fasterxml.jackson.core.JsonGenerator;
7+
8+
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
10+
11+
public class TestIterable358 extends BaseMapTest
12+
{
13+
// [Issue#358]
14+
static class A {
15+
public String unexpected = "Bye.";
16+
}
17+
18+
static class B {
19+
@JsonSerialize(as = Iterable.class, contentUsing = ASerializer.class)
20+
public List<A> list = Arrays.asList(new A());
21+
}
22+
static class ASerializer extends JsonSerializer<A> {
23+
@Override
24+
public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
25+
jsonGenerator.writeStartArray();
26+
jsonGenerator.writeString("Hello world.");
27+
jsonGenerator.writeEndArray();
28+
}
29+
}
30+
31+
final ObjectMapper MAPPER = new ObjectMapper();
32+
33+
public void testIterable358() throws Exception {
34+
String json = MAPPER.writeValueAsString(new B());
35+
assertEquals("{\"list\":[[\"Hello world.\"]]}", json);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)