mongodb - Morphia converter calling other converters -
i want convert optional<bigdecimal> in morphia. created bigdecimalconverter, , works fine. want create optionalconverter.
optional can hold object type. in optionalconverter.encode method can extract underlying object, , i'd pass default mongo conversion. if there string, i'll string, if there 1 of entities, i'll encoded entity. how can it?
there 2 questions:
1. how call other converters?
2. how create converter generic class type parameters not statically known?
the first 1 possible creating mappingmongoconveter , custom converter together:
@configuration public class customconfig extends abstractmongoconfiguration { @override protected string getdatabasename() { // ... } @override @bean public mongo mongo() throws exception { // ... } @override @bean public mappingmongoconverter mappingmongoconverter() throws exception { mappingmongoconverter mmc = new mappingmongoconverter( mongodbfactory(), mongomappingcontext()); mmc.setcustomconversions(new customconversions(customconverters .create(mmc))); return mmc; } } public class fooconverter implements converter<foo, dbobject> { private mappingmongoconverter mmc; public fooconverter(mappingmongoconverter mmc) { this.mmc = mmc; } public dbobject convert(foo foo) { // ... } } public class customconverters { public static list<?> create(mappingmongoconverter mmc) { list<?> list = new arraylist<>(); list.add(new fooconverter(mmc)); return list; } } the second 1 more difficult due type erasure. i've tried create converter scala's map haven't found way. unable exact type information source map when writing, or target map when reading.
for simple cases, e.g. if don't need handle possible parameter types, , there no ambiguity while reading, may possible though.
Comments
Post a Comment