jmapper-core icon indicating copy to clipboard operation
jmapper-core copied to clipboard

Mapping to Map

Open khirschmann opened this issue 8 years ago • 2 comments

When mapping multiple fields to a map AND one of those fields is null all members listed before this field in class are lost (map is set to null).

`public class Source {

@JMap("map")
private String id;
@JMap("map")
private Integer quantity;
@JMap("map")
private Date purchase;


public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public Integer getQuantity() {
	return quantity;
}
public void setQuantity(Integer quantity) {
	this.quantity = quantity;
}
public Date getPurchase() {
	return purchase;
}
public void setPurchase(Date purchase) {
	this.purchase = purchase;
}


@JMapConversion(from = { "id", "quantity", "purchase" }, to = "map", type = Type.DYNAMIC)
public static String toAttributeConversion() {
	return " ${destination} = (${destination} != null ? ${destination} : new java.util.HashMap());"			
			+ " System.out.println(\"Converting: \" + \"${source.name}\"); "				
			+ " ${destination}.put(\"${source.name}\", ${source}); "
			+ " return ${destination}; ";
}

}

public class Destination {

private HashMap<String, Object> map;

public HashMap<String, Object> getMap() {
	return map;
}

public void setMap(HashMap<String, Object> map) {
	this.map = map;
}

}

public class Main {

public static void main(String[] args) {
	Source s = new Source();
	s.setId("Id");
	s.setPurchase(new Date());
	
	JMapper<Destination, Source> mapper = new JMapper<Destination, Source>(Destination.class, Source.class);
	Destination d = mapper.getDestination(s);
	System.out.println(d.getMap().toString());

}

} ` Output: Converting: id Converting: purchase {purchase=Thu Oct 12 14:04:53 CEST 2017}

Perhaps that is the wrong way to do it?

Thx for your help. Kevin

khirschmann avatar Oct 12 '17 12:10 khirschmann

Hi @khirschmann,

I need to do a test, let you know as soon as possible

avurro avatar Oct 12 '17 13:10 avurro

Hi @khirschmann,

I need to do a test, let you know as soon as possible

Have you done this test?

mdowsian avatar Dec 07 '23 18:12 mdowsian