core: move check Json object methods to JsonUtil (#6214)
diff --git a/core/src/main/java/io/grpc/internal/DnsNameResolver.java b/core/src/main/java/io/grpc/internal/DnsNameResolver.java
index 6555c98..0da0929 100644
--- a/core/src/main/java/io/grpc/internal/DnsNameResolver.java
+++ b/core/src/main/java/io/grpc/internal/DnsNameResolver.java
@@ -451,7 +451,7 @@
throw new ClassCastException("wrong type " + rawChoices);
}
List<?> listChoices = (List<?>) rawChoices;
- possibleServiceConfigChoices.addAll(ServiceConfigUtil.checkObjectList(listChoices));
+ possibleServiceConfigChoices.addAll(JsonUtil.checkObjectList(listChoices));
}
return possibleServiceConfigChoices;
}
@@ -470,7 +470,7 @@
if (!serviceConfigChoice.containsKey(SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY)) {
return null;
}
- return ServiceConfigUtil.checkStringList(
+ return JsonUtil.checkStringList(
JsonUtil.getList(serviceConfigChoice, SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY));
}
@@ -479,7 +479,7 @@
if (!serviceConfigChoice.containsKey(SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY)) {
return null;
}
- return ServiceConfigUtil.checkStringList(
+ return JsonUtil.checkStringList(
JsonUtil.getList(serviceConfigChoice, SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY));
}
diff --git a/core/src/main/java/io/grpc/internal/JsonUtil.java b/core/src/main/java/io/grpc/internal/JsonUtil.java
index 876d354..d25698c 100644
--- a/core/src/main/java/io/grpc/internal/JsonUtil.java
+++ b/core/src/main/java/io/grpc/internal/JsonUtil.java
@@ -115,4 +115,35 @@
}
return (Boolean) value;
}
+
+ /**
+ * Casts a list of unchecked JSON values to a list of checked objects in Java type.
+ * If the given list contains a value that is not a Map, throws an exception.
+ */
+ @SuppressWarnings("unchecked")
+ public static List<Map<String, ?>> checkObjectList(List<?> rawList) {
+ for (int i = 0; i < rawList.size(); i++) {
+ if (!(rawList.get(i) instanceof Map)) {
+ throw new ClassCastException(
+ String.format("value %s for idx %d in %s is not object", rawList.get(i), i, rawList));
+ }
+ }
+ return (List<Map<String, ?>>) rawList;
+ }
+
+ /**
+ * Casts a list of unchecked JSON values to a list of String. If the given list
+ * contains a value that is not a String, throws an exception.
+ */
+ @SuppressWarnings("unchecked")
+ public static List<String> checkStringList(List<?> rawList) {
+ for (int i = 0; i < rawList.size(); i++) {
+ if (!(rawList.get(i) instanceof String)) {
+ throw new ClassCastException(
+ String.format(
+ "value '%s' for idx %d in '%s' is not string", rawList.get(i), i, rawList));
+ }
+ }
+ return (List<String>) rawList;
+ }
}
diff --git a/core/src/main/java/io/grpc/internal/ServiceConfigUtil.java b/core/src/main/java/io/grpc/internal/ServiceConfigUtil.java
index 3033788..0f4318a 100644
--- a/core/src/main/java/io/grpc/internal/ServiceConfigUtil.java
+++ b/core/src/main/java/io/grpc/internal/ServiceConfigUtil.java
@@ -293,7 +293,7 @@
if (!methodConfig.containsKey(METHOD_CONFIG_NAME_KEY)) {
return null;
}
- return checkObjectList(JsonUtil.getList(methodConfig, METHOD_CONFIG_NAME_KEY));
+ return JsonUtil.checkObjectList(JsonUtil.getList(methodConfig, METHOD_CONFIG_NAME_KEY));
}
/**
@@ -345,7 +345,8 @@
if (!serviceConfig.containsKey(SERVICE_CONFIG_METHOD_CONFIG_KEY)) {
return null;
}
- return checkObjectList(JsonUtil.getList(serviceConfig, SERVICE_CONFIG_METHOD_CONFIG_KEY));
+ return JsonUtil
+ .checkObjectList(JsonUtil.getList(serviceConfig, SERVICE_CONFIG_METHOD_CONFIG_KEY));
}
/**
@@ -373,7 +374,7 @@
List<Map<String, ?>> lbConfigs = new ArrayList<>();
if (serviceConfig.containsKey(SERVICE_CONFIG_LOAD_BALANCING_CONFIG_KEY)) {
List<?> configs = JsonUtil.getList(serviceConfig, SERVICE_CONFIG_LOAD_BALANCING_CONFIG_KEY);
- for (Map<String, ?> config : checkObjectList(configs)) {
+ for (Map<String, ?> config : JsonUtil.checkObjectList(configs)) {
lbConfigs.add(config);
}
}
@@ -433,7 +434,7 @@
public static List<LbConfig> getChildPolicyFromXdsConfig(Map<String, ?> rawXdsConfig) {
List<?> rawChildPolicies = JsonUtil.getList(rawXdsConfig, XDS_CONFIG_CHILD_POLICY_KEY);
if (rawChildPolicies != null) {
- return unwrapLoadBalancingConfigList(checkObjectList(rawChildPolicies));
+ return unwrapLoadBalancingConfigList(JsonUtil.checkObjectList(rawChildPolicies));
}
return null;
}
@@ -445,7 +446,7 @@
public static List<LbConfig> getFallbackPolicyFromXdsConfig(Map<String, ?> rawXdsConfig) {
List<?> rawFallbackPolicies = JsonUtil.getList(rawXdsConfig, XDS_CONFIG_FALLBACK_POLICY_KEY);
if (rawFallbackPolicies != null) {
- return unwrapLoadBalancingConfigList(checkObjectList(rawFallbackPolicies));
+ return unwrapLoadBalancingConfigList(JsonUtil.checkObjectList(rawFallbackPolicies));
}
return null;
}
@@ -462,29 +463,6 @@
return JsonUtil.getString(serviceConfig, SERVICE_CONFIG_STICKINESS_METADATA_KEY);
}
- @SuppressWarnings("unchecked")
- static List<Map<String, ?>> checkObjectList(List<?> rawList) {
- for (int i = 0; i < rawList.size(); i++) {
- if (!(rawList.get(i) instanceof Map)) {
- throw new ClassCastException(
- String.format("value %s for idx %d in %s is not object", rawList.get(i), i, rawList));
- }
- }
- return (List<Map<String, ?>>) rawList;
- }
-
- @SuppressWarnings("unchecked")
- static List<String> checkStringList(List<?> rawList) {
- for (int i = 0; i < rawList.size(); i++) {
- if (!(rawList.get(i) instanceof String)) {
- throw new ClassCastException(
- String.format(
- "value '%s' for idx %d in '%s' is not string", rawList.get(i), i, rawList));
- }
- }
- return (List<String>) rawList;
- }
-
/**
* Parse from a string to produce a duration. Copy of
* {@link com.google.protobuf.util.Durations#parse}.