gpt4 book ai didi

java - Google Http Java 客户端解析为枚举

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:02 24 4
gpt4 key购买 nike

因此,我知道您可以使用 @Value 将 json 值解析为枚举。注释,但是如果 json 中的值与使用 @Value 注释的任何值不匹配,行为是什么。例如,假设一个 api 决定添加一个您的客户不知道的新类型。它的值是否被设置为空,或者是否有异常?有没有办法将枚举值设置为默认的捕获所有值?

最佳答案

根据 this Google groups post ,你会得到 NullPointerException

编辑

引用内容:

I have an object called Job with a enum field statusCode that holds an API. That enum is correctly annotated with @Value, so long as the enum handles all possible values that might come back in the status_code field, json parsing works just fine.

public static class Job {

@Key("status_code")
public JobStatus statusCode;
}

public enum JobStatus {

@Value("starting")
STARTING
}

However, if an unknown value comes back in that field, then I get a

NullPointerException:
java.lang.NullPointerException
at com.google.api.client.util.Data.parsePrimitiveValue(Data.java:445)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:795)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:438)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:745)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:358)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:331)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)

I would like this code to be robust such that if an unknown value comes back it will be treated as null rather than throwing an exception and not returning the Job object. Is there any way to do this?

关于java - Google Http Java 客户端解析为枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166436/

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