使用Morphia手动转换第三方类别 [英] Manual Conversion of 3rd Party Class With Morphia
问题描述
长话短说:是否可以使用Morphia为第三方库类编写类型转换器?
Long story short: Is it possible to write a type converter for a 3rd party library class with Morphia?
长话短说:我是Morphia的新手.我有一个实体类,其中包含一个键入为 javax.activation.MimeType 的字段.当我尝试保存类的实例时,Morphia抱怨它无法序列化类javax.activation.MimeType".我尝试编写TypeConverter并将其添加到转换器列表中,但是没有用. 以下是代码段:
Long story: I'm new to Morphia. I have an entity class which contains a field typed javax.activation.MimeType. When I try to save instances of my class, Morphia complains it "can't serialize class javax.activation.MimeType". I tried writing a TypeConverter and adding it to the list of converters but it didn't work. Here are the code pieces:
@Entity
@Converters(MimeTypeConverter.class)
public class Entity {
@Id ObjectId id;
String name;
javax.activation.MimeType mimeType;
}
MimeTypeConverter.class
public class MimeTypeConverter extends TypeConverter {
@Override
public Object decode(Class targetClass,
Object fromDBObject,
MappedField optionalExtraInfo) {
MimeType mimetype;
BasicDBObject dbObject = (BasicDBObject) fromDBObject;
String mimeString = dbObject.getString("mimeType");
try{
mimetype = new MimeType(mimeString);
} catch(MimeTypeParseException ex){
mimetype = new MimeType();
}
return mimetype;
}
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
MimeType mimetype = (MimeType) value;
return mimetype.getBaseType();
}
@Override
public Class[] getSupportTypes() {
return new Class[]{MimeType.class};
}
}
测试用例
Morphia morphia = new Morphia().map(Entity.class);
morphia.getMapper().getConverters().addConverter(new MimeTypeConverter());
Datastore ds = morphia.createDatastore(new MongoClient(), "test"); //UnknownHostException
Entity entity = new Entity();
entity.name = "test name";
entity.mimeType = new MimeType("text/plain"); //MimeTypeParseException
ds.save(entity); // FAILS WITH ERROR HERE
我希望MimeType类以"foo/bar"样式进行序列化,并从中反序列化.有可能吗?
I want MimeType class to serialize in "foo/bar" style, and deserialize from it. Is that possible?
推荐答案
这实际上对我来说使用0.107代码有效.我正在使用经过稍微修改的转换器,这可能对我的成功有所帮助.我正在使用的转换器也在测试之列.
This actually works for me using 0.107 code. I'm using a slightly modified converter which might have contributed to my success. The converter i'm using is below as well as the test.
@Entity
@Converters(MimeTypeConverter.class)
public static class MimeTyped {
@Id
private ObjectId id;
private String name;
private javax.activation.MimeType mimeType;
}
public class MimeTypeConverter extends TypeConverter {
public MimeTypeConverter() {
super(MimeType.class);
}
@Override
public Object decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo) {
try {
return new MimeType(((BasicDBObject) fromDBObject).getString("mimeType"));
} catch (MimeTypeParseException ex) {
return new MimeType();
}
}
@Override
public Object encode(final Object value, final MappedField optionalExtraInfo) {
return ((MimeType) value).getBaseType();
}
}
@Test
public void mimeType() throws UnknownHostException, MimeTypeParseException {
getMorphia().getMapper().getConverters().addConverter(new MimeTypeConverter());
MimeTyped entity = new MimeTyped();
entity.name = "test name";
entity.mimeType = new MimeType("text/plain"); //MimeTypeParseException
final DBObject dbObject = getMorphia().toDBObject(entity);
assertEquals("text/plain", dbObject.get("mimeType"));
getDs().save(entity);
}
这篇关于使用Morphia手动转换第三方类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!