以前开发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包下载