com.fitbur.jackson.databind.ser.std.NumberSerializers Maven / Gradle / Ivy
package com.fitbur.jackson.databind.ser.std;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import com.fitbur.jackson.annotation.JsonFormat;
import com.fitbur.jackson.core.*;
import com.fitbur.jackson.databind.*;
import com.fitbur.jackson.databind.annotation.JacksonStdImpl;
import com.fitbur.jackson.databind.introspect.AnnotatedMember;
import com.fitbur.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fitbur.jackson.databind.jsontype.TypeSerializer;
import com.fitbur.jackson.databind.ser.ContextualSerializer;
/**
* Container class for serializers used for handling standard JDK-provided
* types.
*/
@SuppressWarnings("serial")
public class NumberSerializers {
protected NumberSerializers() {
}
public static void addAll(Map> allDeserializers) {
final JsonSerializer> intS = new IntegerSerializer();
allDeserializers.put(Integer.class.getName(), intS);
allDeserializers.put(Integer.TYPE.getName(), intS);
allDeserializers.put(Long.class.getName(), LongSerializer.instance);
allDeserializers.put(Long.TYPE.getName(), LongSerializer.instance);
allDeserializers.put(Byte.class.getName(), IntLikeSerializer.instance);
allDeserializers.put(Byte.TYPE.getName(), IntLikeSerializer.instance);
allDeserializers.put(Short.class.getName(), ShortSerializer.instance);
allDeserializers.put(Short.TYPE.getName(), ShortSerializer.instance);
// Numbers, limited length floating point
allDeserializers.put(Float.class.getName(), FloatSerializer.instance);
allDeserializers.put(Float.TYPE.getName(), FloatSerializer.instance);
allDeserializers.put(Double.class.getName(), DoubleSerializer.instance);
allDeserializers.put(Double.TYPE.getName(), DoubleSerializer.instance);
}
/*
/**********************************************************
/* Shared base class
/**********************************************************
*/
protected abstract static class Base extends StdScalarSerializer
implements ContextualSerializer {
protected final JsonParser.NumberType _numberType;
protected final String _schemaType;
protected final boolean _isInt;
protected Base(Class> cls, JsonParser.NumberType numberType,
String schemaType) {
super(cls, false);
_numberType = numberType;
_schemaType = schemaType;
_isInt = (numberType == JsonParser.NumberType.INT)
|| (numberType == JsonParser.NumberType.LONG)
|| (numberType == JsonParser.NumberType.BIG_INTEGER);
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode(_schemaType, true);
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor,
JavaType typeHint) throws JsonMappingException
{
if (_isInt) {
visitIntFormat(visitor, typeHint, _numberType);
} else {
visitFloatFormat(visitor, typeHint, _numberType);
}
}
@Override
public JsonSerializer> createContextual(SerializerProvider prov,
BeanProperty property) throws JsonMappingException {
if (property != null) {
AnnotatedMember m = property.getMember();
if (m != null) {
JsonFormat.Value format = prov.getAnnotationIntrospector()
.findFormat(m);
if (format != null) {
switch (format.getShape()) {
case STRING:
return ToStringSerializer.instance;
default:
}
}
}
}
return this;
}
}
/*
* /********************************************************** /* Concrete
* serializers, numerics
* /**********************************************************
*/
@JacksonStdImpl
public final static class ShortSerializer extends Base
© 2015 - 2024 Weber Informatics LLC | Privacy Policy