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. 面向对象变成(OOP)-创建类和使用类

    1.1.1对象的抽象:抽象是一种归纳或总结,对象是现实世界物体特征的实例. (1)一切皆是对象. (2)类是对象的抽象. 1.1.2 对象的使用: 当对象被抽象为类以后,就可以创建具体的实例来操作了. ...

  2. psp --2

    PSP0 ---2 项目计划日志 姓名:赵腾                                                日期:9/12/2017 任务 日期 听课 编写程序 阅读课 ...

  3. C语言备忘录——向上取整

    众所周知,C语言的取整方式是向下取整,昨天老师留了一道思考题,问我们C语言怎么向上取整,当时我第一反应就是ceil(),老师说不能用if……else之类的,函数也不行.当时想了想没事不用就不用,去ma ...

  4. mysql四种事务隔离级别

    mysql事务并发问题 ACID什么的就不啰嗦了.mysql多个事务并发的时候,可能会出现如下问题: 1. 更新丢失 即两个事务同时更新某一条数据,后执行的更新操作会覆盖先执行的更新操作,导致先执行的 ...

  5. HTML5 Canvas——基础入门

    认识canvas html5的新标签 <canvas>标签只是图像容器,必须使用js来绘制图形 可以通过多种方法使用canvas绘制路径,盒,圆,字符以及添加图像 canvas画布 < ...

  6. 删除xcode项目中不再使用的图片资源

    1. 利用工具    下载地址  http://jeffhodnett.github.io/Unused/   运行效果如下 2. 通过终端 执行 shell 命令 a. 第一步建立.sh 文件  如 ...

  7. python-IDE使用

    集成开发工具 集成开发环境(IDE,Integrated Development Environment ) VIM 经典的linux下的文本编辑器,只有小白和的牛会用这个 Emacs linux文本 ...

  8. jstl中遍历Map

    在jstl中遍历Map和遍历List与数组一样,都是使用forEach标签. 例子: <%@ page import="java.util.Map" %> <%@ ...

  9. JavaWeb之Servlet入门(二)

    1. 准备 在JavaWeb之Servlet入门(一)中,我们完成了第一个Servlet程序,完成了从URL到后台控制器的中转过程,接下来我们延续JavaWeb之Servlet入门(一)学习下如何传参 ...

  10. MySQL--Centos7下安装5.7.19

    https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html https://segmentfault.com/a/11900000 ...