JsonUtils

https://blog.csdn.net/shaoduo/article/details/70214487?utm_medium=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.nonecase

import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
 

public final class JsonUtils {
    private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);
    
    private static final ObjectMapper mapper = new ObjectMapper();
    private static final JavaType map_str;
    static {
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        map_str = mapper.getTypeFactory().constructParametricType(Map.class, String.class, String.class);
    }
    
    final static public String toJson0(final Object obj) throws JsonProcessingException {
        if(obj==null) {
            return null;
        }
        return mapper.writeValueAsString(obj);
    }
    
    final static public Map<String, String> toMap0(final String json) throws IOException{
        if(StringUtils.isBlank(json)) {
            return null;
        }
        return mapper.readValue(json, map_str);
    }
    
    final static public String toJson(final Object obj) {
        try {
            return toJson0(obj);
        } catch (JsonProcessingException e) {
            LOG.error("Json Error toJson obj="+obj, e);
        }
        return null;
    }
    
    final static public Map<String, String> toMap(final String json){
        try {
            return toMap0(json);
        } catch (IOException e) {
            LOG.error("Json Error toMap json="+json, e);
        }
        return null;
    }
    
    public final static boolean isNoneValue(final String cacheValue) {
        return cacheValue.startsWith(SFCons.Cache_Not_DB);
    }
    
    public final static String mkNoneValue(String suffix) {
        return SFCons.Cache_Not_DB + SFCons.Cache_Not_Divide + suffix;
    }
    
    public final static Permission toPm0(final String json) throws IOException {
        if(StringUtils.isBlank(json)) {
            return null;
        }
        return mapper.readValue(json, Permission.class);
    }
    
    public final static Permission toPm(final String json) {
        try {
            return toPm0(json);
        } catch (IOException e) {
            LOG.error("Json Error toPm json="+json, e);
        }
        return null;
    }
    
    public final static CreditUser toCreditUser0(final String json) throws IOException {
        if(StringUtils.isBlank(json)) {
            return null;
        }
        return mapper.readValue(json, CreditUser.class);
    }
    
    public final static CreditUser toCreditUser(final String json) {
        try {
            return toCreditUser0(json);
        } catch (IOException e) {
            LOG.error("Json Error CreditUser json="+json, e);
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值