码迷,mamicode.com
首页 > 编程语言 > 详细

JsonUtil.java

时间:2015-12-14 18:21:00      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

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;
     }
}

 

JsonUtil.java

标签:

原文地址:http://www.cnblogs.com/iamcui/p/5045731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!