123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package com.zx.dataservice.utils.redis;
- import org.apache.commons.lang3.StringUtils;
- import org.codehaus.jackson.map.DeserializationConfig;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.codehaus.jackson.map.SerializationConfig;
- import org.codehaus.jackson.map.annotate.JsonSerialize;
- import org.codehaus.jackson.type.JavaType;
- import org.codehaus.jackson.type.TypeReference;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.text.SimpleDateFormat;
- public class JsonUtil {
- private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
- private static ObjectMapper objectMapper = new ObjectMapper();
- static {
- objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);
- objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
- objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
- objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
- objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- }
- public static <T> String obj2String(T obj) {
- if (obj == null) {
- return null;
- }
- try {
- return (obj instanceof String) ? (String) obj : objectMapper.writeValueAsString(obj);
- } catch (Exception e) {
- log.warn("XC JsonUtil Parse obj to string error ", e);
- return null;
- }
- }
- public static <T> String obj2StringPretty(T obj) {
- if (obj == null) {
- return null;
- }
- try {
- return (obj instanceof String) ? (String) obj : objectMapper
- .writerWithDefaultPrettyPrinter().writeValueAsString(obj);
- } catch (Exception e) {
- log.warn("XC JsonUtil Parse obj to string error ", e);
- return null;
- }
- }
- public static <T> T string2Obj(String str, Class<T> clazz) {
- if (StringUtils.isEmpty(str) || clazz == null) {
- return null;
- }
- try {
- return (T) (clazz.equals(String.class) ? str : objectMapper.readValue(str, clazz));
- } catch (Exception e) {
- log.warn("XC 1 JsonUtil Parse string to obj error", e);
- return null;
- }
- }
- public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
- if (StringUtils.isEmpty(str) || typeReference == null) {
- return null;
- }
- try {
- return (T) (typeReference.getType().equals(String.class) ? str : objectMapper
- .readValue(str, typeReference));
- } catch (Exception e) {
- log.warn("XC 2 JsonUtil Parse string to obj error", e);
- return null;
- }
- }
- public static <T> T string2Obj(String str, Class<?> collectionClass, Class... elementClasses) {
- JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
- try {
- return (T) objectMapper.readValue(str, javaType);
- } catch (Exception e) {
- log.warn("XC 3 JsonUtil Parse string to obj error", e);
- return null;
- }
- }
- public static void main(String[] args) {
- }
- }
|