szetszwo commented on code in PR #6500:
URL: https://github.com/apache/ozone/pull/6500#discussion_r1589414033


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/server/JsonUtils.java:
##########
@@ -116,4 +121,26 @@ public static <T> List<T> readFromFile(File file, Class<T> 
itemType)
     }
   }
 
+  /**
+   * Reads JSON content from a Reader and deserializes it into an array of the
+   * specified type.
+   */
+  public static <T> T[] readArrayFromReader(Reader reader, Class<T[]> 
valueType)
+      throws IOException {
+    return MAPPER.readValue(reader, valueType);
+  }
+
+  /**
+   * Converts a JsonNode into a Java object of the specified type.
+   * @param node The JsonNode to convert.
+   * @param valueType The target class of the Java object.
+   * @param <T> The type of the Java object.
+   * @return A Java object of type T, populated with data from the JsonNode.
+   * @throws IOException
+   */
+  public static <T> T treeToValue(JsonNode node, Class<T> valueType)
+      throws IOException {
+    return MAPPER.treeToValue(node, valueType);
+  }

Review Comment:
   All the new methods in `JsonUtils` are unused.  Please remove them.



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueContainerMetadataInspector.java:
##########
@@ -181,55 +182,60 @@ public String process(ContainerData containerData, 
DatanodeStore store,
       return null;
     }
 
-    JsonObject containerJson = inspectContainer(kvData, store);
+    ObjectNode containerJson = inspectContainer(kvData, store);
     boolean correct = checkAndRepair(containerJson, kvData, store);
 
-    Gson gson = new GsonBuilder()
-        .setPrettyPrinting()
-        .serializeNulls()
-        .create();
-    String jsonReport = gson.toJson(containerJson);
-    if (log != null) {
-      if (correct) {
-        log.trace(jsonReport);
-      } else {
-        log.error(jsonReport);
+    ObjectMapper objectMapper = new ObjectMapper();
+    objectMapper.enable(
+        SerializationFeature.INDENT_OUTPUT);
+    String jsonReport = null;
+    try {
+      jsonReport = objectMapper.writeValueAsString(containerJson);
+      if (log != null) {
+        if (correct) {
+          log.trace(jsonReport);
+        } else {
+          log.error(jsonReport);
+        }
       }
+    } catch (Exception e) {
+      LOG.error("Error serializing container JSON", e);

Review Comment:
   Catch the exception in `JsonUtils`
   ```java
    
   +  public static String indentOutputWriteValueAsString(ObjectNode 
containerJson) {
   +    try {
   +      return INDENT_OUTPUT_MAPPER.writeValueAsString(containerJson);
   +    } catch (JsonProcessingException e) {
   +      return JsonUtil.toJsonString(e);
   +    }
   +  }
   ```



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueContainerMetadataInspector.java:
##########
@@ -181,55 +182,61 @@ public String process(ContainerData containerData, 
DatanodeStore store,
       return null;
     }
 
-    JsonObject containerJson = inspectContainer(kvData, store);
-    boolean correct = checkAndRepair(containerJson, kvData, store);
-
-    Gson gson = new GsonBuilder()
-        .setPrettyPrinting()
-        .serializeNulls()
-        .create();
-    String jsonReport = gson.toJson(containerJson);
-    if (log != null) {
-      if (correct) {
-        log.trace(jsonReport);
-      } else {
-        log.error(jsonReport);
+    ObjectNode containerJson = inspectContainer(kvData, store);
+    boolean correct = checkAndRepair(containerJson, kvData,
+        store);
+
+    ObjectMapper objectMapper = new ObjectMapper();
+    objectMapper.enable(
+        SerializationFeature.INDENT_OUTPUT);
+    String jsonReport = null;
+    try {
+      jsonReport = objectMapper.writeValueAsString(containerJson);

Review Comment:
   We may add a new `INDENT_OUTPUT_MAPPER` as below:
   ```java
   @@ -47,6 +48,7 @@ public final class JsonUtils {
      // before use.
      private static final ObjectMapper MAPPER;
      private static final ObjectWriter WRITER;
   +  private static final ObjectMapper INDENT_OUTPUT_MAPPER;
    
      static {
        MAPPER = new ObjectMapper()
   @@ -54,6 +56,8 @@ public final class JsonUtils {
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        WRITER = MAPPER.writerWithDefaultPrettyPrinter();
   +    INDENT_OUTPUT_MAPPER = new ObjectMapper()
   +        .enable(SerializationFeature.INDENT_OUTPUT);
      }
    
      private JsonUtils() {
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to