mimaison commented on code in PR #13585:
URL: https://github.com/apache/kafka/pull/13585#discussion_r1261096752


##########
server-common/src/main/java/org/apache/kafka/server/util/json/DecodeJson.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.server.util.json;
+
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonNode;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+public interface DecodeJson<T> {
+    /**
+     * Decode the JSON node provided into an instance of `T`.
+     *
+     * @throws JsonMappingException if `node` cannot be decoded into `T`.
+     */
+    T decode(JsonNode node) throws JsonMappingException;
+
+    static JsonMappingException throwJsonMappingException(String expectedType, 
JsonNode node) {
+        return new JsonMappingException(null, String.format("Expected `%s` 
value, received %s", expectedType, node));
+    }
+
+    final class DecodeBoolean implements DecodeJson<Boolean> {
+        @Override
+        public Boolean decode(JsonNode node) throws JsonMappingException {
+            if (node.isBoolean()) {
+                return node.booleanValue();
+            }
+            throw throwJsonMappingException(Boolean.class.getSimpleName(), 
node);
+        }
+    }
+
+    final class DecodeDouble implements DecodeJson<Double> {
+        @Override
+        public Double decode(JsonNode node) throws JsonMappingException {
+            if (node.isDouble() || node.isLong() || node.isInt()) {
+                return node.doubleValue();
+            }
+            throw throwJsonMappingException(Double.class.getSimpleName(), 
node);
+        }
+    }
+
+    final class DecodeInteger implements DecodeJson<Integer> {
+        @Override
+        public Integer decode(JsonNode node) throws JsonMappingException {
+            if (node.isInt()) {
+                return node.intValue();
+            }
+            throw throwJsonMappingException(Integer.class.getSimpleName(), 
node);
+        }
+    }
+
+    final class DecodeLong implements DecodeJson<Long> {
+        @Override
+        public Long decode(JsonNode node) throws JsonMappingException {
+            if (node.isLong() || node.isInt()) {
+                return node.longValue();
+            }
+            throw throwJsonMappingException(Long.class.getSimpleName(), node);
+        }
+    }
+
+    final class DecodeString implements DecodeJson<String> {
+        @Override
+        public String decode(JsonNode node) throws JsonMappingException {
+            if (node.isTextual()) {
+                return node.textValue();
+            }
+            throw throwJsonMappingException(String.class.getSimpleName(), 
node);
+        }
+    }
+
+    static <E> DecodeJson<Optional<E>> decodeOptional(DecodeJson<E> 
decodeJson) {
+        return node -> {
+            if (node.isNull()) return Optional.empty();
+            return Optional.of(decodeJson.decode(node));
+        };
+    }
+
+    static <E> DecodeJson<List<E>> decodeList(DecodeJson<E> decodeJson) {
+        return node -> {
+            if (node.isArray()) {
+                List<E> result = new ArrayList<>();
+                Iterator<JsonNode> elements = node.elements();
+                while (elements.hasNext()) {
+                    try {
+                        result.add(decodeJson.decode(elements.next()));
+                    } catch (JsonMappingException e) {
+                        throw e;

Review Comment:
   Do we need this `catch` is we rethrow the same exception directly?
   Same in `decodeMap()` below



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to