package org.linlinjava.litemall.core.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import java.io.IOException;
import java.util.List;
import java.util.Map; public class JacksonUtil { private static final Log logger = LogFactory.getLog(JacksonUtil.class); public static String parseString(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field);
if (leaf != null)
return leaf.asText();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static List<String> parseStringList(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field); if (leaf != null)
return mapper.convertValue(leaf, new TypeReference<List<String>>() {
});
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static Integer parseInteger(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field);
if (leaf != null)
return leaf.asInt();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static List<Integer> parseIntegerList(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field); if (leaf != null)
return mapper.convertValue(leaf, new TypeReference<List<Integer>>() {
});
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static Boolean parseBoolean(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field);
if (leaf != null)
return leaf.asBoolean();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static Short parseShort(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field);
if (leaf != null) {
Integer value = leaf.asInt();
return value.shortValue();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static Byte parseByte(String body, String field) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
JsonNode leaf = node.get(field);
if (leaf != null) {
Integer value = leaf.asInt();
return value.byteValue();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static <T> T parseObject(String body, String field, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node;
try {
node = mapper.readTree(body);
node = node.get(field);
return mapper.treeToValue(node, clazz);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} public static Object toNode(String json) {
if (json == null) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
try { return mapper.readTree(json);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} return null;
} public static Map<String, String> toMap(String data) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(data, new TypeReference<Map<String, String>>() {
});
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
} }

JacksonUtil的更多相关文章

  1. 第一章 JacksonUtil 序列化与反序列化属性总结

    1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...

  2. Jackson 对象与json数据互转工具类JacksonUtil

    1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...

  3. springMVC、httpClient调用别人提供的接口!!!(外加定时调用)

    import com.ibm.db.util.AppConfig; import com.ibm.db.util.JacksonUitl; import org.apache.http.HttpEnt ...

  4. Android 通过Base64上传图片到服务器

    之前做上传图片是采用HttpServlet上传,不过用了一下Base64上传图片后,感觉比HttpServlet方便很多,大家也可以跟着尝试一下. 前台图片处理:(传Bitmap对象即可) /** * ...

  5. PowerMockito(PowerMock用法)

    网络上大部分是powermock 的用法, PowerMock有两个重要的注解: –@RunWith(PowerMockRunner.class) –@PrepareForTest( { YourCl ...

  6. 介绍4款json的java类库 及 其性能测试

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  7. Maven搭建SpringMVC+MyBatis+Json项目(多模块项目)

    一.开发环境 Eclipse:eclipse-jee-luna-SR1a-win32; JDK:jdk-8u121-windows-i586.exe; MySql:MySQL Server 5.5; ...

  8. SpringAop注解实现日志的存储

    一.介绍 1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封 ...

  9. Java几种常用JSON库性能比较

    本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能. 每次都在网上看到别人说什么某某库性能是如何如何的好,碾压其他的库.但是百闻不如一见,只有自己亲手测试过的才是最值得相信的. JSON ...

随机推荐

  1. 高性能集群软件keepalived

     Keepalived介绍 以下是keepalive官网上的介绍.官方站点为http://www.keepalived.org.         Keepalived is a routing sof ...

  2. C++中substr()详解

    #include<string> #include<iostream> using namespace std; int main() { string s("123 ...

  3. Java8集合框架——基本知识点

    前言 Java的基础集合框架的内容并不复杂,List.Map.Set 中大概10个常见的集合类,建议多看几遍源码(Java8),然后回过头再来看看这些各路博客总结的知识点,会有一种豁然开朗的感觉. 本 ...

  4. SEO教程:快速增加360搜索引擎收录,360自动推送批量推送版

    上次改编了一下百度的JS推送代码,实现了批量推送 传送门>>>百度链接提交-js代码推送批量推送版 这次我们来研究360js自动推送代码. <script> (funct ...

  5. Python的 5 种高级用法,效率提升没毛病!

    任何编程语言的高级特征通常都是通过大量的使用经验才发现的.比如你在编写一个复杂的项目,并在 stackoverflow 上寻找某个问题的答案.然后你突然发现了一个非常优雅的解决方案,它使用了你从不知道 ...

  6. 关于DSP仿真软件CCS中断点和探针的简单理解

    关于DSP仿真软件CCS中断简单理解 (郑州大学姬祥老师编写) CCS中的2.0版本(实验所用)断点(Break Point) 和探针(Probe Point)之所以能组合使用,是因为我们在实现硬件仿 ...

  7. SQL基础教程(第2版)第7章 集合运算:练习题

    可能有些读者会对此感到惊讶:“同时使用 UNION 和 INTERSECT 时,不是 INTERSECT 会优先执行吗?”当然,从执行顺序上来说确实是从 INTERSECT 开始的, 但是在此之前,由 ...

  8. CSS——fixed 固定定位相对于父容器

    position:fixed 固定定位 用 left top 都是相对于浏览器的. 我今天想给网页做一个固定定位的导航 偶然间发现.可以用margin 相对于父容器定位. 小伙伴们可以试试. 不用le ...

  9. Django2.0——Form组件简单总结

    Django提供了一个Form组件来配和前端的表单进行使用,Form有两个强大的功能,分别是生成HTML代码和验证数据的合法性.通常我们不会用其第一个功能,因为前端的设计可以做出更加精美且多样的表单页 ...

  10. Xcode7以后访问http加密

    Xcode7 在Info.plist中add Row添加NSAppTransportSecurity类型Dictionary. 在NSAppTransportSecurity下添加NSAllowsAr ...