com.fasterxml.jackson.databind.ser.std.NumberSerializers Maven / Gradle / Ivy
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.NumberOutput;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
/**
* Container class for serializers used for handling standard JDK-provided
* primitve number types and their wrapper counterparts (like {@link java.lang.Integer}).
*/
@SuppressWarnings("serial")
public class NumberSerializers {
protected NumberSerializers() { }
public static void addAll(Map> allDeserializers) {
allDeserializers.put(Integer.class.getName(), new IntegerSerializer(Integer.class));
allDeserializers.put(Integer.TYPE.getName(), new IntegerSerializer(Integer.TYPE));
allDeserializers.put(Long.class.getName(), new LongSerializer(Long.class));
allDeserializers.put(Long.TYPE.getName(), new LongSerializer(Long.TYPE));
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(Double.class.getName(), new DoubleSerializer(Double.class));
allDeserializers.put(Double.TYPE.getName(), new DoubleSerializer(Double.TYPE));
allDeserializers.put(Float.class.getName(), FloatSerializer.instance);
allDeserializers.put(Float.TYPE.getName(), FloatSerializer.instance);
}
/*
/**********************************************************
/* Shared base class
/**********************************************************
*/
/**
* Shared base class for actual primitive/wrapper number serializers.
* Note that this class is not meant as general-purpose base class nor
* is it part of public API: you may extend it with the caveat that not
* being part of public API its implementation and interfaces may change
* in minor releases; however deprecation markers will be used to allow
* code evolution.
*
* NOTE: {@code public} since 2.10: previously had {@code protected} access.
*/
public 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);
}
/**
* @deprecated Since 2.15
*/
@Deprecated
@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
{
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
if (format != null) {
switch (format.getShape()) {
case STRING:
if (((Class>) handledType()) == BigDecimal.class) {
return NumberSerializer.bigDecimalAsStringSerializer();
}
return ToStringSerializer.instance;
default:
}
}
return this;
}
}
/*
*************************************************************
* Concrete serializers, numerics
*************************************************************
*/
@JacksonStdImpl
public static class ShortSerializer extends Base