com.fitbur.jackson.databind.ser.impl.IteratorSerializer Maven / Gradle / Ivy
package com.fitbur.jackson.databind.ser.impl;
import java.io.IOException;
import java.util.Iterator;
import com.fitbur.jackson.core.JsonGenerator;
import com.fitbur.jackson.databind.*;
import com.fitbur.jackson.databind.annotation.JacksonStdImpl;
import com.fitbur.jackson.databind.jsontype.TypeSerializer;
import com.fitbur.jackson.databind.ser.ContainerSerializer;
import com.fitbur.jackson.databind.ser.std.AsArraySerializerBase;
@SuppressWarnings("serial")
@JacksonStdImpl
public class IteratorSerializer
extends AsArraySerializerBase>
{
public IteratorSerializer(JavaType elemType, boolean staticTyping, TypeSerializer vts) {
super(Iterator.class, elemType, staticTyping, vts, null);
}
public IteratorSerializer(IteratorSerializer src,
BeanProperty property, TypeSerializer vts, JsonSerializer> valueSerializer,
Boolean unwrapSingle) {
super(src, property, vts, valueSerializer, unwrapSingle);
}
@Override
public boolean isEmpty(SerializerProvider prov, Iterator> value) {
return (value == null) || !value.hasNext();
}
@Override
public boolean hasSingleElement(Iterator> value) {
// no really good way to determine (without consuming iterator), so:
return false;
}
@Override
public ContainerSerializer> _withValueTypeSerializer(TypeSerializer vts) {
return new IteratorSerializer(this, _property, vts, _elementSerializer, _unwrapSingle);
}
@Override
public IteratorSerializer withResolved(BeanProperty property,
TypeSerializer vts, JsonSerializer> elementSerializer,
Boolean unwrapSingle) {
return new IteratorSerializer(this, property, vts, elementSerializer, unwrapSingle);
}
@Override
public final void serialize(Iterator> value, JsonGenerator gen,
SerializerProvider provider) throws IOException
{
if (((_unwrapSingle == null) &&
provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| (_unwrapSingle == Boolean.TRUE)) {
if (hasSingleElement(value)) {
serializeContents(value, gen, provider);
return;
}
}
gen.writeStartArray();
serializeContents(value, gen, provider);
gen.writeEndArray();
}
@Override
public void serializeContents(Iterator> value, JsonGenerator gen,
SerializerProvider provider) throws IOException
{
if (value.hasNext()) {
final TypeSerializer typeSer = _valueTypeSerializer;
JsonSerializer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy