使用Jackson在Java中自定义反序列化JSON FIELD? [英] Custom Deserialization of JSON FIELD with Jackson in java?

查看:637
本文介绍了使用Jackson在Java中自定义反序列化JSON FIELD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个像这样的简单实体类

Given a simple entity class like this

public class User
{
  @JsonProperty
  public Calendar createdOn;

  @JsonProperty
  public String name;
}

我有没有办法挂钩杰克逊流API来自定义反序列化只有createdOn字段?如果没有,那将来有可能成为这样吗?

Is there a way for me to hook into the jackson streaming API to custom deserialize ONLY the createdOn field? If there's not, then would something like this be possible in the future?

public class User
{
  @JsonProperty
  @JsonConverter(MyCustomCalendarConverter.class)
  public Calendar createdOn;

  @JsonProperty
  public String name;
}

看来我可以自定义反序列化整个实体。我只是好奇是否有办法一次只为一个字段自定义反序列化,例如,自定义解析特定日期格式,或者将值数组读入自定义实体等,同时让Jackson通常反序列化实体的其余部分。

It appears that I could custom deserialize the entire entity. I'm just curious if there is a way to customize the deserialization just a field at a time in order to, for example, custom parse a particular date format, or read an array of values into a custom entity, etc. while letting Jackson deserialize the rest of the entity normally.

推荐答案

您可以使用@JsonSerialize定义自定义序列化特定字段:

You can define custom serialize a specific field using @JsonSerialize:

@JsonSerialize(using=MuCustomCalendarConverter.class)

这篇关于使用Jackson在Java中自定义反序列化JSON FIELD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆