Any Tool For Java Object to Object Mapping

any tool for java object to object mapping?

You could try Dozer.

Dozer is a Java Bean to Java Bean
mapper that recursively copies data
from one object to another. Typically,
these Java Beans will be of different
complex types.

Dozer supports simple property
mapping, complex type mapping,
bi-directional mapping,
implicit-explicit mapping, as well as
recursive mapping. This includes
mapping collection attributes that
also need mapping at the element
level.

How to Map specific fields of an object to another object?

You can use ModelMapper. This library is used to perform this entity-DTO conversion.

  1. Add below maven dependancy :
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>

  1. Autowire the ModelMapper -
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}

  1. Sample Code transforming DTO to entity :
private Employee convertToDto(EmployeeDto employeeDto) {
Employee employee = modelMapper.map(employeeDto, Employee.class);
return employee;
}

Object in Object to HashMap with reflection

You need to decide if a value should me put plain in the map or recursively. Therefore you can use a simple method like this:

private boolean isSimpleType(Class<?> type) {
return type.isPrimitive() ||
Boolean.class == type ||
Character.class == type ||
CharSequence.class.isAssignableFrom(type) ||
Number.class.isAssignableFrom(type) ||
Enum.class.isAssignableFrom(type);
}

This method returns true for all primitive types or the Wrapper objects, Strings and Enums. You can simply adjust that methods to fit your needs. Look here for more details on how to determine if a class is simple.

Now you can use to transform an object:

public Map<String, Object> convert(Object object) {
Map<String, Object> map = new HashMap<>();
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true);
try {
if (isSimpleType(field.getType())) {
map.put(field.getName(), field.get(object));
} else {
map.put(field.getName(), convert(field.get(object)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}

You also can adjust this to do fir your export method.

The result for your example will be this:

{a=2, b=5, bar={q=10, e=it works, w=15}, c=abc}

Beside that I would strongly recommend using an library (like Jackson), which already does stuff like this by perfection. Here is the same solution using Jackson:

Map result = new ObjectMapper().convertValue(object, Map.class);

Or if you need type safety:

ObjectMapper mapper = new ObjectMapper();
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
Map<String, Object> result = mapper.convertValue(value, mapType);

Simple object-to-object mapper without xml?

I'd prefer to do this in your Java code if possible. I'm not sure why there's a benefit to having some declaration-based solution when a code-based solution is more likely easier to read and more extensible.

If you need a framework to do this, perhaps Dozer is of use. It provides a means of identifying mappings using annotations (as well as XML)

How to convert a Java object (bean) to key-value pairs (and vice versa)?

There is always apache commons beanutils but of course it uses reflection under the hood



Related Topics



Leave a reply



Submit