public static <T> T deserializeNodeForCollection(JsonParser jp, DeserializationContext ctxt, JavaType javaType) throws IOException, JsonProcessingException { if (jp.isExpectedStartArrayToken()) { JsonNode jsonNode = jp.readValueAs(JsonNode.class); T result = XML_MAPPER.reader(javaType).readValue(jsonNode); return result; } else if (jp.getCurrentToken() == JsonToken.VALUE_STRING) { String str = jp.getText(); if (StringUtils.isEmpty(StringUtils.trimAllWhitespace(str))) { // TODO: cleanup final DeserializationConfig config = ctxt.getConfig(); BeanDescription beanDesc = config.introspect(javaType); boolean fixAccess = ctxt.canOverrideAccessModifiers(); CreatorCollector creators = new CreatorCollector(beanDesc, fixAccess); ValueInstantiator valueInstantiator = creators.constructValueInstantiator(config); return (T) valueInstantiator.createUsingDefault(ctxt); } } throw ctxt.mappingException("Expected XML element"); }
/** * Method that will construct standard default {@link ValueInstantiator} * using annotations (like @JsonCreator) and visibility rules */ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationContext ctxt, BeanDescription beanDesc) throws JsonMappingException { boolean fixAccess = ctxt.canOverrideAccessModifiers(); CreatorCollector creators = new CreatorCollector(beanDesc, fixAccess); AnnotationIntrospector intr = ctxt.getAnnotationIntrospector(); // need to construct suitable visibility checker: final DeserializationConfig config = ctxt.getConfig(); VisibilityChecker<?> vchecker = config.getDefaultVisibilityChecker(); vchecker = intr.findAutoDetectVisibility(beanDesc.getClassInfo(), vchecker); /* Important: first add factory methods; then constructors, so * latter can override former! */ _addDeserializerFactoryMethods(ctxt, beanDesc, vchecker, intr, creators); // constructors only usable on concrete types: if (beanDesc.getType().isConcrete()) { _addDeserializerConstructors(ctxt, beanDesc, vchecker, intr, creators); } return creators.constructValueInstantiator(config); }
protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationContext paramDeserializationContext, BeanDescription paramBeanDescription) { CreatorCollector localCreatorCollector = new CreatorCollector(paramBeanDescription, paramDeserializationContext.canOverrideAccessModifiers()); AnnotationIntrospector localAnnotationIntrospector = paramDeserializationContext.getAnnotationIntrospector(); DeserializationConfig localDeserializationConfig = paramDeserializationContext.getConfig(); VisibilityChecker localVisibilityChecker1 = localDeserializationConfig.getDefaultVisibilityChecker(); VisibilityChecker localVisibilityChecker2 = localAnnotationIntrospector.findAutoDetectVisibility(paramBeanDescription.getClassInfo(), localVisibilityChecker1); _addDeserializerFactoryMethods(paramDeserializationContext, paramBeanDescription, localVisibilityChecker2, localAnnotationIntrospector, localCreatorCollector); if (paramBeanDescription.getType().isConcrete()) _addDeserializerConstructors(paramDeserializationContext, paramBeanDescription, localVisibilityChecker2, localAnnotationIntrospector, localCreatorCollector); return localCreatorCollector.constructValueInstantiator(localDeserializationConfig); }
protected boolean _handleSingleArgumentFactory(DeserializationConfig paramDeserializationConfig, BeanDescription paramBeanDescription, VisibilityChecker<?> paramVisibilityChecker, AnnotationIntrospector paramAnnotationIntrospector, CreatorCollector paramCreatorCollector, AnnotatedMethod paramAnnotatedMethod, boolean paramBoolean) { Class localClass = paramAnnotatedMethod.getRawParameterType(0); if (localClass == String.class) { if ((paramBoolean) || (paramVisibilityChecker.isCreatorVisible(paramAnnotatedMethod))) paramCreatorCollector.addStringCreator(paramAnnotatedMethod); return true; } if ((localClass == Integer.TYPE) || (localClass == Integer.class)) { if ((paramBoolean) || (paramVisibilityChecker.isCreatorVisible(paramAnnotatedMethod))) paramCreatorCollector.addIntCreator(paramAnnotatedMethod); return true; } if ((localClass == Long.TYPE) || (localClass == Long.class)) { if ((paramBoolean) || (paramVisibilityChecker.isCreatorVisible(paramAnnotatedMethod))) paramCreatorCollector.addLongCreator(paramAnnotatedMethod); return true; } if ((localClass == Double.TYPE) || (localClass == Double.class)) { if ((paramBoolean) || (paramVisibilityChecker.isCreatorVisible(paramAnnotatedMethod))) paramCreatorCollector.addDoubleCreator(paramAnnotatedMethod); return true; } if ((localClass == Boolean.TYPE) || (localClass == Boolean.class)) { if ((paramBoolean) || (paramVisibilityChecker.isCreatorVisible(paramAnnotatedMethod))) paramCreatorCollector.addBooleanCreator(paramAnnotatedMethod); return true; } if (paramAnnotationIntrospector.hasCreatorAnnotation(paramAnnotatedMethod)) { paramCreatorCollector.addDelegatingCreator(paramAnnotatedMethod, null); return true; } return false; }
protected void _addDeserializerFactoryMethods(DeserializationContext paramDeserializationContext, BeanDescription paramBeanDescription, VisibilityChecker<?> paramVisibilityChecker, AnnotationIntrospector paramAnnotationIntrospector, CreatorCollector paramCreatorCollector) { DeserializationConfig localDeserializationConfig = paramDeserializationContext.getConfig(); Iterator localIterator = paramBeanDescription.getFactoryMethods().iterator(); while (localIterator.hasNext()) { AnnotatedMethod localAnnotatedMethod = (AnnotatedMethod)localIterator.next(); boolean bool = paramAnnotationIntrospector.hasCreatorAnnotation(localAnnotatedMethod); int i = localAnnotatedMethod.getParameterCount(); if (i == 0) { if (bool) paramCreatorCollector.setDefaultCreator(localAnnotatedMethod); } else if (i == 1) { AnnotatedParameter localAnnotatedParameter2 = localAnnotatedMethod.getParameter(0); PropertyName localPropertyName3; if (localAnnotatedParameter2 == null) localPropertyName3 = null; else localPropertyName3 = paramAnnotationIntrospector.findNameForDeserialization(localAnnotatedParameter2); PropertyName localPropertyName4 = localPropertyName3; String str2; if (localPropertyName3 == null) str2 = null; else str2 = localPropertyName4.getSimpleName(); if ((paramAnnotationIntrospector.findInjectableValueId(localAnnotatedParameter2) == null) && ((str2 == null) || (str2.length() == 0))) _handleSingleArgumentFactory(localDeserializationConfig, paramBeanDescription, paramVisibilityChecker, paramAnnotationIntrospector, paramCreatorCollector, localAnnotatedMethod, bool); else; } else if (paramAnnotationIntrospector.hasCreatorAnnotation(localAnnotatedMethod)) { Object localObject1 = null; CreatorProperty[] arrayOfCreatorProperty = new CreatorProperty[i]; int j = 0; int k = 0; for (int m = 0; m < i; m++) { AnnotatedParameter localAnnotatedParameter1 = localAnnotatedMethod.getParameter(m); PropertyName localPropertyName1; if (localAnnotatedParameter1 == null) localPropertyName1 = null; else localPropertyName1 = paramAnnotationIntrospector.findNameForDeserialization(localAnnotatedParameter1); PropertyName localPropertyName2 = localPropertyName1; String str1; if (localPropertyName1 == null) str1 = null; else str1 = localPropertyName2.getSimpleName(); Object localObject2 = paramAnnotationIntrospector.findInjectableValueId(localAnnotatedParameter1); if ((str1 != null) && (str1.length() > 0)) { j++; arrayOfCreatorProperty[m] = constructCreatorProperty(paramDeserializationContext, paramBeanDescription, str1, m, localAnnotatedParameter1, localObject2); } else if (localObject2 != null) { k++; arrayOfCreatorProperty[m] = constructCreatorProperty(paramDeserializationContext, paramBeanDescription, str1, m, localAnnotatedParameter1, localObject2); } else if (localObject1 == null) { localObject1 = localAnnotatedParameter1; } } if ((bool) || (j > 0) || (k > 0)) if (j + k == i) paramCreatorCollector.addPropertyCreator(localAnnotatedMethod, arrayOfCreatorProperty); else if ((j == 0) && (k + 1 == i)) paramCreatorCollector.addDelegatingCreator(localAnnotatedMethod, arrayOfCreatorProperty); else throw new IllegalArgumentException("Argument #" + localObject1.getIndex() + " of factory method " + localAnnotatedMethod + " has no property name annotation; must have name when multiple-paramater constructor annotated as Creator"); } } }
protected boolean _handleSingleArgumentConstructor(DeserializationContext paramDeserializationContext, BeanDescription paramBeanDescription, VisibilityChecker<?> paramVisibilityChecker, AnnotationIntrospector paramAnnotationIntrospector, CreatorCollector paramCreatorCollector, AnnotatedConstructor paramAnnotatedConstructor, boolean paramBoolean1, boolean paramBoolean2, String paramString) { AnnotatedParameter localAnnotatedParameter = paramAnnotatedConstructor.getParameter(0); if (paramString == null) { PropertyName localPropertyName1; if (localAnnotatedParameter == null) localPropertyName1 = null; else localPropertyName1 = paramAnnotationIntrospector.findNameForDeserialization(localAnnotatedParameter); PropertyName localPropertyName2 = localPropertyName1; if (localPropertyName1 == null) paramString = null; else paramString = localPropertyName2.getSimpleName(); } Object localObject = paramAnnotationIntrospector.findInjectableValueId(localAnnotatedParameter); if ((localObject != null) || ((paramString != null) && (paramString.length() > 0))) { CreatorProperty[] arrayOfCreatorProperty = new CreatorProperty[1]; arrayOfCreatorProperty[0] = constructCreatorProperty(paramDeserializationContext, paramBeanDescription, paramString, 0, localAnnotatedParameter, localObject); paramCreatorCollector.addPropertyCreator(paramAnnotatedConstructor, arrayOfCreatorProperty); return true; } Class localClass = paramAnnotatedConstructor.getRawParameterType(0); if (localClass == String.class) { if ((paramBoolean1) || (paramBoolean2)) paramCreatorCollector.addStringCreator(paramAnnotatedConstructor); return true; } if ((localClass == Integer.TYPE) || (localClass == Integer.class)) { if ((paramBoolean1) || (paramBoolean2)) paramCreatorCollector.addIntCreator(paramAnnotatedConstructor); return true; } if ((localClass == Long.TYPE) || (localClass == Long.class)) { if ((paramBoolean1) || (paramBoolean2)) paramCreatorCollector.addLongCreator(paramAnnotatedConstructor); return true; } if ((localClass == Double.TYPE) || (localClass == Double.class)) { if ((paramBoolean1) || (paramBoolean2)) paramCreatorCollector.addDoubleCreator(paramAnnotatedConstructor); return true; } if (paramBoolean1) { paramCreatorCollector.addDelegatingCreator(paramAnnotatedConstructor, null); return true; } return false; }
protected void _addDeserializerConstructors (DeserializationContext ctxt, BeanDescription beanDesc, VisibilityChecker<?> vchecker, AnnotationIntrospector intr, CreatorCollector creators) throws JsonMappingException { /* First things first: the "default constructor" (zero-arg * constructor; whether implicit or explicit) is NOT included * in list of constructors, so needs to be handled separately. */ AnnotatedConstructor defaultCtor = beanDesc.findDefaultConstructor(); if (defaultCtor != null) { if (!creators.hasDefaultCreator() || intr.hasCreatorAnnotation(defaultCtor)) { creators.setDefaultCreator(defaultCtor); } } for (AnnotatedConstructor ctor : beanDesc.getConstructors()) { int argCount = ctor.getParameterCount(); boolean isCreator = intr.hasCreatorAnnotation(ctor); boolean isVisible = vchecker.isCreatorVisible(ctor); // some single-arg constructors (String, number) are auto-detected if (argCount == 1) { _handleSingleArgumentConstructor(ctxt, beanDesc, vchecker, intr, creators, ctor, isCreator, isVisible); continue; } if (!isCreator && !isVisible) { continue; } // [JACKSON-541] improved handling a bit so: // 2 or more args; all params must have name annotations // ... or @JacksonInject (or equivalent) /* [JACKSON-711] One more possibility; can have 1 or more injectables, and * exactly one non-annotated parameter: if so, it's still delegating. */ AnnotatedParameter nonAnnotatedParam = null; int namedCount = 0; int injectCount = 0; CreatorProperty[] properties = new CreatorProperty[argCount]; for (int i = 0; i < argCount; ++i) { AnnotatedParameter param = ctor.getParameter(i); PropertyName pn = (param == null) ? null : intr.findNameForDeserialization(param); String name = (pn == null) ? null : pn.getSimpleName(); Object injectId = intr.findInjectableValueId(param); if (name != null && name.length() > 0) { ++namedCount; properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId); } else if (injectId != null) { ++injectCount; properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId); } else if (nonAnnotatedParam == null) { nonAnnotatedParam = param; } } // Ok: if named or injectable, we have more work to do if (isCreator || namedCount > 0 || injectCount > 0) { // simple case; everything covered: if ((namedCount + injectCount) == argCount) { creators.addPropertyCreator(ctor, properties); } else if ((namedCount == 0) && ((injectCount + 1) == argCount)) { // [712] secondary: all but one injectable, one un-annotated (un-named) creators.addDelegatingCreator(ctor, properties); } else { // otherwise, epic fail throw new IllegalArgumentException("Argument #"+nonAnnotatedParam.getIndex()+" of constructor "+ctor+" has no property name annotation; must have name when multiple-paramater constructor annotated as Creator"); } } } }
protected boolean _handleSingleArgumentConstructor(DeserializationContext ctxt, BeanDescription beanDesc, VisibilityChecker<?> vchecker, AnnotationIntrospector intr, CreatorCollector creators, AnnotatedConstructor ctor, boolean isCreator, boolean isVisible) throws JsonMappingException { // note: if we do have parameter name, it'll be "property constructor": AnnotatedParameter param = ctor.getParameter(0); PropertyName pn = (param == null) ? null : intr.findNameForDeserialization(param); String name = (pn == null) ? null : pn.getSimpleName(); Object injectId = intr.findInjectableValueId(param); if ((injectId != null) || (name != null && name.length() > 0)) { // property-based // We know there's a name and it's only 1 parameter. CreatorProperty[] properties = new CreatorProperty[1]; properties[0] = constructCreatorProperty(ctxt, beanDesc, name, 0, param, injectId); creators.addPropertyCreator(ctor, properties); return true; } // otherwise either 'simple' number, String, or general delegate: Class<?> type = ctor.getRawParameterType(0); if (type == String.class) { if (isCreator || isVisible) { creators.addStringCreator(ctor); } return true; } if (type == int.class || type == Integer.class) { if (isCreator || isVisible) { creators.addIntCreator(ctor); } return true; } if (type == long.class || type == Long.class) { if (isCreator || isVisible) { creators.addLongCreator(ctor); } return true; } if (type == double.class || type == Double.class) { if (isCreator || isVisible) { creators.addDoubleCreator(ctor); } return true; } // Delegating Creator ok iff it has @JsonCreator (etc) if (isCreator) { creators.addDelegatingCreator(ctor, null); return true; } return false; }
protected boolean _handleSingleArgumentFactory(DeserializationConfig config, BeanDescription beanDesc, VisibilityChecker<?> vchecker, AnnotationIntrospector intr, CreatorCollector creators, AnnotatedMethod factory, boolean isCreator) throws JsonMappingException { Class<?> type = factory.getRawParameterType(0); if (type == String.class) { if (isCreator || vchecker.isCreatorVisible(factory)) { creators.addStringCreator(factory); } return true; } if (type == int.class || type == Integer.class) { if (isCreator || vchecker.isCreatorVisible(factory)) { creators.addIntCreator(factory); } return true; } if (type == long.class || type == Long.class) { if (isCreator || vchecker.isCreatorVisible(factory)) { creators.addLongCreator(factory); } return true; } if (type == double.class || type == Double.class) { if (isCreator || vchecker.isCreatorVisible(factory)) { creators.addDoubleCreator(factory); } return true; } if (type == boolean.class || type == Boolean.class) { if (isCreator || vchecker.isCreatorVisible(factory)) { creators.addBooleanCreator(factory); } return true; } if (intr.hasCreatorAnnotation(factory)) { creators.addDelegatingCreator(factory, null); return true; } return false; }