ImmutableList.Builder<JDeserializerType> parametersDeserializerBuilder = ImmutableList.builder();
String[] params = new String[typeArgs.length];
for ( int i = 0; i < params.length; i++ ) {
JDeserializerType parameterDeserializerType;
if ( MapperType.KEY_DESERIALIZER == configuredDeserializer.get().getParameters()[i] ) {
parameterDeserializerType = getKeyDeserializerFromType( typeArgs[i] );
} else {
parameterDeserializerType = getJsonDeserializerFromType( typeArgs[i], subtype );
}
parametersDeserializerBuilder.add( parameterDeserializerType );
params[i] = parameterDeserializerType.getInstance();
}
builder.parameters( parametersDeserializerBuilder.build() );
builder.instance( String.format( configuredDeserializer.get().getInstanceCreation(), params ) );
} else {
builder.instance( configuredDeserializer.get().getInstanceCreation() );
}
return builder.build();
}
JEnumType enumType = type.isEnum();
if ( null != enumType ) {
return builder.instance( EnumJsonDeserializer.class.getCanonicalName() + ".newInstance(" + enumType
.getQualifiedSourceName() + ".class)" ).build();
}
if ( Enum.class.getName().equals( type.getQualifiedSourceName() ) ) {
String message = "Type java.lang.Enum is not supported by deserialization";
logger.log( TreeLogger.Type.WARN, message );
throw new UnsupportedTypeException( message );
}
JArrayType arrayType = type.isArray();
if ( null != arrayType ) {
String method = "%s.newInstance(%s, %s)";
String arrayCreator;
String arrayDeserializer;
JType leafType = arrayType.getLeafType();
if ( arrayType.getRank() == 1 ) {
// one dimension array
arrayCreator = "new " + ArrayCreator.class.getCanonicalName() + "<" + leafType
.getParameterizedQualifiedSourceName() + ">(){\n" +
" @Override\n" +
" public " + arrayType.getParameterizedQualifiedSourceName() + " create( int length ) {\n" +
" return new " + leafType.getQualifiedSourceName() + "[length];\n" +
" }\n" +
"}";
arrayDeserializer = ArrayJsonDeserializer.class.getCanonicalName();
} else if ( arrayType.getRank() == 2 ) {
// 2 dimensions array
arrayCreator = "new " + Array2dCreator.class.getCanonicalName() + "<" + leafType
.getParameterizedQualifiedSourceName() + ">(){\n" +
" @Override\n" +
" public " + arrayType.getParameterizedQualifiedSourceName() + " create( int first, int second ) {\n" +
" return new " + leafType.getQualifiedSourceName() + "[first][second];\n" +
" }\n" +
"}";
arrayDeserializer = Array2dJsonDeserializer.class.getCanonicalName();
} else {
// more dimensions are not supported
String message = "Arrays with 3 or more dimensions are not supported";
logger.log( TreeLogger.Type.WARN, message );
throw new UnsupportedTypeException( message );
}
JDeserializerType parameterDeserializerType = getJsonDeserializerFromType( leafType, subtype );
builder.parameters( ImmutableList.of( parameterDeserializerType ) );
return builder.instance( String.format( method, arrayDeserializer, parameterDeserializerType.getInstance(), arrayCreator ) )
.build();
}
if ( null != type.isAnnotation() ) {
String message = "Annotations are not supported";
logger.log( TreeLogger.Type.WARN, message );
throw new UnsupportedTypeException( message );
}
JClassType classType = type.isClassOrInterface();
if ( null != classType ) {
// it's a bean
JClassType baseClassType = classType;
JParameterizedType parameterizedType = classType.isParameterized();
if ( null != parameterizedType ) {
// it's a bean with generics, we create a deserializer based on generic type
baseClassType = parameterizedType.getBaseType();
}
BeanJsonDeserializerCreator beanJsonDeserializerCreator = new BeanJsonDeserializerCreator( logger
.branch( Type.DEBUG, "Creating deserializer for " + baseClassType
.getQualifiedSourceName() ), context, configuration, typeOracle );
String qualifiedClassName = beanJsonDeserializerCreator.create( baseClassType );
ImmutableList<? extends JType> typeParameters = getTypeParameters( classType, subtype );
StringBuilder joinedTypeParameterDeserializers = new StringBuilder();
if ( !typeParameters.isEmpty() ) {
ImmutableList.Builder<JDeserializerType> parametersDeserializerBuilder = ImmutableList.builder();
boolean first = true;
for ( JType argType : typeParameters ) {
if ( first ) {
first = false;
} else {
joinedTypeParameterDeserializers.append( ", " );
}
JDeserializerType parameterDeserializerType = getJsonDeserializerFromType( argType, subtype );
parametersDeserializerBuilder.add( parameterDeserializerType );
joinedTypeParameterDeserializers.append( parameterDeserializerType.getInstance() );
}
builder.parameters( parametersDeserializerBuilder.build() );
}
builder.beanMapper( true );