org.codehaus.jackson.map.ser.std.StringCollectionSerializer Maven / Gradle / Ivy
Go to download
Data Mapper package is a high-performance data binding package
built on Jackson JSON processor
package org.codehaus.jackson.map.ser.std;
import java.io.IOException;
import java.util.*;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.BeanProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ResolvableSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.TypeSerializer;
import org.codehaus.jackson.map.annotate.JacksonStdImpl;
/**
* Efficient implement for serializing {@link Collection}s that contain Strings.
* The only complexity is due to possibility that serializer for {@link String}
* may be overridde; because of this, logic is needed to ensure that the default
* serializer is in use to use fastest mode, or if not, to defer to custom
* String serializer.
*/
@JacksonStdImpl
public class StringCollectionSerializer
extends StaticListSerializerBase>
implements ResolvableSerializer
{
protected JsonSerializer _serializer;
@Deprecated
public StringCollectionSerializer(BeanProperty property) {
this(property, null);
}
@SuppressWarnings("unchecked")
public StringCollectionSerializer(BeanProperty property, JsonSerializer> ser) {
super(Collection.class, property);
_serializer = (JsonSerializer) ser;
}
@Override protected JsonNode contentSchema() {
return createSchemaNode("string", true);
}
@SuppressWarnings("unchecked")
@Override
public void resolve(SerializerProvider provider) throws JsonMappingException
{
if (_serializer == null) {
JsonSerializer> ser = provider.findValueSerializer(String.class, _property);
if (!isDefaultSerializer(ser)) {
_serializer = (JsonSerializer) ser;
}
}
}
@Override
public void serialize(Collection value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
jgen.writeStartArray();
if (_serializer == null) {
serializeContents(value, jgen, provider);
} else {
serializeUsingCustom(value, jgen, provider);
}
jgen.writeEndArray();
}
@Override
public void serializeWithType(Collection value, JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException, JsonGenerationException
{
typeSer.writeTypePrefixForArray(value, jgen);
if (_serializer == null) {
serializeContents(value, jgen, provider);
} else {
serializeUsingCustom(value, jgen, provider);
}
typeSer.writeTypeSuffixForArray(value, jgen);
}
private final void serializeContents(Collection value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
if (_serializer != null) {
serializeUsingCustom(value, jgen, provider);
return;
}
int i = 0;
for (String str : value) {
try {
if (str == null) {
provider.defaultSerializeNull(jgen);
} else {
jgen.writeString(str);
}
++i;
} catch (Exception e) {
wrapAndThrow(provider, e, value, i);
}
}
}
private void serializeUsingCustom(Collection value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
final JsonSerializer ser = _serializer;
int i = 0;
for (String str : value) {
try {
if (str == null) {
provider.defaultSerializeNull(jgen);
} else {
ser.serialize(str, jgen, provider);
}
} catch (Exception e) {
wrapAndThrow(provider, e, value, i);
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy