您的位置:首页 > 其它

jaskSon二次封装,使用更方便

2016-02-18 16:52 363 查看
原帖地址找不到了,抱歉,如有原作者要求我即可删除。。。

public class JacksonUtil {

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);
mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ;
mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{

@Override
public void serialize(
Object value,
JsonGenerator jg,
SerializerProvider sp) throws IOException, JsonProcessingException
{
jg.writeString("");
}
});
}
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;
}

/**
* 获取指定节点boolean值
* key,value(String)
*/
public static boolean GetNodeBooleanValue(String content, String... path) {
boolean result = false;
try {
JsonNode jsonNode = Instance().readTree(content);

for (String p : path) {
jsonNode = jsonNode.path(p);
}
result = jsonNode.getBooleanValue();
} 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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: