如何使用Jackson反序列化空类型JSON字段 [英] How to deserialize null type JSON fields with Jackson
问题描述
我正在开发PATCH API.如果将字段作为JSON
null
值发送,则需要将其另存为null
.但是我无法区分它们是作为null
发送还是从不发送.
I'm developing a PATCH API. If the fields are sent as JSON
null
value I need to save them as null
. However I can't distinguish if they're sent as null
or never sent.
{
"max_amount": null
}
Double maxAmount;
我有Double
,Integer
,Date
等字段.当它们确实以null
的形式发送时,我可以将它们反序列化为Double.NAN
,Integer.MIN_VALUE
,以了解它们是否以null
的形式发送.但是,当字段为null
时,反序列化器将不起作用.当然,可以选择发送"-1"或一个不可能的值来定义null
,但是我不喜欢这种方法.我将不得不同意所有类型的客户.在这种情况下最好的方法是什么?
I have Double
, Integer
, Date
etc. fields. I can deserialize them to Double.NAN
, Integer.MIN_VALUE
when they're really sent as null
to understand if they're sent as null
. But Deserializers don't work when the field is null
. Of course it's an option to send "-1" or an impossible value to define null
but I didn't like this approach. I will have to agree with the clients for all types. What's the best approach in this case?
推荐答案
在这种情况下,应定义属性设置为预定义的undefined like
数据的POJO
类.例如,对于Integer
属性,如果从业务角度来看不允许使用负数,则它可以是-1
.然后,将JSON
反序列化为POJO
时,设置为null
的属性将覆盖默认值,并且您将知道它已发送.应该有3个选项:
In cases like this you should define POJO
class with properties set to predefined undefined like
data. For example, for Integer
property if negative numbers are not allowed from business point of view, it could be -1
. Then, when JSON
is deserialised to POJO
properties set to null
override default values and you will know it was sent. There should be 3 options:
-
JSON
中的常规值-正确反序列化为POJO
中的值 -
null
值-在POJO
中反序列化为null
- 不可用对
key-value
-POJO
中的默认值不会被覆盖.
JSON
中的- Regular value in
JSON
- properly deserialised to a value inPOJO
null
value inJSON
- deserialised tonull
inPOJO
- Not available pair
key-value
- default value inPOJO
is not overridden.
下面的示例在版本2.9.9
中使用Jackson
:
Below example uses Jackson
in version 2.9.9
:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.LocalDateTime;
public class JsonApp {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
String[] jsons = {
"{\"number\":1,\"date\":\"2019-01-01T22:23:11\",\"max_amount\":4.9}",
"{\"number\":null,\"date\":null,\"max_amount\":null}",
"{}",
"{\"number\":1}",
"{\"date\":\"2019-01-01T22:23:11\"}",
"{\"max_amount\":4.9}",
"{\"number\":1,\"date\":null,\"max_amount\":null}"
};
for (String json : jsons) {
System.out.println(json + " => " + mapper.readValue(json, Pojo.class));
}
}
}
class Pojo {
private static final LocalDateTime NULL_DATE = LocalDateTime.of(1900, 1, 1, 12, 13);
@JsonProperty("max_amount")
private Double maxAmount = Double.MIN_VALUE;
private Integer number = Integer.MIN_VALUE;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private LocalDateTime date = NULL_DATE;
public Double getMaxAmount() {
return maxAmount;
}
public void setMaxAmount(Double maxAmount) {
this.maxAmount = maxAmount;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public LocalDateTime getDate() {
return date;
}
public void setDate(LocalDateTime date) {
this.date = date;
}
@Override
public String toString() {
return "{" +
"number=" + number +
", date=" + date +
", maxAmount=" + maxAmount +
'}';
}
}
上面的代码显示:
{"number":1,"date":"2019-01-01T22:23:11","max_amount":4.9} => {number=1, date=2019-01-01T22:23:11, maxAmount=4.9}
{"number":null,"date":null,"max_amount":null} => {number=null, date=null, maxAmount=null}
{} => {number=-2147483648, date=1900-01-01T12:13, maxAmount=4.9E-324}
{"number":1} => {number=1, date=1900-01-01T12:13, maxAmount=4.9E-324}
{"date":"2019-01-01T22:23:11"} => {number=-2147483648, date=2019-01-01T22:23:11, maxAmount=4.9E-324}
{"max_amount":4.9} => {number=-2147483648, date=1900-01-01T12:13, maxAmount=4.9}
{"number":1,"date":null,"max_amount":null} => {number=1, date=null, maxAmount=null}
当然,您应该选择默认值,以最大程度地减少客户端偶然将您的API
发送为undefined
的值发送给客户端的冲突可能性.
Of course, you should pick default values in the way it will minimise possibility of collision that client by accident send value treated as undefined
by your API
.
这篇关于如何使用Jackson反序列化空类型JSON字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!