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