diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2014-05-10 19:18:33 -0700 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2014-05-10 19:18:33 -0700 |
commit | ec6eafb55ade5bc45c8c56c63ef94d25ac2d462e (patch) | |
tree | 5f491e4f9c78809380d5d2d6df8ce3110cd8bae8 /src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java | |
parent | 8d9e6338249d7866be55e057cc5085d1ff8abbd0 (diff) | |
download | jackson-databind-ec6eafb55ade5bc45c8c56c63ef94d25ac2d462e.tar.gz |
Add an alternative creator for collections
Diffstat (limited to 'src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java')
-rw-r--r-- | src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java index 03303e6a7..c0eadd01d 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java @@ -3,7 +3,6 @@ package com.fasterxml.jackson.databind.deser; import java.io.IOException; import com.fasterxml.jackson.core.JsonProcessingException; - import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams; @@ -143,8 +142,7 @@ public abstract class ValueInstantiator * This method is called if {@link #getFromObjectArguments} returns * null or empty List. */ - public Object createUsingDefault(DeserializationContext ctxt) - throws IOException, JsonProcessingException { + public Object createUsingDefault(DeserializationContext ctxt) throws IOException { throw new JsonMappingException("Can not instantiate value of type " +getValueTypeDesc()+"; no default creator found"); } @@ -157,8 +155,7 @@ public abstract class ValueInstantiator * This method is called if {@link #getFromObjectArguments} returns * a non-empty List of arguments. */ - public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) - throws IOException, JsonProcessingException { + public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) throws IOException { throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()+" with arguments"); } @@ -166,8 +163,7 @@ public abstract class ValueInstantiator * Method to called to create value instance from JSON Object using * an intermediate "delegate" value to pass to createor method */ - public Object createUsingDelegate(DeserializationContext ctxt, Object delegate) - throws IOException, JsonProcessingException { + public Object createUsingDelegate(DeserializationContext ctxt, Object delegate) throws IOException { throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()+" using delegate"); } @@ -178,32 +174,26 @@ public abstract class ValueInstantiator /********************************************************** */ - public Object createFromString(DeserializationContext ctxt, String value) - throws IOException, JsonProcessingException { - throw new JsonMappingException("Can not instantiate value of type " - +getValueTypeDesc()+" from String value '"+value+"'"); + public Object createFromString(DeserializationContext ctxt, String value) throws IOException { + return _createFromStringFallbacks(ctxt, value); } - - public Object createFromInt(DeserializationContext ctxt, int value) - throws IOException, JsonProcessingException { + + public Object createFromInt(DeserializationContext ctxt, int value) throws IOException { throw new JsonMappingException("Can not instantiate value of type " +getValueTypeDesc()+" from Integer number ("+value+", int)"); } - public Object createFromLong(DeserializationContext ctxt, long value) - throws IOException, JsonProcessingException { + public Object createFromLong(DeserializationContext ctxt, long value) throws IOException { throw new JsonMappingException("Can not instantiate value of type " +getValueTypeDesc()+" from Integer number ("+value+", long)"); } - public Object createFromDouble(DeserializationContext ctxt, double value) - throws IOException, JsonProcessingException { + public Object createFromDouble(DeserializationContext ctxt, double value) throws IOException { throw new JsonMappingException("Can not instantiate value of type " +getValueTypeDesc()+" from Floating-point number ("+value+", double)"); } - public Object createFromBoolean(DeserializationContext ctxt, boolean value) - throws IOException, JsonProcessingException { + public Object createFromBoolean(DeserializationContext ctxt, boolean value) throws IOException { throw new JsonMappingException("Can not instantiate value of type " +getValueTypeDesc()+" from Boolean value ("+value+")"); } @@ -225,7 +215,7 @@ public abstract class ValueInstantiator * this method may return null . */ public AnnotatedWithParams getDefaultCreator() { return null; } - + /** * Method that can be called to try to access member (constructor, * static factory method) that is used as the "delegate creator". @@ -252,4 +242,39 @@ public abstract class ValueInstantiator * needs further annotation to help make the creator complete. */ public AnnotatedParameter getIncompleteParameter() { return null; } + + /* + /********************************************************** + /* Helper methods + /********************************************************** + */ + + /** + * @since 2.4 (demoted from <code>StdValueInstantiator) + */ + protected Object _createFromStringFallbacks(DeserializationContext ctxt, String value) + throws IOException, JsonProcessingException + { + /* 28-Sep-2011, tatu: Ok this is not clean at all; but since there are legacy + * systems that expect conversions in some cases, let's just add a minimal + * patch (note: same could conceivably be used for numbers too). + */ + if (canCreateFromBoolean()) { + String str = value.trim(); + if ("true".equals(str)) { + return createFromBoolean(ctxt, true); + } + if ("false".equals(str)) { + return createFromBoolean(ctxt, false); + } + } + // also, empty Strings might be accepted as null Object... + if (value.length() == 0) { + if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) { + return null; + } + } + throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc() + +" from String value ('"+value+"'); no single-String constructor/factory method"); + } } |