jackson中自定义处理序列化和反序列化
http://jackyrong.iteye.com/blog/2005323
**********************************************
对于一直用gson的人来说,如果单独用jackson,真是麻烦了,但还是得小结下了:
先来看下如何自定义把某个对象序列化为json:
先是对象:
Java代码
public class User {
public int id;
public String name;
}
public class Item {
public int id;
public String itemName;
public User owner;
}
JACKSON一般的使用很容易,如;
Item myItem = new Item(1, "theItem", new User(2, "theUser"));
String serialized = new ObjectMapper().writeValueAsString(myItem);
结果为:
{
"id": 1,
"itemName": "theItem",
"owner": {
"id": 2,
"name": "theUser"
}
}
如果要输出为如下的样子,比如;
{
"id": 25,
"itemName": "FEDUfRgS",
"owner": 15
}
则要自定义了,要继承JsonSerializer类,如下:
public class ItemSerializer extends JsonSerializer<Item> {
@Override
public void serialize(Item value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeStringField("itemName", value.itemName);
jgen.writeNumberField("owner", value.owner.id);
jgen.writeEndObject();
}
}
然后
Item myItem = new Item(1, "theItem", new User(2, "theUser"));
ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule();
module.addSerializer(Item.class, new ItemSerializer());
mapper.registerModule(module); String serialized = mapper.writeValueAsString(myItem);
看,相当复杂,然后看有无办法简单点,其实是有的哦;方法为:
@JsonSerialize(using = ItemSerializer.class)
public class Item {
...
}
就是使用注解@JsonSerialize,然后:
Item myItem = new Item(1, "theItem", new User(2, "theUser"));
String serialized = new ObjectMapper().writeValueAsString(myItem);
接下来看如何反序列化了,
同样,要反序列化的两个pojo为:
public class User {
public int id;
public String name;
}
public class Item {
public int id;
public String itemName;
public User owner;
}
反序列化代码为:
Item itemWithOwner = new ObjectMapper().readValue(json, Item.class);
如果要自定义反序列化,比如要反序列化的JSON为;
{
"id": 1,
"itemName": "theItem",
"owner": 2
}
则上面这样会报错:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "createdBy" (class org.baeldung.jackson.dtos.Item),
not marked as ignorable (3 known properties: "id", "owner", "itemName"])
at [Source: java.io.StringReader@53c7a917; line: 1, column: 43]
(through reference chain: org.baeldung.jackson.dtos.Item["createdBy"])
代码:
public class ItemDeserializer extends JsonDeserializer<Item> {
@Override
public Item deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
int id = (Integer) ((IntNode) node.get("id")).numberValue();
String itemName = node.get("itemName").asText();
int userId = (Integer) ((IntNode) node.get("id")).numberValue();
return new Item(id, itemName, new User(userId, null));
}
}
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);
Item readValue = mapper.readValue(json, Item.class);
也可以用注解:
@JsonDeserialize(using = ItemDeserializer.class)
public class Item {
...
}
jackson中自定义处理序列化和反序列化的更多相关文章
- 在Spark中自定义Kryo序列化输入输出API(转)
原文链接:在Spark中自定义Kryo序列化输入输出API 在Spark中内置支持两种系列化格式:(1).Java serialization:(2).Kryo serialization.在默认情况 ...
- 【SpringBoot】 中时间类型 序列化、反序列化、格式处理
[SpringBoot] 中时间类型 序列化.反序列化.格式处理 Date yml全局配置 spring: jackson: time-zone: GMT+8 date-format: yyyy-MM ...
- Asp.net中Json的序列化和反序列化(一)
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- ASP.NET中JSON的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...
- ASP.NET 中JSON 的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- Asp.Net中JSON的序列化和反序列化-----JavaScriptSerializer ,加上自己工作心得
在工作中和手机通信用到web服务和javascriptSerializer,返回json数据,供手机端调用,一开始返回的数据是一大堆,比如 [{"word_picture9":&q ...
- ASP.NET中JSON的序列化和反序列化(转)
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- [转]ASP.NET中JSON的序列化和反序列化
本文转自:http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html JSON是专门为浏览器中的网页上运行的JavaScript代码 ...
- Scala中使用 Jackson API 进行JSON序列化和反序列化
1. 什么是 Json 序列化 和 反序列化 序列化 => 将 Java对象 转换成 json字符串反序列化 => 将 json字符串 转换成 Java对象 2. 依赖包 说明 < ...
随机推荐
- 倒计时实现两种方法-NSTimer/GCD
#import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...
- 【转】fileno函数与ftruncate函数
fileno函数与ftruncate函数 2011-10-25 10:03:33 分类: LINUX fileno()函数 功 能:把文件流指针转换成文件描述符相关函数:open, fope ...
- 【转】GDB中应该知道的几个调试方法
文章来源:http://coolshell.cn/articles/3643.html GDB中应该知道的几个调试方法 2011年02月10日 陈皓 评论 40 条评论 70,776 人阅读 七.八 ...
- vim缩进参考线
编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级.代码如下: ? ReferenceLine 1 2 3 4 5 6 7 8 9 10 11 ...
- IDEA git修改远程仓库地址
方法有三种: 方法1.修改命令 git remote set-url origin <url> 方法2.先删后加 git remote rm origin git remote add o ...
- 【MyBatis】MyBatis之如何配置
1,MyBatis简介 MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简 ...
- nmapport状态解析
- 使用GDI+进行图片处理时要注意的问题
原文链接: http://blog.csdn.net/chenlycly/article/details/24112955 与GDI相比,GDI+要强大很多.对于Windows应用程序来说,用GDI是 ...
- JPA学习笔记(8)——映射一对多关联关系
一对多关联关系 本文有很多和多对一是一样的,因此不会写得非常具体. 有看不懂的.能够參考JPA学习笔记(7)--映射多对一关联关系 Order实体类 package com.jpa.helloworl ...
- 开关电源9v,1A