mongodb java - Manual Conversion of 3rd Party Class With Morphia -


long story short: possible write type converter 3rd party library class morphia?

long story: i'm new morphia. have entity class contains field typed javax.activation.mimetype. when try save instances of class, morphia complains "can't serialize class javax.activation.mimetype". tried writing typeconverter , adding list of converters didn't work. here code pieces:

entity.class

@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};     } } 

test case

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 error here 

i want mimetype class serialize in "foo/bar" style, , deserialize it. possible?

this works me using 0.107 code. i'm using modified converter might have contributed success. converter i'm using below 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); } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -