Json操作工具类,使用jackson,可用作json字符串转Java类,字符串转Map,Java对象转json字符串等等

Json操作工具类,使用jackson,可用作json字符串转Java类,字符串转Map,Java对象转json字符串等等.md

  • 一、Json工具类如下
  • 二、测试


一、Json工具类如下

package cn.lihua.system.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

public class JsonUtil {

    private final static Logger logger = LoggerFactory.getLogger(JsonUtil.class);

    //日期格式化
    private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static ObjectMapper objectMapper;

    static{

        /**
         * ObjectobjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectobjectMapper中实现。
         * ObjectobjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
         * writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
         * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
         * writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
         * writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
         */
        objectMapper = new ObjectMapper();

        //对象的所有字段全部列入
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        //取消默认转换timestamps形式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

        //忽略空Bean转json的错误
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);

        //所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));

        //忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);

        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
        
        //解决Java8 LocalDate,LocalDateTime等序列化问是同
        JavaTimeModule module=new JavaTimeModule();
        module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        module.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        objectMapper.registerModule(module);


    }

    /**
     * 对象转Json格式字符串
     * @param obj 对象
     * @return Json格式字符串
     */
    public static String toJSONString(Object o) {

        if (o == null) {
            return null;
        }

        if (o instanceof String)
            return (String) o;

        String jsonValue = null;
        try {
            jsonValue = objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            logger.error("Parse Object to String error",e);
        }

        return jsonValue;

    }

    @SuppressWarnings("unchecked")
    public static Map<String,Object> castToObject(String fromValue){
        if(fromValue == null || "".equals(fromValue) ){
            return null;
        }

        try {
            return objectMapper.readValue(fromValue, Map.class);
        } catch (Exception e) {
            logger.error("Parse String to Object error:", e);
            return null;
        }

    }

    /**
     * 字符串转换为自定义对象
     * @param str 要转换的字符串
     * @param clazz 自定义对象的class对象
     * @return 自定义对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T castToObject(String fromValue, Class<T> clazz){
        if(fromValue == null || "".equals(fromValue) || clazz == null){
            return null;
        }

        try {
            return clazz.equals(String.class) ? (T) fromValue : objectMapper.readValue(fromValue, clazz);
        } catch (Exception e) {
            logger.error("Parse String to Object error:", e);
            return null;
        }

    }

    @SuppressWarnings("unchecked")
    public static <T> T castToObject(String fromValue, TypeReference<T> typeReference) {
        if (fromValue == null || "".equals(fromValue) || typeReference == null) {
            return null;
        }
        try {
            return (T) (typeReference.getType().equals(String.class) ? fromValue : objectMapper.readValue(fromValue, typeReference));
        } catch (IOException e) {
            logger.error("Parse String to Object error:", e);
            return null;
        }
    }
    
    public static <T> T castToObject(String fromValue, Class<?> collectionClazz, Class<?>... elementClazzes) {
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
        try {
            return objectMapper.readValue(fromValue, javaType);
        } catch (IOException e) {
            logger.error("Parse String to Object error : ", e.getMessage());
            return null;
        }
    }
    
    public static <T> T getValue(String fromValue, Class<T> clazz){
    	return castToObject(fromValue,clazz);
    }
    
    public static <T> T getValue(String fromValue, TypeReference<T> toValueTypeRef){
    	return castToObject(fromValue, toValueTypeRef);
    }
    
    public static <T> T getValue(String fromValue, Class<?> collectionClazz, Class<?>... elementClazzes){
    	return castToObject(fromValue, collectionClazz, elementClazzes);
    }
    
    //可通过点语法获取数据,如getValue("data.list","xxxxxxxx",List.class);
    public static <T> T getValue(String key, String fromValue, Class<T> clazz){
    	Map<String,Object> infoMap = castToObject(fromValue);
    	if(infoMap == null) return null;
    	
    	return getValue(key, infoMap, clazz);
    }
    
    //可通过点语法获取数据,如getValue("data.list","xxxxxxxx",new TypeReference>(){});
    public static <T> T getValue(String key, String fromValue, TypeReference<T> toValueTypeRef){
    	Map<String,Object> infoMap = castToObject(fromValue);
    	if(infoMap == null) return null;
    	
    	return getValue(key, infoMap, toValueTypeRef);
    }
    
    public static <T> T getValue(String key, String fromValue, Class<?> collectionClazz, Class<?>... elementClazzes){
    	Map<String,Object> infoMap = castToObject(fromValue);
    	if(infoMap == null) return null;
    	return getValue(key, infoMap, collectionClazz, elementClazzes);
    }
    
    //可通过点语法获取数据,如getValue("data.list",new TypeReference>(){});
    @SuppressWarnings("rawtypes")
	public static <T> T getValue(String key, Map fromMap, Class<T> clazz){
    	
    	try {
    		
    		// 首先将key进行拆分
            String[] keys = key.split("[.]");
            
            for (int i = 0; i < keys.length; i++) {
            	
            	Object value = fromMap.get(keys[i]);
            	if(value == null) return null;
            	
            	if (i < keys.length - 1) {
            		fromMap = (Map) value;
                }else {
                    return objectMapper.convertValue(value, clazz);
                }
            }
            
        } catch (Exception e) {
            logger.error("getValue error : ", e.getMessage());
            return null;
        }
        
        return null;
        
    }
    
    @SuppressWarnings("rawtypes")
	public static <T> T getValue(String key, Map fromMap, Class<?> collectionClazz, Class<?>... elementClazzes){
    	try {
    		
        	// 首先将key进行拆分
            String[] keys = key.split("[.]");
            
            for (int i = 0; i < keys.length; i++) {
            	
            	Object value = fromMap.get(keys[i]);
            	if(value == null) return null;
            	
            	if (i < keys.length - 1) {
            		fromMap = (Map) value;
                }else {
                	JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
                    return objectMapper.convertValue(value, javaType);
                }
            	
            }
            
        } catch (Exception e) {
            logger.error("getValue error : ", e.getMessage());
            return null;
        }
    	
        return null;
    }
    
    @SuppressWarnings("rawtypes")
	public static <T> T getValue(String key, Map fromMap, TypeReference<T> toValueTypeRef){
    	
    	try {
    		
        	// 首先将key进行拆分
            String[] keys = key.split("[.]");
            
            for (int i = 0; i < keys.length; i++) {
            	
            	Object value = fromMap.get(keys[i]);
            	if(value == null) return null;
            	
            	if (i < keys.length - 1) {
            		fromMap = (Map) value;
                }else {
                    return objectMapper.convertValue(value, toValueTypeRef);
                }
            	
            }
            
        } catch (Exception e) {
            logger.error("getValue error : ", e.getMessage());
            return null;
        }
    	
        return null;
        
    }
    
    /**
     * 将对像转换成具体的其他Bean对像
     * @param fromValue
     * @param toValueTypeRef
     * @return
     */
    public static <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef){
    	
    	try {
    		return objectMapper.convertValue(fromValue, toValueTypeRef);
        } catch (Exception e) {
            logger.error("convertValue error : ", e.getMessage());
            return null;
        }
    	
    }
    
    public static <T> T convertValue(Object fromValue, Class<T> toValueType){
    	
    	try {
    		return objectMapper.convertValue(fromValue, toValueType);
        } catch (Exception e) {
            logger.error("convertValue error : ", e.getMessage());
            return null;
        }
    	
    }

    public static String getString(Map<String,Object> fromMap, String fieldName){
        return fromMap.get(fieldName)==null ? null : fromMap.get(fieldName).toString();
    }
    
    //根据filedName的key查找map为空时,使用对应的defaultValue默认值替换返回
    public static String getString(Map<String,Object> jsonObject, String fieldName,String defaultValue){
        return jsonObject.get(fieldName)==null ? defaultValue : jsonObject.get(fieldName).toString();
    }

    public static Integer getInteger(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? null : (Integer)jsonObject.get(fieldName);
    }
    
    public static Double getDouble(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? null : (Double)jsonObject.get(fieldName);
    }
    
    public static Boolean getBoolean(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? false : (Boolean)jsonObject.get(fieldName);
    }
    
    public static Long getLong(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? null : (Long)jsonObject.get(fieldName);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> List<T> getList(Map<String,Object> jsonObject, String fieldName,Class<T> clazz){
        return jsonObject.get(fieldName)==null ? null : (List<T>)jsonObject.get(fieldName);
    }

    @SuppressWarnings("unchecked")
    public static <T> T[] getArray(Map<String,Object> jsonObject, String fieldName,Class<T> clazz){
        return jsonObject.get(fieldName)==null ? null : (T[])jsonObject.get(fieldName);
    }
}

二、测试

		UserList info = new UserList();
    	info.setUuid(java.util.UUID.randomUUID().toString());
    	info.setAge(100);
    	
    	User user1 = new User();
    	user1.setUser("liu");
    	user1.setAge(40);
    	user1.setCurrentTime(LocalDateTime.now());
    	user1.setDateTime(new Date());
    	
    	User user2 = new User();
    	user2.setUser("he");
    	user2.setAge(35);
    	user2.setCurrentTime(LocalDateTime.now());
    	user2.setDateTime(new Date());
    	
    	List<User> list1 = new ArrayList<User>();
    	list1.add(user1);
    	list1.add(user2);
    	
    	Set<User> sets = new HashSet<User>();
    	sets.add(user1);
    	
    	info.setSubUsers(list1);
    	info.setSubSets(sets);
    	
    	Map<String,Object> result = new LinkedHashMap<String, Object>();
    	result.put("success", true);
    	result.put("code", 200);
    	result.put("data", info);
    	
    	//{"uuid":"3d74ba34-4042-4f2e-8c09-0d78e4cd51bd","subUsers":[{"user":"liu","age":40,"dateTime":"2022-10-15 21:22:28","currentTime":"2022-10-15 21:22:28"},{"user":"he","age":35,"dateTime":"2022-10-15 21:22:28","currentTime":"2022-10-15 21:22:28"}],"subSets":[{"user":"liu","age":40,"dateTime":"2022-10-15 21:22:28","currentTime":"2022-10-15 21:22:28"}]}
    	//对像转String
    	String jsonStr = JsonUtil.toJSONString(result);
    	System.out.println(jsonStr);
    	
    	//转换成map
    	Map<String,Object> mapInfo = JsonUtil.castToObject(jsonStr);
    	
    	//转换加UserList对像
    	UserList userList = JsonUtil.getValue("data", jsonStr, UserList.class);
    	System.out.println(userList);
    	System.out.println(JsonUtil.getValue("data", jsonStr, Map.class));
    	
    	//获取uuid
    	System.out.println(JsonUtil.getValue("data.uuid", jsonStr, String.class));
    	System.out.println(JsonUtil.getValue("data.uuid", mapInfo, String.class));
    	//获取Integer
    	System.out.println(JsonUtil.getValue("data.age", jsonStr, Integer.class));
    	System.out.println(JsonUtil.getValue("data.age", mapInfo, Integer.class));
    	
    	//获取List
    	List<User> list11 = JsonUtil.getValue("data.subUsers", jsonStr, List.class, User.class);
    	System.out.println("list11:" + list11);
    	System.out.println(JsonUtil.getValue("data.subUsers", jsonStr, new TypeReference<List<User>>(){} ).getClass());
    	System.out.println(JsonUtil.getValue("data.subUsers", mapInfo, new TypeReference<List<User>>(){} ).getClass());
    	System.out.println(JsonUtil.getValue("data.subUsers", mapInfo, List.class ).getClass());
    	
    	//获取set
    	Set<User> set11 = JsonUtil.getValue("data.subSets", jsonStr, Set.class, User.class);
    	System.out.println("set11:" + set11);
    	System.out.println(JsonUtil.getValue("data.subSets", jsonStr, new TypeReference<Set<User>>(){} ).getClass());
    	System.out.println(JsonUtil.getValue("data.subSets", mapInfo, new TypeReference<Set<User>>(){} ).getClass());
    	String setStr = JsonUtil.toJSONString(JsonUtil.getValue("data.subSets", jsonStr, List.class));
    	System.out.println("setStr: " + setStr);
    	System.out.println(JsonUtil.castToObject(setStr, new TypeReference<Set<User>>(){} ));
    	
    	//
    	System.out.println(JsonUtil.toJSONString(list1));
    	List<User> list = JsonUtil.castToObject(JsonUtil.toJSONString(list1),List.class, User.class);
    	System.out.println(list);
    	
    	System.out.println(JsonUtil.getValue("data", jsonStr, UserList.class));

你可能感兴趣的:(【Java】,java,json,servlet)