Minor: JsonUtils.extractValue shouldn't throw exceptions (#18554)

This commit is contained in:
Sriharsha Chintalapani 2024-11-09 09:45:07 -08:00 committed by GitHub
parent 9d9eccedda
commit a03d915909
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -517,14 +517,17 @@ public final class JsonUtils {
public static <T> T extractValue(String jsonResponse, String... keys) { public static <T> T extractValue(String jsonResponse, String... keys) {
JsonNode jsonNode = JsonUtils.readTree(jsonResponse); JsonNode jsonNode = JsonUtils.readTree(jsonResponse);
// Traverse the JSON structure using keys
for (String key : keys) { for (String key : keys) {
jsonNode = jsonNode.path(key); jsonNode = jsonNode.path(key);
} }
if (jsonNode.isMissingNode() || jsonNode.isNull()) {
// Extract the final value return null;
return JsonUtils.treeToValue(jsonNode, (Class<T>) getValueClass(jsonNode)); }
try {
return JsonUtils.treeToValue(jsonNode, (Class<T>) getValueClass(jsonNode));
} catch (Exception e) {
return null;
}
} }
public static <T> T extractValue(JsonNode jsonNode, String... keys) { public static <T> T extractValue(JsonNode jsonNode, String... keys) {