org.codehaus.jackson.map.deser.StdDeserializer Maven / Gradle / Ivy
package org.codehaus.jackson.map.deser;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.codehaus.jackson.Base64Variants;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.map.annotate.JacksonStdImpl;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.util.TokenBuffer;
/**
* Base class for common deserializers. Contains shared
* base functionality for dealing with primitive values, such
* as (re)parsing from String.
*/
public abstract class StdDeserializer
extends JsonDeserializer
{
/**
* Type of values this deserializer handles: sometimes
* exact types, other time most specific supertype of
* types deserializer handles (which may be as generic
* as {@link Object} in some case)
*/
final protected Class> _valueClass;
protected StdDeserializer(Class> vc) {
_valueClass = vc;
}
/*
/**********************************************************
/* Extended API
/**********************************************************
*/
public Class> getValueClass() { return _valueClass; }
/**
* Exact structured type deserializer handles, if known.
*
* Default implementation just returns null.
*/
public JavaType getValueType() { return null; }
/*
/**********************************************************
/* Partial JsonDeserializer implementation
/**********************************************************
*/
/**
* Base implementation that does not assume specific type
* inclusion mechanism. Sub-classes are expected to override
* this method if they are to handle type information.
*/
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException, JsonProcessingException
{
return typeDeserializer.deserializeTypedFromAny(jp, ctxt);
}
/*
/**********************************************************
/* Helper methods for sub-classes, parsing
/**********************************************************
*/
protected final boolean _parseBoolean(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_TRUE) {
return true;
}
if (t == JsonToken.VALUE_FALSE) {
return false;
}
// [JACKSON-78]: should accept ints too, (0 == false, otherwise true)
if (t == JsonToken.VALUE_NUMBER_INT) {
return (jp.getIntValue() == 0) ? Boolean.FALSE : Boolean.TRUE;
}
// And finally, let's allow Strings to be converted too
if (t == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
if ("true".equals(text)) {
return Boolean.TRUE;
}
if ("false".equals(text)) {
return Boolean.FALSE;
}
throw ctxt.weirdStringException(_valueClass, "only \"true\" or \"false\" recognized");
}
if (t == JsonToken.VALUE_NULL) {
return Boolean.FALSE;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final Short _parseShort(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NULL) {
return (short) 0;
}
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return jp.getShortValue();
}
int value = _parseIntPrimitive(jp, ctxt);
// So far so good: but does it fit?
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
throw ctxt.weirdStringException(_valueClass, "overflow, value can not be represented as 16-bit value");
}
return (short) value;
}
protected final short _parseShortPrimitive(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
int value = _parseIntPrimitive(jp, ctxt);
// So far so good: but does it fit?
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
throw ctxt.weirdStringException(_valueClass, "overflow, value can not be represented as 16-bit value");
}
return (short) value;
}
protected final int _parseIntPrimitive(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
// Int works as is, coercing fine as well
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return jp.getIntValue();
}
if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
/* 31-Dec-2009, tatus: Should improve handling of overflow
* values... but this'll have to do for now
*/
String text = jp.getText().trim();
try {
int len = text.length();
if (len > 9) {
long l = Long.parseLong(text);
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw ctxt.weirdStringException(_valueClass,
"Overflow: numeric value ("+text+") out of range of int ("+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE+")");
}
return (int) l;
}
if (len == 0) {
return 0;
}
return Integer.parseInt(text);
} catch (IllegalArgumentException iae) {
throw ctxt.weirdStringException(_valueClass, "not a valid int value");
}
}
if (t == JsonToken.VALUE_NULL) {
return 0;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final Integer _parseInteger(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return Integer.valueOf(jp.getIntValue());
}
if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
String text = jp.getText().trim();
try {
int len = text.length();
if (len > 9) {
long l = Long.parseLong(text);
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw ctxt.weirdStringException(_valueClass,
"Overflow: numeric value ("+text+") out of range of Integer ("+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE+")");
}
return Integer.valueOf((int) l);
}
if (len == 0) {
return null;
}
return Integer.parseInt(text);
} catch (IllegalArgumentException iae) {
throw ctxt.weirdStringException(_valueClass, "not a valid Integer value");
}
}
if (t == JsonToken.VALUE_NULL) {
return null;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final Long _parseLong(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
// it should be ok to coerce (although may fail, too)
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
return jp.getLongValue();
}
// let's allow Strings to be converted too
if (t == JsonToken.VALUE_STRING) {
// !!! 05-Jan-2009, tatu: Should we try to limit value space, JDK is too lenient?
String text = jp.getText().trim();
if (text.length() == 0) {
return null;
}
try {
return Long.parseLong(text);
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(_valueClass, "not a valid Long value");
}
if (t == JsonToken.VALUE_NULL) {
return null;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final long _parseLongPrimitive(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
return jp.getLongValue();
}
if (t == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
if (text.length() == 0) {
return 0L;
}
try {
return Long.parseLong(text);
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(_valueClass, "not a valid long value");
}
if (t == JsonToken.VALUE_NULL) {
return 0L;
}
throw ctxt.mappingException(_valueClass);
}
protected final Float _parseFloat(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
// We accept couple of different types; obvious ones first:
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return jp.getFloatValue();
}
// And finally, let's allow Strings to be converted too
if (t == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
if (text.length() == 0) {
return null;
}
switch (text.charAt(0)) {
case 'I':
if ("Infinity".equals(text) || "INF".equals(text)) {
return Float.POSITIVE_INFINITY;
}
break;
case 'N':
if ("NaN".equals(text)) {
return Float.NaN;
}
break;
case '-':
if ("-Infinity".equals(text) || "-INF".equals(text)) {
return Float.NEGATIVE_INFINITY;
}
break;
}
try {
return Float.parseFloat(text);
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(_valueClass, "not a valid Float value");
}
if (t == JsonToken.VALUE_NULL) {
return null;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final float _parseFloatPrimitive(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return jp.getFloatValue();
}
if (t == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
if (text.length() == 0) {
return 0.0f;
}
switch (text.charAt(0)) {
case 'I':
if ("Infinity".equals(text) || "INF".equals(text)) {
return Float.POSITIVE_INFINITY;
}
break;
case 'N':
if ("NaN".equals(text)) {
return Float.NaN;
}
break;
case '-':
if ("-Infinity".equals(text) || "-INF".equals(text)) {
return Float.NEGATIVE_INFINITY;
}
break;
}
try {
return Float.parseFloat(text);
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(_valueClass, "not a valid float value");
}
if (t == JsonToken.VALUE_NULL) {
return 0.0f;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final Double _parseDouble(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return jp.getDoubleValue();
}
if (t == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
if (text.length() == 0) {
return null;
}
switch (text.charAt(0)) {
case 'I':
if ("Infinity".equals(text) || "INF".equals(text)) {
return Double.POSITIVE_INFINITY;
}
break;
case 'N':
if ("NaN".equals(text)) {
return Double.NaN;
}
break;
case '-':
if ("-Infinity".equals(text) || "-INF".equals(text)) {
return Double.NEGATIVE_INFINITY;
}
break;
}
try {
return Double.parseDouble(text);
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(_valueClass, "not a valid Double value");
}
if (t == JsonToken.VALUE_NULL) {
return null;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected final double _parseDoublePrimitive(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
// We accept couple of different types; obvious ones first:
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
return jp.getDoubleValue();
}
// And finally, let's allow Strings to be converted too
if (t == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
if (text.length() == 0) {
return 0.0;
}
switch (text.charAt(0)) {
case 'I':
if ("Infinity".equals(text) || "INF".equals(text)) {
return Double.POSITIVE_INFINITY;
}
break;
case 'N':
if ("NaN".equals(text)) {
return Double.NaN;
}
break;
case '-':
if ("-Infinity".equals(text) || "-INF".equals(text)) {
return Double.NEGATIVE_INFINITY;
}
break;
}
try {
return Double.parseDouble(text);
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(_valueClass, "not a valid double value");
}
if (t == JsonToken.VALUE_NULL) {
return 0.0;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass);
}
protected java.util.Date _parseDate(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
try {
if (t == JsonToken.VALUE_NUMBER_INT) {
return new java.util.Date(jp.getLongValue());
}
if (t == JsonToken.VALUE_STRING) {
/* As per [JACKSON-203], take empty Strings to mean
* null
*/
String str = jp.getText().trim();
if (str.length() == 0) {
return null;
}
return ctxt.parseDate(str);
}
throw ctxt.mappingException(_valueClass);
} catch (IllegalArgumentException iae) {
throw ctxt.weirdStringException(_valueClass, "not a valid representation (error: "+iae.getMessage()+")");
}
}
/*
/****************************************************
/* Helper methods for sub-classes, resolving dependencies
/****************************************************
*/
/**
* Helper method used to locate deserializers for properties the
* type this deserializer handles contains (usually for properties of
* bean types)
*
* @param type Type of property to deserialize
*/
protected JsonDeserializer