jackson中自定义处理序列化和反序列化

对于一直用gson的人来说,如果单独用jackson,真是麻烦了,但还是得小结下了: 
先来看下如何自定义把某个对象序列化为json: 

先是对象: 
Java代码   收藏代码
  1. public class User {  
  2.     public int id;  
  3.     public String name;  
  4. }  
  5. public class Item {  
  6.     public int id;  
  7.     public String itemName;  
  8.     public User owner;  
  9. }  


   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类,如下: 
 
Java代码   收藏代码
  1. public class ItemSerializer extends JsonSerializer<Item> {  
  2.     @Override  
  3.     public void serialize(Item value, JsonGenerator jgen, SerializerProvider provider)   
  4.       throws IOException, JsonProcessingException {  
  5.         jgen.writeStartObject();  
  6.         jgen.writeNumberField("id", value.id);  
  7.         jgen.writeStringField("itemName", value.itemName);  
  8.         jgen.writeNumberField("owner", value.owner.id);  
  9.         jgen.writeEndObject();  
  10.     }  
  11. }  

  然后 
Java代码   收藏代码
  1. Item myItem = new Item(1"theItem"new User(2"theUser"));  
  2. ObjectMapper mapper = new ObjectMapper();  
  3.    
  4. SimpleModule module = new SimpleModule();  
  5. module.addSerializer(Item.classnew ItemSerializer());  
  6. mapper.registerModule(module);  
  7.    
  8. String serialized = mapper.writeValueAsString(myItem);  

    看,相当复杂,然后看有无办法简单点,其实是有的哦;方法为: 
Java代码   收藏代码
  1. @JsonSerialize(using = ItemSerializer.class)  
  2. public class Item {  
  3.     ...  
  4. }  


  就是使用注解@JsonSerialize,然后: 
  
Java代码   收藏代码
  1. Item myItem = new Item(1"theItem"new User(2"theUser"));  
  2. String serialized = new ObjectMapper().writeValueAsString(myItem);  



  接下来看如何反序列化了, 
同样,要反序列化的两个pojo为: 
 
Java代码   收藏代码
  1. public class User {  
  2.     public int id;  
  3.     public String name;  
  4. }  
  5. public class Item {  
  6.     public int id;  
  7.     public String itemName;  
  8.     public User owner;  
  9. }  

   反序列化代码为: 
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"]) 
  
代码: 
  
Java代码   收藏代码
  1. public class ItemDeserializer extends JsonDeserializer<Item> {  
  2.    
  3.     @Override  
  4.     public Item deserialize(JsonParser jp, DeserializationContext ctxt)   
  5.       throws IOException, JsonProcessingException {  
  6.         JsonNode node = jp.getCodec().readTree(jp);  
  7.         int id = (Integer) ((IntNode) node.get("id")).numberValue();  
  8.         String itemName = node.get("itemName").asText();  
  9.         int userId = (Integer) ((IntNode) node.get("id")).numberValue();  
  10.    
  11.         return new Item(id, itemName, new User(userId, null));  
  12.     }  
  13. }  
  14.   
  15. ObjectMapper mapper = new ObjectMapper();  
  16. SimpleModule module = new SimpleModule();  
  17. module.addDeserializer(Item.classnew ItemDeserializer());  
  18. mapper.registerModule(module);  
  19.    
  20. Item readValue = mapper.readValue(json, Item.class);  

   也可以用注解: 
@JsonDeserialize(using = ItemDeserializer.class) 
public class Item { 
    ... 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值