Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.juli.logging.com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer Maven / Gradle / Ivy
package org.apache.juli.logging.com.fasterxml.jackson.databind.ser.impl;
import java.io.IOException;
import java.util.*;
import org.apache.juli.logging.com.fasterxml.jackson.core.JsonGenerator;
import org.apache.juli.logging.com.fasterxml.jackson.databind.*;
import org.apache.juli.logging.com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import org.apache.juli.logging.com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.apache.juli.logging.com.fasterxml.jackson.databind.ser.ContainerSerializer;
import org.apache.juli.logging.com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase;
/**
* This is an optimized serializer for Lists that can be efficiently
* traversed by index (as opposed to others, such as {@link LinkedList}
* that cannot}.
*/
@JacksonStdImpl
public final class IndexedListSerializer
extends AsArraySerializerBase>
{
private static final long serialVersionUID = 1L;
public IndexedListSerializer(JavaType elemType, boolean staticTyping, TypeSerializer vts,
JsonSerializer valueSerializer)
{
super(List.class, elemType, staticTyping, vts, valueSerializer);
}
public IndexedListSerializer(IndexedListSerializer src,
BeanProperty property, TypeSerializer vts, JsonSerializer valueSerializer,
Boolean unwrapSingle) {
super(src, property, vts, valueSerializer, unwrapSingle);
}
@Override
public IndexedListSerializer withResolved(BeanProperty property,
TypeSerializer vts, JsonSerializer elementSerializer,
Boolean unwrapSingle) {
return new IndexedListSerializer(this, property, vts, elementSerializer, unwrapSingle);
}
/*
/**********************************************************
/* Accessors
/**********************************************************
*/
@Override
public boolean isEmpty(SerializerProvider prov, List value) {
return value.isEmpty();
}
@Override
public boolean hasSingleElement(List value) {
return (value.size() == 1);
}
@Override
public ContainerSerializer _withValueTypeSerializer(TypeSerializer vts) {
return new IndexedListSerializer(this,
_property, vts, _elementSerializer, _unwrapSingle);
}
@Override
public final void serialize(List value, JsonGenerator gen, SerializerProvider provider)
throws IOException
{
final int len = value.size();
if (len == 1) {
if (((_unwrapSingle == null) &&
provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| (_unwrapSingle == Boolean.TRUE)) {
serializeContents(value, gen, provider);
return;
}
}
gen.writeStartArray(value, len);
serializeContents(value, gen, provider);
gen.writeEndArray();
}
@Override
public void serializeContents(List value, JsonGenerator g, SerializerProvider provider)
throws IOException
{
if (_elementSerializer != null) {
serializeContentsUsing(value, g, provider, _elementSerializer);
return;
}
if (_valueTypeSerializer != null) {
serializeTypedContents(value, g, provider);
return;
}
final int len = value.size();
if (len == 0) {
return;
}
int i = 0;
try {
PropertySerializerMap serializers = _dynamicSerializers;
for (; i < len; ++i) {
Object elem = value.get(i);
if (elem == null) {
provider.defaultSerializeNull(g);
} else {
Class cc = elem.getClass();
JsonSerializer serializer = serializers.serializerFor(cc);
if (serializer == null) {
// To fix [JACKSON-508]
if (_elementType.hasGenericTypes()) {
serializer = _findAndAddDynamic(serializers,
provider.constructSpecializedType(_elementType, cc), provider);
} else {
serializer = _findAndAddDynamic(serializers, cc, provider);
}
serializers = _dynamicSerializers;
}
serializer.serialize(elem, g, provider);
}
}
} catch (Exception e) {
wrapAndThrow(provider, e, value, i);
}
}
public void serializeContentsUsing(List value, JsonGenerator jgen, SerializerProvider provider,
JsonSerializer ser)
throws IOException
{
final int len = value.size();
if (len == 0) {
return;
}
final TypeSerializer typeSer = _valueTypeSerializer;
for (int i = 0; i < len; ++i) {
Object elem = value.get(i);
try {
if (elem == null) {
provider.defaultSerializeNull(jgen);
} else if (typeSer == null) {
ser.serialize(elem, jgen, provider);
} else {
ser.serializeWithType(elem, jgen, provider, typeSer);
}
} catch (Exception e) {
// [JACKSON-55] Need to add reference information
wrapAndThrow(provider, e, value, i);
}
}
}
public void serializeTypedContents(List value, JsonGenerator jgen, SerializerProvider provider)
throws IOException
{
final int len = value.size();
if (len == 0) {
return;
}
int i = 0;
try {
final TypeSerializer typeSer = _valueTypeSerializer;
PropertySerializerMap serializers = _dynamicSerializers;
for (; i < len; ++i) {
Object elem = value.get(i);
if (elem == null) {
provider.defaultSerializeNull(jgen);
} else {
Class cc = elem.getClass();
JsonSerializer serializer = serializers.serializerFor(cc);
if (serializer == null) {
// To fix [JACKSON-508]
if (_elementType.hasGenericTypes()) {
serializer = _findAndAddDynamic(serializers,
provider.constructSpecializedType(_elementType, cc), provider);
} else {
serializer = _findAndAddDynamic(serializers, cc, provider);
}
serializers = _dynamicSerializers;
}
serializer.serializeWithType(elem, jgen, provider, typeSer);
}
}
} catch (Exception e) {
wrapAndThrow(provider, e, value, i);
}
}
}