From 816a54a83b354cad169ce83b72a431093c764469 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Mon, 27 Sep 2021 13:26:04 -0700 Subject: [PATCH] api: Add doc snippet to convert types for defaultServiceConfig() Tested with Jackson's `new ObjectMapper().readValue(json, Map.class)`. Fixes #8300 --- .../main/java/io/grpc/ManagedChannelBuilder.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/api/src/main/java/io/grpc/ManagedChannelBuilder.java b/api/src/main/java/io/grpc/ManagedChannelBuilder.java index 98b22807cc..726f2e1ba5 100644 --- a/api/src/main/java/io/grpc/ManagedChannelBuilder.java +++ b/api/src/main/java/io/grpc/ManagedChannelBuilder.java @@ -555,6 +555,22 @@ public abstract class ManagedChannelBuilder> * *

If null is passed, then there will be no default service config. * + *

Your preferred JSON parser may not produce results in the format expected. For such cases, + * you can convert its output. For example, if your parser produces Integers and other Numbers + * in addition to Double: + * + *

{@code @SuppressWarnings("unchecked")
+   * private static Object convertNumbers(Object o) {
+   *   if (o instanceof Map) {
+   *     ((Map) o).replaceAll((k,v) -> convertNumbers(v));
+   *   } else if (o instanceof List) {
+   *     ((List) o).replaceAll(YourClass::convertNumbers);
+   *   } else if (o instanceof Number && !(o instanceof Double)) {
+   *     o = ((Number) o).doubleValue();
+   *   }
+   *   return o;
+   * }}
+ * * @throws IllegalArgumentException When the given serviceConfig is invalid or the current version * of grpc library can not parse it gracefully. The state of the builder is unchanged if * an exception is thrown.