net.shopxx.util.JsonUtil.java Source code

Java tutorial

Introduction

Here is the source code for net.shopxx.util.JsonUtil.java

Source

package net.shopxx.util;

import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.util.Assert;

/**
 * Util - JSON
 * http://www.shopxx.net
 * 
 * Copyright  2012 shopxx.net All Rights Reserved.
 * License: http://www.shopxx.net/license
 * 
 * Revision: 2.1
 * Date: 2012-01
 */

public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * ?JSON
     * @param object 
     */
    public static String toJson(Object object) {
        Assert.notNull(object);
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * ?JSON?
     * @param response HttpServletResponse
     * @param contentType contentType
     * @param object 
     */
    public static void toJson(HttpServletResponse response, String contentType, Object value) {
        Assert.notNull(response);
        Assert.notNull(contentType);
        Assert.notNull(value);
        try {
            response.setContentType(contentType);
            mapper.writeValue(response.getWriter(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ?JSON?
     * @param response HttpServletResponse
     * @param object 
     */
    public static void toJson(HttpServletResponse response, Object value) {
        Assert.notNull(response);
        Assert.notNull(value);
        try {
            mapper.writeValue(response.getWriter(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * JSON?
     * @param json JSON
     * @param valueType 
     */
    public static <T> T toObject(String json, Class<T> valueType) {
        Assert.notNull(json);
        Assert.notNull(valueType);
        try {
            return mapper.readValue(json, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JSON?
     * @param json JSON
     * @param typeReference 
     */
    public static <T> T toObject(String json, TypeReference<?> typeReference) {
        Assert.notNull(json);
        Assert.notNull(typeReference);
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JSON?
     * @param json JSON
     * @param javaType 
     */
    public static <T> T toObject(String json, JavaType javaType) {
        Assert.notNull(json);
        Assert.notNull(javaType);
        try {
            return mapper.readValue(json, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}