package com.fasterxml.jackson.databind.ser.impl;
import java.io.IOException;
import java.lang.reflect.Type;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.ArraySerializerBase;
import com.fasterxml.jackson.databind.type.TypeFactory;
@JacksonStdImpl
@SuppressWarnings("serial")
public class StringArraySerializer
extends ArraySerializerBase<String[]>
implements ContextualSerializer
{
@SuppressWarnings("deprecation")
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(String.class);
public final static StringArraySerializer instance = new StringArraySerializer();
protected final JsonSerializer<Object> _elementSerializer;
protected StringArraySerializer() {
super(String[].class);
_elementSerializer = null;
}
@SuppressWarnings("unchecked")
public StringArraySerializer(StringArraySerializer src,
BeanProperty prop, JsonSerializer<?> ser, Boolean unwrapSingle) {
super(src, prop, unwrapSingle);
_elementSerializer = (JsonSerializer<Object>) ser;
}
@Override
public JsonSerializer<?> _withResolved(BeanProperty prop, Boolean unwrapSingle) {
return new StringArraySerializer(this, prop, _elementSerializer, unwrapSingle);
}
@Override
public ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
return this;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = null;
if (property != null) {
final AnnotationIntrospector ai = provider.getAnnotationIntrospector();
AnnotatedMember m = property.getMember();
if (m != null) {
Object serDef = ai.findContentSerializer(m);
if (serDef != null) {
ser = provider.serializerInstance(m, serDef);
}
}
}
Boolean unwrapSingle = findFormatFeature(provider, property, String[].class,
JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
if (ser == null) {
ser = _elementSerializer;
}
ser = findContextualConvertingSerializer(provider, property, ser);
if (ser == null) {
ser = provider.findContentValueSerializer(String.class, property);
}
if (isDefaultSerializer(ser)) {
ser = null;
}
if ((ser == _elementSerializer) && (unwrapSingle == _unwrapSingle)) {
return this;
}
return new StringArraySerializer(this, property, ser, unwrapSingle);
}
@Override
public JavaType getContentType() {
return VALUE_TYPE;
}
@Override
public JsonSerializer<?> getContentSerializer() {
return _elementSerializer;
}
@Override
public boolean isEmpty(SerializerProvider prov, String[] value) {
return (value.length == 0);
}
@Override
public boolean hasSingleElement(String[] value) {
return (value.length == 1);
}
@Override
public final void serialize(String[] value, JsonGenerator gen, SerializerProvider provider)
throws IOException
{
final int len = value.length;
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(String[] value, JsonGenerator gen, SerializerProvider provider)
throws IOException
{
final int len = value.length;
if (len == 0) {
return;
}
if (_elementSerializer != null) {
serializeContentsSlow(value, gen, provider, _elementSerializer);
return;
}
for (int i = 0; i < len; ++i) {
String str = value[i];
if (str == null) {
gen.writeNull();
} else {
gen.writeString(value[i]);
}
}
}
private void serializeContentsSlow(String[] value, JsonGenerator gen, SerializerProvider provider, JsonSerializer<Object> ser)
throws IOException
{
for (int i = 0, len = value.length; i < len; ++i) {
String str = value[i];
if (str == null) {
provider.defaultSerializeNull(gen);
} else {
ser.serialize(value[i], gen, provider);
}
}
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("array", true).set("items", createSchemaNode("string"));
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
{
visitArrayFormat(visitor, typeHint, JsonFormatTypes.STRING);
}
}