Skip to content

Instantly share code, notes, and snippets.

@codersidprogrammer
Created December 29, 2023 06:45
Show Gist options
  • Save codersidprogrammer/1bb76555e13dcf5ec02ce93ebe04e3bc to your computer and use it in GitHub Desktop.
Save codersidprogrammer/1bb76555e13dcf5ec02ce93ebe04e3bc to your computer and use it in GitHub Desktop.
Convert plain object into POJO
package com.gmf.tms.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class ObjectMapperUtil {
@Autowired
private static final ModelMapper modelMapper;
@Autowired
private static final ObjectMapper objectMapper;
/**
* Model mapper property setting are specified in the following block.
* Default property matching strategy is set to Strict see
* {@link MatchingStrategies}
* Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
*/
static {
modelMapper = new ModelMapper();
objectMapper = new ObjectMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
/**
* Hide from public usage.
*/
private ObjectMapperUtil() {
}
/**
* <p>
* Note: outClass object must have default constructor with no arguments
* </p>
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
/**
* <p>
* Note: outClass object must have default constructor with no arguments
* </p>
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}
/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
public static Object stringToObject(String json) throws JsonProcessingException {
Object obj = objectMapper.readValue(json, Object.class);
return obj;
}
public static<D> String jsonToString(D dto) throws JsonProcessingException {
return objectMapper.writeValueAsString(dto);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment