如何处理同名但类型不同的字段?在同一个请求中,我有时会从 API 获取整数值,有时会获取 boolean 值。我想知道当我得到这样的 Json 时如何处理。我创建了类型适配器,但它不起作用
我考虑过创建不同的 POJO 类。但是这个问题不仅仅针对一个请求。出于这个原因,我不喜欢创建 POJO。顺便说一句,我看到了类似的问题,但它并没有解决我的问题。
{
"name" : "john doe",
"isValid" : true
}
有时我会得到 int
{
"name" : "john doe",
"isValid" : 1
}
获取整数时出现意外的 json 异常
class XModel{
private boolean isValid;
...
...
}
我想为每个请求返回一个 boolean 值。有谁知道如何解决这个问题?
编辑: 我想通过类型适配器阻止 instanceOf 关键字
解决方案:@Michał Ziober 的回复对我有用。
class BooleanJsonDeserializer implements JsonDeserializer<Boolean> {
private final Set<String> TRUE_STRINGS = new HashSet<>(Arrays.asList("true", "1", "yes"));
@Override
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
System.out.println(json);
JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
return jsonPrimitive.getAsBoolean();
} else if (jsonPrimitive.isNumber()) {
return jsonPrimitive.getAsNumber().intValue() == 1;
} else if (jsonPrimitive.isString()) {
return TRUE_STRINGS.contains(jsonPrimitive.getAsString().toLowerCase());
}
return false;
}
}
最佳答案
如果 XModel
类不大,您可以编写自定义反序列化器,如下所示,您可以控制传入的元素:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class GsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
Gson gson = new GsonBuilder()
.registerTypeAdapter(XModel.class, new XModelJsonDeserializer())
.create();
System.out.println(gson.fromJson(new FileReader(jsonFile), XModel.class));
}
}
class XModelJsonDeserializer implements JsonDeserializer<XModel> {
private final Set<String> TRUE_STRINGS = new HashSet<>(Arrays.asList("true", "1", "yes"));
@Override
public XModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
XModel response = new XModel();
JsonObject jsonResponse = (JsonObject) json;
response.setName(jsonResponse.get("name").getAsString());
// other fields
JsonElement dataElement = jsonResponse.get("isValid");
if (dataElement.isJsonNull()) {
response.setValid(false);
} else if (dataElement.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = dataElement.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
response.setValid(jsonPrimitive.getAsBoolean());
} else if (jsonPrimitive.isNumber()) {
response.setValid(jsonPrimitive.getAsNumber().intValue() == 1);
} else if (jsonPrimitive.isString()) {
response.setValid(TRUE_STRINGS.contains(jsonPrimitive.getAsString()));
}
System.out.println("Json data is primitive: " + dataElement.getAsString());
} else if (dataElement.isJsonObject() || dataElement.isJsonArray()) {
response.setValid(true); //?!?!
}
return response;
}
}
对于下面的 JSON
负载:
{
"name" : "john doe",
"isValid" : true
}
以上程序打印:
Json data is primitive: true
XModel{name='john doe', isValid=true}
对于 JSON
负载:
{
"name" : "john doe",
"isValid" : 1
}
打印:
Json data is primitive: 1
XModel{name='john doe', isValid=true}
您的模型很清晰,因为所有工作都在解串器级别完成。
更精确的解决方案是仅序列化 primitive
。让我们假设模型如下所示:
class XModel {
private String name;
@JsonAdapter(value = BooleanJsonDeserializer.class)
private boolean isValid;
// getters, setters
}
我们的 BooleanJsonDeserializer
反序列化器如下所示:
class BooleanJsonDeserializer implements JsonDeserializer<Boolean> {
private final Set<String> TRUE_STRINGS = new HashSet<>(Arrays.asList("true", "1", "yes"));
@Override
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
System.out.println(json);
JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
return jsonPrimitive.getAsBoolean();
} else if (jsonPrimitive.isNumber()) {
return jsonPrimitive.getAsNumber().intValue() == 1;
} else if (jsonPrimitive.isString()) {
return TRUE_STRINGS.contains(jsonPrimitive.getAsString().toLowerCase());
}
return false;
}
}
您只需在模型中使用此适配器注释每个 boolean
属性,它就可以处理:1
、True
等.
关于java - GSON 将特定字段的整数值动态转换为 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55091868/