JSON処理類

5099 ワード

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * 
 * <b>   :</b>Jackson   
 * 
 * <p>
 * <b>    :</b>
 * 
 * @author liuhuanchao
 * @since  2016-04-18
 */
public class JacksonUtil {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    private static final JsonFactory JSONFACTORY = new JsonFactory();

    /**
     *   Java Bean   json
     */
    public static String beanToJson(Object o) throws JsonParseException {
        StringWriter sw = new StringWriter();
        JsonGenerator jsonGenerator = null;

        try {
            jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
            MAPPER.writeValue(jsonGenerator, o);
            return sw.toString();

        } catch (Exception e) {
            throw new RuntimeException(e+"  Java Bean   json  ");

        } finally {
            if (jsonGenerator != null) {
                try {
                    jsonGenerator.close();
                } catch (Exception e) {
                    throw new RuntimeException(e+"  Java Bean   json  ");
                }
            }
        }
    }

    /**
     * json   javabean
     *
     * @param json
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object jsonToBean(String json, Class clazz) throws JsonParseException {
        try {
//        	MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        	
            return MAPPER.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e+"json   javabean  ");
        }
    }

    /**
     *   Java Bean   HashMap
     */
    @SuppressWarnings("unchecked")
	public static Map<String, Object> beanToMap(Object o) throws JsonParseException {
        try {
            return MAPPER.readValue(beanToJson(o), HashMap.class);
        } catch (Exception e) {
            throw new RuntimeException(e+"  Java Bean   HashMap  ");
        }
    }

    /**
     *   Json String   HashMap
     */
    @SuppressWarnings("unchecked")
	public static Map<String, Object> jsonToMap(String json, boolean collToString) throws JsonParseException {
        Map<String, Object> map = null;
        try {
            map = MAPPER.readValue(json, HashMap.class);
        } catch (IOException e) {
            throw new RuntimeException(e+"  Java Bean   HashMap  ");
        }
        if (collToString) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (entry.getValue() instanceof Collection || entry.getValue() instanceof Map) {
                    entry.setValue(beanToJson(entry.getValue()));
                }
            }
        }
        return map;

    }

    /**
     * List    json
     *
     * @param list
     * @return
     */
    public static String listToJson(List<Map<String, String>> list) throws JsonParseException {
        JsonGenerator jsonGenerator = null;
        StringWriter sw = new StringWriter();
        try {
            jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
            new ObjectMapper().writeValue(jsonGenerator, list);
            jsonGenerator.flush();
            return sw.toString();
        } catch (Exception e) {
            throw new RuntimeException(e+"List    json  ");
        } finally {
            if (jsonGenerator != null) {
                try {
                    jsonGenerator.flush();
                    jsonGenerator.close();
                } catch (Exception e) {
                    throw new RuntimeException(e+"List    json  ");
                }
            }
        }
    }

    /**
     * json  List
     *
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
	public static List<Map<String, String>> jsonToList(String json) throws JsonParseException {
        try {
            if (json != null && !"".equals(json.trim())) {
                JsonParser jsonParse = JSONFACTORY.createJsonParser(new StringReader(json));

                return (List<Map<String, String>>) new ObjectMapper().readValue(jsonParse, ArrayList.class);
            } else {
                throw new RuntimeException("json  List  ");
            }
        } catch (Exception e) {
            throw new RuntimeException(e+"json  List  ");
        }
    }
}