gpt4 book ai didi

java - Gson反序列化: How to distinguish between fields that are missing and fields that are explicitly set to null?

转载 作者:行者123 更新时间:2023-12-02 12:35:50 25 4
gpt4 key购买 nike

我正在尝试实现 JSON Merge Patch对于我正在构建的 Java (JAX-RS) Web 服务。

要点是通过将 JSON 文档发送到仅包含应更改字段的服务器来完成记录的部分更新。

鉴于此记录

{
"a": "b",
"c": {
"d": "e",
"f": "g"
}
}

,以下JSON更新文档

{
"a":"z",
"c": {
"f": null
}
}

应为“a”设置新值并删除“c”内的“f”

后者才是问题所在。我不知道如何区分 f 缺失的输入和 f 为 null 的输入。 据我所知,两者都会被反序列化为 null目标 Java 对象。

做什么?

最佳答案

我承认 mlk 的回答,但考虑到我已经有了(并且仍然需要)JSON 对象的 POJO 表示,我觉得自动映射仍然比手动查找更好。

这样做的挑战是,正如我所说,在相应的 POJO 中,缺失值和显式 null 值都设置为 null gson.fromJson(...)会填充。 (与 R 的 NULLNA 不同,Java 只有一种“不存在”的表示。)

但是,通过使用Java 8's Optionals对我的数据结构进行建模我可以这样做:区分未设置的内容和设置为 null 的内容。 。这是我最终得到的结果:

1) 我将数据对象中的所有字段替换为 Optional<T> .

public class BasicObjectOptional {

private Optional<String> someKey;
private Optional<Integer> someNumber;
private Optional<String> mayBeNull;

public BasicObjectOptional() {
}

public BasicObjectOptional(boolean initialize) {
if (initialize) {
someKey = Optional.ofNullable("someValue");
someNumber = Optional.ofNullable(42);
mayBeNull = Optional.ofNullable(null);
}
}

@Override
public String toString() {
return String.format("someKey = %s, someNumber = %s, mayBeNull = %s",
someKey, someNumber, mayBeNull);
}

}

或者嵌套的:

public class ComplexObjectOptional {

Optional<String> theTitle;
Optional<List<Optional<String>>> stringArray;
Optional<BasicObjectOptional> theObject;

public ComplexObjectOptional() {
}

public ComplexObjectOptional(boolean initialize) {
if (initialize) {
theTitle = Optional.ofNullable("Complex Object");
stringArray = Optional.ofNullable(Arrays.asList(Optional.ofNullable("Hello"),Optional.ofNullable("World")));
theObject = Optional.ofNullable(new BasicObjectOptional(true));
}
}

@Override
public String toString() {
return String.format("theTitle = %s, stringArray = %s, theObject = (%s)", theTitle, stringArray, theObject);
}
}

2) 实现了序列化器和反序列化器 based on this useful SO answer .

public class OptionalTypeAdapter<E> extends TypeAdapter<Optional<E>> {

public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {

//@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType();
if (rawType != Optional.class) {
return null;
}
final ParameterizedType parameterizedType = (ParameterizedType) type.getType();
final Type actualType = parameterizedType.getActualTypeArguments()[0];
final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(actualType));
return new OptionalTypeAdapter(adapter);
}
};
private final TypeAdapter<E> adapter;

public OptionalTypeAdapter(TypeAdapter<E> adapter) {
this.adapter = adapter;
}

@Override
public void write(JsonWriter out, Optional<E> value) throws IOException {
if(value == null || !value.isPresent()){
out.nullValue();
} else {
adapter.write(out, value.get());
}
}

@Override
public Optional<E> read(JsonReader in) throws IOException {
final JsonToken peek = in.peek();
if(peek != JsonToken.NULL){
return Optional.ofNullable(adapter.read(in));
}
in.nextNull();
return Optional.empty();
}

}

3)初始化Gson时注册了该适配器。

Gson gsonOptFact = new GsonBuilder()
.serializeNulls() // matter of taste, just for output anyway
.registerTypeAdapterFactory(OptionalTypeAdapter.FACTORY)
.create();

这允许我编写 JSON,这样 null和空Optional序列化为null (或者只是从输出中删除),同时将 JSON 读入 Optional字段,例如如果字段是 null我知道 JSON 输入中缺少它,如果该字段是 Optional.empty我知道它被设置为 null在输入中。

<小时/>

示例:

System.out.println(gsonOptFact.toJson(new BasicObjectOptional(true)));
// {"someKey":"someValue","someNumber":42,"mayBeNull":null}

System.out.println(gsonOptFact.toJson(new ComplexObjectOptional(true)));
// {"theTitle":"Complex Object","stringArray":["Hello","World"],"theObject":{"someKey":"someValue","someNumber":42,"mayBeNull":null}}

// Now read back in:
String basic = "{\"someKey\":\"someValue\",\"someNumber\":42,\"mayBeNull\":null}";
String complex = "{\"theTitle\":\"Complex Object\",\"stringArray\":[\"Hello\",\"world\"],\"theObject\":{\"someKey\":\"someValue\",\"someNumber\":42,\"mayBeNull\":null}}";
String complexMissing = "{\"theTitle\":\"Complex Object\",\"theObject\":{\"someKey\":\"someValue\",\"mayBeNull\":null}}";

BasicObjectOptional boo = gsonOptFact.fromJson(basic, BasicObjectOptional.class);
System.out.println(boo);
// someKey = Optional[someValue], someNumber = Optional[42], mayBeNull = Optional.empty

ComplexObjectOptional coo = gsonOptFact.fromJson(complex, ComplexObjectOptional.class);
System.out.println(coo);
// theTitle = Optional[Complex Object], stringArray = Optional[[Optional[Hello], Optional[world]]], theObject = (Optional[someKey = Optional[someValue], someNumber = Optional[42], mayBeNull = Optional.empty])

ComplexObjectOptional coom = gsonOptFact.fromJson(complexMissing, ComplexObjectOptional.class);
System.out.println(coom);
// theTitle = Optional[Complex Object], stringArray = null, theObject = (Optional[someKey = Optional[someValue], someNumber = null, mayBeNull = Optional.empty])

我认为这将使我能够很好地将 JSON Merge Patch 与我现有的数据对象集成。

关于java - Gson反序列化: How to distinguish between fields that are missing and fields that are explicitly set to null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33737289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com