org.mongodb.morphia.converters.StringConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of morphia Show documentation
Show all versions of morphia Show documentation
Java Object Document Mapper for MongoDB
The newest version!
package org.mongodb.morphia.converters;
import org.mongodb.morphia.mapping.MappedField;
import org.mongodb.morphia.utils.ReflectionUtils;
import java.util.List;
/**
* @author Uwe Schaefer, ([email protected])
* @author scotthernandez
*/
public class StringConverter extends TypeConverter implements SimpleValueConverter {
/**
* Creates the Converter.
*/
public StringConverter() {
super(String.class, String[].class);
}
@Override
public Object decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo) {
if (fromDBObject == null) {
return null;
}
if (targetClass.equals(fromDBObject.getClass())) {
return fromDBObject;
}
if (fromDBObject instanceof List) {
final Class type = targetClass.isArray() ? targetClass.getComponentType() : targetClass;
return ReflectionUtils.convertToArray(type, (List) fromDBObject);
}
if (targetClass.equals(String[].class)) {
return new String[]{fromDBObject.toString()};
}
return fromDBObject.toString();
}
}