
com.sap.cloud.sdk.service.prov.api.util.PojoUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api Show documentation
Show all versions of api Show documentation
SAP Cloud Platform SDK for service development
The newest version!
package com.sap.cloud.sdk.service.prov.api.util;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.beanutils.BeanMap;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sap.cloud.sdk.service.prov.api.annotations.Key;
public class PojoUtil {
private static ObjectMapper getMapper( ){
ObjectMapper mapper = new ObjectMapper();
/*-- Ignore Null Values --*/
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
/*-- do not fail on empty beans --*/
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
/*-- First: hide all elements in the class ; make property,getter,setter hidden --*/
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
/*-- Second: now only make the properties discoverable --*/
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
/*-- To ensure that during POJO to Map conversion, the null valued properties are not ignored. --*/
mapper.setSerializationInclusion(Include.ALWAYS);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper ;
}
public static Map getMapFromPojo(Object pojo) {
return (Map) (getMapper().convertValue(pojo, HashMap.class));
}
public static T getPojoFromMap(Class clazz, Map data) {
ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(data, clazz);
}
public static List getKeyProperties(T p) {
Field[] ss = p.getClass().getDeclaredFields();
return Arrays
.asList(ss)
.stream()
.filter(propertyElement -> propertyElement
.getDeclaredAnnotation(Key.class) != null)
.map(propertyElement -> propertyElement.getName())
.collect(Collectors.toList());
}
public static Map getMapUsingBeanUtils(Object pojo) {
if(pojo instanceof Map)
return (Map) pojo;
List pojoFields = Arrays.asList(pojo.getClass().getDeclaredFields()).stream().map(f->f.getName()).collect(Collectors.toList());
Map pojoAsMap = new HashMap();
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy