list<T>批量替换实体中属性名并转换成json

JsonConfigEx:

package com.sys.common.util.json;

import com.sys.common.util.Assert;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class JsonConfigEx {
    private final Set<String> includes = new HashSet();
    private final Set<String> excludes = new HashSet();
    private final Map<String, Set<String>> propertyExcludesMap = new HashMap();
    public static final JsonConfigEx.AdditionalPropertiesHolder propertyExcludesHolder = new JsonConfigEx.AdditionalPropertiesHolder();
    public static final JsonConfigEx.AdditionalPropertiesHolder additionalPropertiesHolder = new JsonConfigEx.AdditionalPropertiesHolder();
    public static final JsonConfigEx.AdditionalPropertiesHolder replacedJsonKeyNamesHolder = new JsonConfigEx.AdditionalPropertiesHolder();
    private final Map<Class<? extends Enum<?>>, String> enumVPMap = new HashMap();
    private boolean isIgoreHibernateLazyLoadProperty;
    private boolean isIgoreCollectionProperty;
    private final Map<Class<?>, Map<String, String>> propNameReplaceMap = new HashMap();
    private PropertyIncludeNameGenerator propertyIncludeNameGenerator = new DefaultNameGenerator();
    private ColPropIncludesValueGenerator colPropIncludesValueGenerator = new DefaultValueGenerator();

    public JsonConfigEx() {
        String isIgoreHLLPFlag = System.getProperty("com.vrv.sys.utils.json.isIgoreHibernateLazyLoadProperty");
        if ("true".equalsIgnoreCase(isIgoreHLLPFlag)) {
            this.isIgoreHibernateLazyLoadProperty = true;
        }

        String isIgoreCPFlag = System.getProperty("com.vrv.sys.utils.json.isIgoreCollectionProperty");
        if ("true".equalsIgnoreCase(isIgoreCPFlag)) {
            this.isIgoreCollectionProperty = true;
        }

    }

    public void addExcludes(String... excludes) {
        Assert.notEmpty(excludes, "要排除的属性集不能为空!");
        this.excludes.addAll(Arrays.asList(excludes));
    }

    public void addIncludes(String... includes) {
        Assert.notEmpty(includes, "要包含的属性集不能为空!");
        this.includes.addAll(Arrays.asList(includes));
    }

    public void addPropertyIncludes(String propertyExpression, String... includes) {
        Assert.isNotBlank(propertyExpression, "属性表达式不能为空!");
        Assert.notEmpty(includes, "要包含的属性集不能为空!");
        Set<String> includesSet = new HashSet(Arrays.asList(includes));
        this.propertyExcludesMap.put(propertyExpression, includesSet);
    }

    public void addEnumValueProcessor(Class<? extends Enum<?>> enumClass, String propertyName) {
        Assert.notNull(enumClass, "要进行处理的枚举类型不能为null!");
        Assert.isNotBlank(propertyName, "要进行处理的枚举属性不能为空!");
        this.enumVPMap.put(enumClass, propertyName);
    }

    public void setIgoreHibernateLazyLoadProperty(boolean isIgore) {
        this.isIgoreHibernateLazyLoadProperty = isIgore;
    }

    public void setIgoreCollectionProperty(boolean isIgore) {
        this.isIgoreCollectionProperty = isIgore;
    }

    public void addPropertiesToJson(Map<String, Object> props) {
        Assert.notNull(props, "要添加的属性集不能为null!");
        ((Map)additionalPropertiesHolder.get()).putAll(props);
    }

    public void addPropertyToJson(String key, Object value) {
        Assert.notNull(key, "要添加的属性key不能为null!");
        Assert.notNull(value, "要添加的属性value不能为null!");
        ((Map)additionalPropertiesHolder.get()).put(key, value);
    }

    public void replacePropertyName(Class<?> targetClass, String propertyName, String newName) {
        Assert.notNull(targetClass, "目标类型不能为null!");
        Assert.notNull(propertyName, "要替换的属性名称不能为null!");
        Assert.notNull(newName, "属性新名称不能为null!");
        Map<String, String> propNameMap = new HashMap();
        propNameMap.put(propertyName, newName);
        this.propNameReplaceMap.put(targetClass, propNameMap);
    }

    public void replaceJsonKeyName(String keyName, String newName) {
        ((Map)replacedJsonKeyNamesHolder.get()).put(keyName, newName);
    }

    public PropertyIncludeNameGenerator getPropertyIncludeNameGenerator() {
        return this.propertyIncludeNameGenerator;
    }

    public void setPropertyIncludeNameGenerator(PropertyIncludeNameGenerator propertyIncludeNameGenerator) {
        Assert.notNull(propertyIncludeNameGenerator, "属性名称生成器不能为null!");
        this.propertyIncludeNameGenerator = propertyIncludeNameGenerator;
    }

    public ColPropIncludesValueGenerator getColPropIncludesValueGenerator() {
        return this.colPropIncludesValueGenerator;
    }

    public void setColPropIncludesValueGenerator(ColPropIncludesValueGenerator colPropIncludesValueGenerator) {
        Assert.notNull(colPropIncludesValueGenerator, "集合属性值生成器不能为null!");
        this.colPropIncludesValueGenerator = colPropIncludesValueGenerator;
    }

    public Set<String> getExcludes() {
        return this.excludes;
    }

    public Set<String> getIncludes() {
        return this.includes;
    }

    public Map<Class<? extends Enum<?>>, String> getEnumVPMap() {
        return this.enumVPMap;
    }

    public boolean isIgoreHibernateLazyLoadProperty() {
        return this.isIgoreHibernateLazyLoadProperty;
    }

    public boolean isIgoreCollectionProperty() {
        return this.isIgoreCollectionProperty;
    }

    public Map<String, Set<String>> getPropertyExcludesMap() {
        return this.propertyExcludesMap;
    }

    public Map<Class<?>, Map<String, String>> getPropNameReplaceMap() {
        return this.propNameReplaceMap;
    }

    static class AdditionalPropertiesHolder extends ThreadLocal<Map<String, Object>> {
        AdditionalPropertiesHolder() {
        }

        protected Map<String, Object> initialValue() {
            return new HashMap();
        }
    }
}
JsonUtils:
package com.sys.common.util.json;

import com.sys.common.util.Assert;
import com.sys.common.util.CollectionUtils;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter;

public class JsonUtils {
    private static final MorpherRegistry MORPHER_REGISTRY = JSONUtils.getMorpherRegistry();
    private static boolean isHibernateExist;
    private static final JsonConfigEx EMPTY_JSONCONFIGEX;

    static {
        MORPHER_REGISTRY.registerMorpher(new DateMorpher());
        MORPHER_REGISTRY.registerMorpher(new SqlDateMorpher());
        MORPHER_REGISTRY.registerMorpher(new SqlTimeMorpher());
        MORPHER_REGISTRY.registerMorpher(new TimestampMorpher());
        MORPHER_REGISTRY.registerMorpher(new CalendarMorpher());

        try {
            Class.forName("org.hibernate.Session");
            isHibernateExist = true;
        } catch (ClassNotFoundException var1) {
            isHibernateExist = false;
        }

        EMPTY_JSONCONFIGEX = new JsonConfigEx();
    }

    public static String object2Json(Object object) {
        return object2Json(object, new JsonConfig());
    }

    public static String object2Json(Object object, JsonConfigEx jsonConfigEx) {
        return object2Json(object, new JsonConfig(), jsonConfigEx);
    }

    public static String object2Json(Object object, String... excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        return object2Json(object, jsonConfig);
    }

    public static String object2Json(Object object, JsonConfigEx jsonConfigEx, String... excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        return object2Json(object, jsonConfig, jsonConfigEx);
    }

    public static String object2Json(Object object, JsonConfig jsonConfig) {
        jsonConfig = prepareJsonConfig(jsonConfig, EMPTY_JSONCONFIGEX);
        JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
        return jsonObject.toString();
    }

    public static String object2Json(Object object, JsonConfig jsonConfig, JsonConfigEx jsonConfigEx) {
        jsonConfig = prepareJsonConfig(jsonConfig, jsonConfigEx);
        JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
        jsonObject.putAll((Map)JsonConfigEx.additionalPropertiesHolder.get());
        jsonObject.putAll((Map)JsonConfigEx.propertyExcludesHolder.get());
        ((Map)JsonConfigEx.propertyExcludesHolder.get()).clear();
        Map<String, Object> jkrepMap = (Map)JsonConfigEx.replacedJsonKeyNamesHolder.get();
        if (!jkrepMap.isEmpty()) {
            Iterator var6 = jkrepMap.keySet().iterator();

            while(var6.hasNext()) {
                String key = (String)var6.next();
                Object jValue = jsonObject.get(key);
                jsonObject.remove(key);
                jsonObject.put(jkrepMap.get(key), jValue);
            }
        }

        clearStatus();
        return jsonObject.toString();
    }

    public static String collection2Json(Collection<?> collection, String... excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        return collection2Json(collection, jsonConfig);
    }

    public static String collection2Json(Collection<?> collection, JsonConfigEx jsonConfigEx, String... excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        return collection2Json(collection, jsonConfig, jsonConfigEx);
    }

    public static String collection2Json(Collection<?> collection) {
        return collection2Json(collection, new JsonConfig());
    }

    public static String collection2Json(Collection<?> collection, JsonConfigEx jsonConfigEx) {
        return collection2Json(collection, new JsonConfig(), jsonConfigEx);
    }

    public static String collection2Json(Collection<?> collection, JsonConfig jsonConfig) {
        if (collection == null) {
            collection = Collections.EMPTY_LIST;
        }

        jsonConfig = prepareJsonConfig(jsonConfig, EMPTY_JSONCONFIGEX);
        JSONArray jsonArray = JSONArray.fromObject(collection, jsonConfig);
        return jsonArray.toString();
    }

    public static String collection2Json(Collection<?> collection, JsonConfig jsonConfig, JsonConfigEx jsonConfigEx) {
        if (collection == null) {
            collection = Collections.EMPTY_LIST;
        }

        jsonConfig = prepareJsonConfig(jsonConfig, jsonConfigEx);
        JSONArray jsonArray = JSONArray.fromObject(collection, jsonConfig);
        clearStatus();
        return jsonArray.toString();
    }

    public static <T> T json2Object(String json, Class<T> objClass) {
        Assert.hasLength(json, "Json字符串不能为空!");
        JSONObject jsonObject = JSONObject.fromObject(json);
        return JSONObject.toBean(jsonObject, objClass);
    }

    public static <T> T json2Object(String json, Class<T> objClass, Map classMap) {
        Assert.hasLength(json, "Json字符串不能为空!");
        JSONObject jsonObject = JSONObject.fromObject(json);
        return JSONObject.toBean(jsonObject, objClass, classMap);
    }

    public static Object json2Object(String json) {
        Assert.hasLength(json, "Json字符串不能为空!");
        JSONObject jsonObject = JSONObject.fromObject(json);
        return JSONObject.toBean(jsonObject);
    }

    public static synchronized void registerMorpher(Morpher morpher, boolean override) {
        Assert.notNull(morpher, "要注册的转换器不能为空!");
        MORPHER_REGISTRY.registerMorpher(morpher, override);
    }

    public static synchronized void registerMorpher(Morpher morpher) {
        registerMorpher(morpher, true);
    }

    public static String toJson(String key, Object value) {
        Map<String, Object> map = new HashMap();
        map.put(key, value);
        return toJson(map);
    }

    public static String toJson(Map<?, ?> map) {
        Assert.notNull(map, "要转化为Json格式的map不能为null!");
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(map, prepareJsonConfig((JsonConfig)null, EMPTY_JSONCONFIGEX));
        return jsonObject.toString();
    }

    public static String toJson(String[] keys, String... values) {
        Assert.notNull(keys, "键数组不能为null!");
        Assert.notNull(values, "值数组不能为null!");
        int len = Math.min(keys.length, values.length);
        JSONObject jsonObject = new JSONObject();

        for(int i = 0; i < len; ++i) {
            jsonObject.put(keys[i], values[i]);
        }

        return jsonObject.toString();
    }

    private static void clearStatus() {
        ((Map)JsonConfigEx.additionalPropertiesHolder.get()).clear();
        ((Map)JsonConfigEx.replacedJsonKeyNamesHolder.get()).clear();
    }

    private static JsonConfig prepareJsonConfig(JsonConfig jsonConfig, JsonConfigEx jsonConfigEx) {
        Assert.notNull(jsonConfigEx, "Json扩展配置不能为null!");
        if (jsonConfig == null) {
            jsonConfig = new JsonConfig();
        }

        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.NOPROP);
        CombinedPropertyFilter combinedPropertyFilter = new CombinedPropertyFilter();
        PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
        if (jsonPropertyFilter != null) {
            combinedPropertyFilter.addPropertyFilter(jsonPropertyFilter);
        }

        Set<String> excludes = jsonConfigEx.getExcludes();
        if (excludes != null && excludes.size() > 0) {
            jsonConfig.setExcludes((String[])excludes.toArray(new String[0]));
        }

        Set<String> includes = jsonConfigEx.getIncludes();
        if (includes != null && includes.size() > 0) {
            combinedPropertyFilter.addPropertyFilter(new IncludePropertyFilter(includes));
        }

        if (isHibernateExist) {
            if (jsonConfigEx.isIgoreHibernateLazyLoadProperty()) {
                combinedPropertyFilter.addPropertyFilter(new HibernateProxyPropertyFilter());
            } else {
                combinedPropertyFilter.addPropertyFilter(new HibernateProxyLazyInitHandlerFilter());
            }
        }

        if (!CollectionUtils.isMapEmpty(jsonConfigEx.getPropertyExcludesMap())) {
            combinedPropertyFilter.addPropertyFilter(new PropertyIncludesFilter(jsonConfigEx));
        }

        jsonConfig.addJsonEventListener(new PropertyIncludesJsonEventListener());
        jsonConfig.enableEventTriggering();
        if (jsonConfigEx.isIgoreCollectionProperty()) {
            combinedPropertyFilter.addPropertyFilter(new SkipCollectionPropertyFilter());
        }

        jsonConfig.setJsonPropertyFilter(combinedPropertyFilter);
        Map<Class<? extends Enum<?>>, String> enumVPMap = jsonConfigEx.getEnumVPMap();
        Class targetClass;
        Iterator var9;
        if (enumVPMap != null && enumVPMap.size() > 0) {
            Set<Class<? extends Enum<?>>> keySet = enumVPMap.keySet();
            var9 = keySet.iterator();

            while(var9.hasNext()) {
                targetClass = (Class)var9.next();
                if (jsonConfig.findJsonValueProcessor(targetClass) == null) {
                    EnumJsonValueProcessor enumJVProcessor = new EnumJsonValueProcessor(targetClass, (String)enumVPMap.get(targetClass));
                    jsonConfig.registerJsonValueProcessor(targetClass, enumJVProcessor);
                }
            }
        }

        Map<Class<?>, Map<String, String>> propNameReplaceMap = jsonConfigEx.getPropNameReplaceMap();
        if (!propNameReplaceMap.isEmpty()) {
            var9 = propNameReplaceMap.keySet().iterator();

            while(var9.hasNext()) {
                targetClass = (Class)var9.next();
                Map<String, String> nrMap = (Map)propNameReplaceMap.get(targetClass);
                Iterator var12 = nrMap.keySet().iterator();

                while(var12.hasNext()) {
                    String propName = (String)var12.next();
                    ReplacePropNameProcessor replacePropNameProcessor = new ReplacePropNameProcessor(propName, (String)nrMap.get(propName));
                    jsonConfig.registerJsonPropertyNameProcessor(targetClass, replacePropNameProcessor);
                }
            }
        }

        DateJsonValueProcessor dateJsonValueProcessor = new DateJsonValueProcessor();
        if (jsonConfig.findJsonValueProcessor(Date.class) == null) {
            jsonConfig.registerJsonValueProcessor(Date.class, dateJsonValueProcessor);
        }

        if (jsonConfig.findJsonValueProcessor(java.sql.Date.class) == null) {
            jsonConfig.registerJsonValueProcessor(java.sql.Date.class, dateJsonValueProcessor);
        }

        if (jsonConfig.findJsonValueProcessor(Time.class) == null) {
            jsonConfig.registerJsonValueProcessor(Time.class, dateJsonValueProcessor);
        }

        if (jsonConfig.findJsonValueProcessor(Timestamp.class) == null) {
            jsonConfig.registerJsonValueProcessor(Timestamp.class, dateJsonValueProcessor);
        }

        if (jsonConfig.findJsonValueProcessor(Calendar.class) == null) {
            jsonConfig.registerJsonValueProcessor(Calendar.class, dateJsonValueProcessor);
        }

        return jsonConfig;
    }

    private JsonUtils() {
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值