
Untitled
By: a guest on
Jun 11th, 2012 | syntax:
None | size: 1.98 KB | hits: 9 | expires: Never
How can I map List<MyObject> to List<HashMap<String,Object>> with Dozer?
public class Foo{
String fooStr;
List<Bar> barList;
//get/set barList/someStr go here
}
public class Bar {
String barStr;
}
<mapping>
<class-a>Foo</class-a>
<class-b>java.util.Map</class-b>
<field>
<a>bar</a>
<b key="bar">this</b>
<a-hint>Bar</a-hint>
<b-hint>java.util.Map</b-hint>
</field>
</mapping>
{
"fooStr" : "value of fooStr",
{
"barStr" : "value of barStr"
}
}
{
"fooStr" : "value of fooStr",
"barList" : [{ "barStr" : "bar1" }, { "barStr" : "bar2" }, ...]
}
public class Foo {
String fooStr;
List<Bar> barList;
...
}
public class Bar {
String barStr;
}
public class Target {
String fooStr;
Map<String, Bar> barMap;
}
public class TestCustomConverter implements CustomConverter {
public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
if (source == null) {
return null;
}
if (source instanceof Foo) {
Map<Bar> dest = null;
// check to see if the object already exists
if (destination == null) {
dest = new Target();
} else {
dest = (Target) destination;
}
((Target) dest).setFooStr(source.getFooStr());
for(Bar : source.getBarList()) {
((Target) dest).getBarMap().put(bar.getBarStr(), bar);
}
return dest;
} else if (source instanceof Target) {
Foo dest = null;
// check to see if the object already exists
if (destination == null) {
dest = new Foo ();
} else {
dest = (Foo) destination;
}
dest.getFoos().addAll(((Target)source).getBarMap().values());
dest.setFooStr(((Target)source).getFooStr()):
return dest;
} else {
throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
+ destination + " and " + source);
}
}