Android JackSon (Json工具) 简单使用封装

以前开发Android Json 序列化一般使用Gson,调查以后发现 JackSon 性能要比Gson 好很多,所以进行了一下总结,准备用在新项目中。

封装代码如下:

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.type.JavaType;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by FengYi on 2014/8/7.
 */
public class FJackson {

    static ObjectMapper mapper;

    /**
     * ObjectMapper 配置及初始化
     */
    public static ObjectMapper Instance() {

        if (mapper == null) {
            mapper = new ObjectMapper();
            mapper.getSerializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            mapper.getDeserializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        }
        return mapper;
    }


    /**
     * 将原始Json转为T类型实体
     */
    public static <T> T ToEntity(String content, Class<T> classT) {
        T result = null;
        try {
            result = Instance().readValue(content, classT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将原始Json转为T类型实体集合
     */
    public static <T> List<T> ToEntityS(String content, Class<T> classT) {
        List<T> result = null;
        try {
            JavaType javaType = Instance().getTypeFactory().constructParametricType(List.class, classT);
            result = Instance().readValue(content, javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 遍历到指定节点并将节点中内容转为T类型实体
     */
    public static <T> T ToEntityByNode(String content, Class<T> classT, String... path) {
        T result = null;
        try {
            result = Instance().readValue(GetNodeString(content, path), classT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 遍历到指定节点并将节点中内容转为T类型实体集合
     */
    public static <T> List<T> ToEntitySByNode(String content, Class<T> classT, String... path) {
        List<T> result = null;
        try {
            JavaType javaType = Instance().getTypeFactory().constructParametricType(List.class, classT);
            result = Instance().readValue(GetNodeString(content, path), javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取指定节点的原始值
     * key,value(String)
     */
    public static String GetNodeStringValue(String content, String... path) {
        String result = null;
        try {
            JsonNode jsonNode = Instance().readTree(content);

            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode.getTextValue();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取指定节点的原始值
     * key,value(Int)
     */
    public static int GetNodeIntValue(String content, String... path) {
        int result = 0;
        try {
            JsonNode jsonNode = Instance().readTree(content);
            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode.getIntValue();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 以字符串形式获取指定节点中的内容(*与数值不同,节点中内容可能为 Json Array 等,并非原始值)
     */
    public static String GetNodeString(String content, String... path) {
        String result = null;
        try {
            JsonNode jsonNode = Instance().readTree(content);
            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode + "";
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * Object 转为  Json
     */
    public static String ToJSon(Object obj) {
        String result = null;
        try {
            result = Instance().writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


}
JackSon(1.9) Jar包下载地址 :  Jar包下载



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值