Jackson-性能比较JSON库

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
	<version>2.2.2</version>
</dependency>


jackson的用法   http://tkhhappyboy.blog.163.com/blog/static/11418581520116131144605/
Deserialize JSON to ArrayList<POJO> using Jackson http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson

Jackson第一篇【JSON字符串、实体之间的相互转换】 http://blog.csdn.net/songyongfeng/article/details/6932655

Jackson第二篇【从JSON字符串中取值】 http://blog.csdn.net/songyongfeng/article/details/6932674

Jackson第三篇【ObjectMapper的并发编程】 http://blog.csdn.net/songyongfeng/article/details/6932677


package com.rh.util;


import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pandy
 * Date: 13-7-6
 * Time: 下午5:26
 * To change this template use File | Settings | File Templates.
 */
public class JSONUtils {

    private static  ObjectMapper getObjectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        return mapper;
    }

    public static String bean2str(Object obj) {
        try {
            ObjectMapper mapper = getObjectMapper();
            StringWriter writer = new StringWriter();
            JsonGenerator gen = new JsonFactory().createJsonGenerator(writer);
            mapper.writeValue(gen, obj);
            gen.close();
            String json = writer.toString();
            writer.close();
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Object str2bean(String json, Class<?> clazz) {
        try {
            ObjectMapper mapper = getObjectMapper();
            Object domain = mapper.readValue(json, clazz);
            return domain;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Object str2list(String json, TypeReference valueTypeRef) {
        try {
            ObjectMapper mapper = getObjectMapper();
            Object domain = mapper.readValue(json, valueTypeRef);
            return domain;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Object str2list(String json, Class<?> clazz) {
        try {
            ObjectMapper mapper = getObjectMapper();
            JavaType type = mapper.getTypeFactory().
                    constructCollectionType(ArrayList.class, clazz) ;

            Object domain = mapper.readValue(json, type);
            return domain;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //new TypeReference<Map<String,Object>>() { }
    public static Object str2map(String json, TypeReference valueTypeRef) {
        try {
            ObjectMapper mapper = getObjectMapper();
            Object domain = mapper.readValue(json, valueTypeRef);
            return domain;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}


           

你可能感兴趣的:(Jackson)