com.fasterxml.jackson.databind.ser.std.IterableSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-databind Show documentation
Show all versions of jackson-databind Show documentation
General data-binding functionality for Jackson: works on core streaming API
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
@JacksonStdImpl
@SuppressWarnings("serial")
public class IterableSerializer
extends AsArraySerializerBase>
{
public IterableSerializer(JavaType elemType, boolean staticTyping,
TypeSerializer vts, BeanProperty property)
{
super(Iterable.class, elemType, staticTyping, vts, property, null);
}
public IterableSerializer(IterableSerializer src, BeanProperty property,
TypeSerializer vts, JsonSerializer valueSerializer)
{
super(src, property, vts, valueSerializer);
}
@Override
public ContainerSerializer _withValueTypeSerializer(TypeSerializer vts) {
return new IterableSerializer(_elementType, _staticTyping, vts, _property);
}
@Override
public IterableSerializer withResolved(BeanProperty property,
TypeSerializer vts, JsonSerializer elementSerializer) {
return new IterableSerializer(this, property, vts, elementSerializer);
}
@Override
public boolean isEmpty(SerializerProvider prov, Iterable value) {
// Not really good way to implement this, but has to do for now:
return (value == null) || !value.iterator().hasNext();
}
@Override
public boolean hasSingleElement(Iterable value) {
// 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;
}
@Override
public final void serialize(Iterable value, JsonGenerator jgen, SerializerProvider provider) throws IOException
{
if (provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED) && hasSingleElement(value)) {
serializeContents(value, jgen, provider);
return;
}
jgen.writeStartArray();
serializeContents(value, jgen, provider);
jgen.writeEndArray();
}
@Override
public void serializeContents(Iterable value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
Iterator it = value.iterator();
if (it.hasNext()) {
final TypeSerializer typeSer = _valueTypeSerializer;
JsonSerializer