You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by ji...@apache.org on 2023/12/04 06:01:08 UTC
(incubator-hugegraph-commons) branch rename-json created (now c9e9516)
This is an automated email from the ASF dual-hosted git repository.
jin pushed a change to branch rename-json
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git
at c9e9516 refact(common): rename jsonutil to avoid conflicts with server
This branch includes the following new commits:
new c9e9516 refact(common): rename jsonutil to avoid conflicts with server
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(incubator-hugegraph-commons) 01/01: refact(common): rename jsonutil to avoid conflicts with server
Posted by ji...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jin pushed a commit to branch rename-json
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git
commit c9e9516fffd0fde9d1dc7605eb8fe80370f81dd4
Author: imbajin <ji...@apache.org>
AuthorDate: Mon Dec 4 14:01:01 2023 +0800
refact(common): rename jsonutil to avoid conflicts with server
Also add some comment for it
---
.../apache/hugegraph/rest/AbstractRestClient.java | 4 +-
.../util/{JsonUtil.java => JsonUtilCommon.java} | 54 +++++++++++++++++-----
2 files changed, 45 insertions(+), 13 deletions(-)
diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
index 27d9add..00662bf 100644
--- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
+++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
@@ -37,7 +37,7 @@ import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang3.StringUtils;
-import org.apache.hugegraph.util.JsonUtil;
+import org.apache.hugegraph.util.JsonUtilCommon;
import org.jetbrains.annotations.NotNull;
import com.google.common.collect.ImmutableMap;
@@ -128,7 +128,7 @@ public abstract class AbstractRestClient implements RestClient {
if (body == null) {
bodyContent = "{}";
} else {
- bodyContent = JsonUtil.toJson(body);
+ bodyContent = JsonUtilCommon.toJson(body);
}
} else {
bodyContent = String.valueOf(body);
diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/util/JsonUtil.java b/hugegraph-common/src/main/java/org/apache/hugegraph/util/JsonUtilCommon.java
similarity index 63%
rename from hugegraph-common/src/main/java/org/apache/hugegraph/util/JsonUtil.java
rename to hugegraph-common/src/main/java/org/apache/hugegraph/util/JsonUtilCommon.java
index fc9586b..4c72b03 100644
--- a/hugegraph-common/src/main/java/org/apache/hugegraph/util/JsonUtil.java
+++ b/hugegraph-common/src/main/java/org/apache/hugegraph/util/JsonUtilCommon.java
@@ -17,46 +17,78 @@
package org.apache.hugegraph.util;
+import java.io.IOException;
+
+import org.apache.hugegraph.rest.SerializeException;
+
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
-import org.apache.hugegraph.rest.SerializeException;
-
-import java.io.IOException;
-public final class JsonUtil {
+/**
+ * Utility class for JSON operations.
+ */
+public final class JsonUtilCommon {
+ /**
+ * ObjectMapper instance used for JSON operations.
+ */
private static final ObjectMapper MAPPER = new ObjectMapper();
+ /**
+ * Registers a module with the ObjectMapper.
+ *
+ * @param module the module to register
+ */
public static void registerModule(Module module) {
MAPPER.registerModule(module);
}
+ /**
+ * Converts an object to a JSON string.
+ *
+ * @param object the object to convert
+ * @return the JSON string representation of the object
+ * @throws SerializeException if the object cannot be serialized
+ */
public static String toJson(Object object) {
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
- throw new SerializeException("Failed to serialize object '%s'",
- e, object);
+ throw new SerializeException("Failed to serialize object '%s'", e, object);
}
}
+ /**
+ * Converts a JSON string to an object of the specified class.
+ *
+ * @param json the JSON string
+ * @param clazz the class of the object
+ * @return the object represented by the JSON string
+ * @throws SerializeException if the JSON string cannot be deserialized
+ */
public static <T> T fromJson(String json, Class<T> clazz) {
try {
return MAPPER.readValue(json, clazz);
} catch (IOException e) {
- throw new SerializeException("Failed to deserialize json '%s'",
- e, json);
+ throw new SerializeException("Failed to deserialize json '%s'", e, json);
}
}
+ /**
+ * Converts a JsonNode to an object of the specified class.
+ *
+ * @param node the JsonNode
+ * @param clazz the class of the object
+ * @return the object represented by the JsonNode
+ * @throws SerializeException if the JsonNode cannot be deserialized
+ */
public static <T> T convertValue(JsonNode node, Class<T> clazz) {
try {
return MAPPER.convertValue(node, clazz);
} catch (IllegalArgumentException e) {
- throw new SerializeException("Failed to deserialize json node '%s'",
- e, node);
+ throw new SerializeException("Failed to deserialize json node '%s'", e, node);
}
}
-}
+}
\ No newline at end of file