Skip to content

Commit

Permalink
Fixed #358
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 4, 2013
1 parent 96dd040 commit ce457af
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.0</version>
<version>2.3.1-SNAPSHOT</version>
</dependency>

<!-- and for testing, JUnit is needed, as well as quite a few
Expand Down
4 changes: 4 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ David Phillips:
Seth Pellegrino (jivesoft):
* Contributed #317: Fix `JsonNode` support for nulls bound to `ObjectNode`, `ArrayNode`
(2.3.0)

Florian Schoppmann (fschopp@github)
* Reported #358: `IterableSerializer` ignoring annotated content serializer
(2.3.1)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Version: 2.3.1 (xx-Dec-2013)
#346: Fix problem deserializing `ObjectNode`, with @JsonCreator, empty
JSON Object
(reported by gaff78@github)
#358: `IterableSerializer` ignoring annotated content serializer
(reported by Florian S)

------------------------------------------------------------------------
=== History: ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ public boolean isEmpty(Iterable<?> value) {

@Override
public boolean hasSingleElement(Iterable<?> value) {
// no really good way to determine (without consuming iterator), so:
// we can do it actually (fixed in 2.3.1)
if (value != null) {
Iterator<?> it = value.iterator();
if (it.hasNext()) {
it.next();
if (!it.hasNext()) {
return true;
}
}
}
return false;
}

Expand All @@ -66,22 +75,24 @@ public void serializeContents(Iterable<?> value, JsonGenerator jgen, SerializerP
Object elem = it.next();
if (elem == null) {
provider.defaultSerializeNull(jgen);
} else {
continue;
}
JsonSerializer<Object> currSerializer = _elementSerializer;
if (currSerializer == null) {
// Minor optimization to avoid most lookups:
Class<?> cc = elem.getClass();
JsonSerializer<Object> currSerializer;
if (cc == prevClass) {
currSerializer = prevSerializer;
} else {
currSerializer = provider.findValueSerializer(cc, _property);
prevSerializer = currSerializer;
prevClass = cc;
}
if (typeSer == null) {
currSerializer.serialize(elem, jgen, provider);
} else {
currSerializer.serializeWithType(elem, jgen, provider, typeSer);
}
}
if (typeSer == null) {
currSerializer.serialize(elem, jgen, provider);
} else {
currSerializer.serializeWithType(elem, jgen, provider, typeSer);
}
} while (it.hasNext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,25 @@ public void remove() { }

public int getX() { return 13; }
}


// [Issue#358]
static class A {
public String unexpected = "Bye.";
}

static class B {
@JsonSerialize(as = Iterable.class, contentUsing = ASerializer.class)
public List<A> list = Arrays.asList(new A());
}
static class ASerializer extends JsonSerializer<A> {
@Override
public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeString("Hello world.");
jsonGenerator.writeEndArray();
}
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -349,4 +367,10 @@ public void testWithIterable() throws IOException
assertEquals("[1,2,3]",
MAPPER.writeValueAsString(new IntIterable()));
}

// [Issue#358]
public void testIterable358() throws Exception {
String json = MAPPER.writeValueAsString(new B());
assertEquals("{\"list\":[[\"Hello world.\"]]}", json);
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/fasterxml/jackson/failing/TestIterable358.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fasterxml.jackson.failing;

import java.io.IOException;
import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class TestIterable358 extends BaseMapTest
{
// [Issue#358]
static class A {
public String unexpected = "Bye.";
}

static class B {
@JsonSerialize(as = Iterable.class, contentUsing = ASerializer.class)
public List<A> list = Arrays.asList(new A());
}
static class ASerializer extends JsonSerializer<A> {
@Override
public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeString("Hello world.");
jsonGenerator.writeEndArray();
}
}

final ObjectMapper MAPPER = new ObjectMapper();

public void testIterable358() throws Exception {
String json = MAPPER.writeValueAsString(new B());
assertEquals("{\"list\":[[\"Hello world.\"]]}", json);
}

}

0 comments on commit ce457af

Please sign in to comment.