• JsonUtil.java


    JsonUtil.java

    package com.xxx.common.util;
    
    import java.io.IOException;
    
    import org.apache.commons.lang.StringUtils;
    import org.codehaus.jackson.JsonParseException;
    import org.codehaus.jackson.JsonParser;
    import org.codehaus.jackson.map.DeserializationConfig;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig.Feature;
    import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
    import org.codehaus.jackson.map.ser.StdSerializerProvider;
    import org.codehaus.jackson.type.TypeReference;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * JSON utility
     * @author bc
     *
     */
    public final class JsonUtil {
        
        /**
         * log
         */
        private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
        
        /**
         * 
         */
        private JsonUtil() {
            
        }
    
        /**
         * 
         */
        static final ObjectMapper OBJECT_MAPPER;
    
        static {
            StdSerializerProvider sp = new StdSerializerProvider();
            OBJECT_MAPPER = new ObjectMapper(null, sp, null);
            OBJECT_MAPPER.getSerializationConfig().withSerializationInclusion(Inclusion.NON_NULL);
            OBJECT_MAPPER.getSerializationConfig().with(Feature.SORT_PROPERTIES_ALPHABETICALLY);
            OBJECT_MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
    
        public static ObjectMapper getObjectMapper() {
            return OBJECT_MAPPER;
        }
    
        /**
         * JSON串转换为Java泛型对象,可以是各种类型
         * 
         * @param <T> return type
         * @param jsonString JSON
         * @param tr TypeReference,例如: new TypeReference< List<FamousUser> >(){}
         * @return List对象列表
         */
        @SuppressWarnings("unchecked")
        public static <T> T json2GenericObject(String jsonString, TypeReference<T> tr) {
            if (!StringUtils.isBlank(jsonString)) {
                try {
                    return (T) OBJECT_MAPPER.readValue(jsonString, tr);
                } catch (Exception e) {
                    LOGGER.warn("json error:" + e.getMessage());
                }
            }
            return null;
        }
     
        /**
         * Java对象转JSON字符串
         * 
         * @param object Java对象,可以是对象,数组,List,Map等
         * @return JSON 字符串
         */
        public static String toJson(Object object) {
            try {
                return OBJECT_MAPPER.writeValueAsString(object);
            } catch (Exception e) {
                LOGGER.warn("json error:" + e.getMessage());
            }
            return null;
    
        }
     
        /**
         * JSON字符串转Java对象
         * 
         * @param jsonString JSON
         * @param c class
         * @return
         */
        public static Object json2Object(String jsonString, Class<?> c) {
            if (!StringUtils.isBlank(jsonString)) {
                try {
                    return OBJECT_MAPPER.readValue(jsonString, c);
                } catch (Exception e) {
                    LOGGER.warn("json error:" + e.getMessage());
                }
    
            }
            return null;
        }
        
        /**
         * 验证字符串是否为json格式
         * 
         * @param json
         * @return
         */
        public static boolean isValidJson(final String json) {
            boolean valid = false;
            try {
               final JsonParser parser = new ObjectMapper().getJsonFactory()
                     .createJsonParser(json);
               while (parser.nextToken() != null) {
               }
               valid = true;
            } catch (JsonParseException jpe) {
                return valid;
            } catch (IOException ioe) {
                return valid;
            }
            return valid;
         }
    }
    在追随技术的道路上,十年如一日~
  • 相关阅读:
    线性最小二乘两种方法
    Coursera machine learning 第二周 编程作业 Linear Regression
    Coursera machine learning 第二周 quiz 答案 Octave/Matlab Tutorial
    Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables
    Codeforces Round #392 (Div. 2) F. Geometrical Progression
    四边形不等式优化DP——石子合并问题 学习笔记
    Codeforces Round #373 (Div. 2) E. Sasha and Array
    hihoCoder #1388 : Periodic Signal
    hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
    Java动态代理分析
  • 原文地址:https://www.cnblogs.com/iamcui/p/5045731.html
Copyright © 2020-2023  润新知