You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/08/15 11:58:47 UTC

(shardingsphere) branch master updated: Unify JSON handling with Jackson (#39470)

This is an automated email from the ASF dual-hosted git repository.

terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 8f49c52be2f Unify JSON handling with Jackson (#39470)
8f49c52be2f is described below

commit 8f49c52be2f3fe07b770d03e69ee28b9876443a5
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Aug 15 19:58:41 2026 +0800

    Unify JSON handling with Jackson (#39470)
---
 .../shardingsphere/infra/util/json/JsonUtils.java  |  9 ++++
 .../infra/util/json/JsonUtilsTest.java             | 18 ++++++++
 .../transport/MCPTransportJsonMapperFactory.java   | 13 +-----
 .../server/http/StreamableHttpMCPServerIT.java     |  3 +-
 .../metadata/GovernanceMetadataQueryService.java   |  3 +-
 mcp/registry/pom.xml                               |  5 +++
 .../mcp/registry/MCPRegistryMetadataCommand.java   |  3 +-
 .../registry/MCPRegistryMetadataCommandTest.java   |  3 +-
 .../e2e/mcp/llm/conversation/LLMMCPJsonValues.java |  4 +-
 .../support/transport/MCPInteractionPayloads.java  |  3 +-
 .../AbstractProcessMCPStdioInteractionClient.java  |  3 +-
 test/it/parser/pom.xml                             |  9 +++-
 .../type/GitHubTestParameterLoadStrategy.java      | 51 +++++++++-------------
 13 files changed, 77 insertions(+), 50 deletions(-)

diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/json/JsonUtils.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/json/JsonUtils.java
index 50e1822bd5a..40b07a66ef7 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/json/JsonUtils.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/json/JsonUtils.java
@@ -50,6 +50,15 @@ public final class JsonUtils {
         return result;
     }
     
+    /**
+     * Create object mapper with ShardingSphere JSON configurations.
+     *
+     * @return object mapper
+     */
+    public static ObjectMapper createObjectMapper() {
+        return MAPPER.copy();
+    }
+    
     /**
      * Serialize object as json string.
      *
diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/json/JsonUtilsTest.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/json/JsonUtilsTest.java
index 8956ca5c0ae..c48a868972e 100644
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/json/JsonUtilsTest.java
+++ b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/json/JsonUtilsTest.java
@@ -17,7 +17,11 @@
 
 package org.apache.shardingsphere.infra.util.json;
 
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
 import org.junit.jupiter.api.Test;
 
 import java.util.Collections;
@@ -25,9 +29,23 @@ import java.util.List;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class JsonUtilsTest {
     
+    @Test
+    void assertCreateObjectMapper() {
+        ObjectMapper actual = JsonUtils.createObjectMapper();
+        assertTrue(actual.getRegisteredModuleIds().contains("jackson-datatype-jsr310"));
+        assertTrue(actual.getRegisteredModuleIds().contains("com.fasterxml.jackson.datatype.jdk8.Jdk8Module"));
+        assertFalse(actual.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
+        assertFalse(actual.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+        assertThat(actual.getSerializationConfig().getDefaultPropertyInclusion().getValueInclusion(), is(Include.NON_NULL));
+        actual.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+        assertThat(JsonUtils.toJsonString(new Object()), is("{}"));
+    }
+    
     @Test
     void assertToJsonString() {
         assertThat(JsonUtils.toJsonString(Collections.singletonMap("k", "v")), is("{\"k\":\"v\"}"));
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/MCPTransportJsonMapperFactory.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/MCPTransportJsonMapperFactory.java
index cd6449d934c..4fc9e430734 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/MCPTransportJsonMapperFactory.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/MCPTransportJsonMapperFactory.java
@@ -17,15 +17,11 @@
 
 package org.apache.shardingsphere.mcp.bootstrap.transport;
 
-import com.fasterxml.jackson.annotation.JsonInclude.Include;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
-import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 import io.modelcontextprotocol.json.McpJsonMapper;
 import io.modelcontextprotocol.json.jackson2.JacksonMcpJsonMapper;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 
 /**
  * MCP transport JSON mapper factory.
@@ -39,12 +35,7 @@ public final class MCPTransportJsonMapperFactory {
      * @return MCP JSON mapper
      */
     public static McpJsonMapper create() {
-        ObjectMapper objectMapper = new ObjectMapper();
-        objectMapper.registerModule(new JavaTimeModule());
-        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
-        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
-        objectMapper.setSerializationInclusion(Include.NON_NULL);
-        return new JacksonMcpJsonMapper(objectMapper);
+        return new JacksonMcpJsonMapper(JsonUtils.createObjectMapper());
     }
     
 }
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java
index 7145ec9149b..c0ebd6693a8 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.mcp.bootstrap.transport.server.http;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 import org.apache.shardingsphere.mcp.api.transport.MCPTransportType;
 import org.apache.shardingsphere.mcp.bootstrap.config.HttpTransportConfiguration;
 import org.apache.shardingsphere.mcp.bootstrap.config.SessionAttributionSourceConfiguration;
@@ -45,7 +46,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class StreamableHttpMCPServerIT {
     
-    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final ObjectMapper OBJECT_MAPPER = JsonUtils.createObjectMapper();
     
     private static final String CONTENT_TYPE_HEADER = "Content-Type";
     
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/metadata/GovernanceMetadataQueryService.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/metadata/GovernanceMetadataQueryService.java
index 5ff00cff7b0..a5d8560ac51 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/metadata/GovernanceMetadataQueryService.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/metadata/GovernanceMetadataQueryService.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.mcp.core.metadata;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 import org.apache.shardingsphere.mcp.support.database.spi.MCPFeatureQueryFacade;
 import org.apache.shardingsphere.mcp.support.workflow.service.WorkflowSQLUtils;
 
@@ -37,7 +38,7 @@ import java.util.Objects;
  */
 public final class GovernanceMetadataQueryService {
     
-    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final ObjectMapper OBJECT_MAPPER = JsonUtils.createObjectMapper();
     
     private static final TypeReference<Object> JSON_VALUE_TYPE = new TypeReference<>() {
     };
diff --git a/mcp/registry/pom.xml b/mcp/registry/pom.xml
index ba58745f089..d69ee2b8861 100644
--- a/mcp/registry/pom.xml
+++ b/mcp/registry/pom.xml
@@ -32,6 +32,11 @@
             <artifactId>shardingsphere-infra-exception</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-infra-util</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
diff --git a/mcp/registry/src/main/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommand.java b/mcp/registry/src/main/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommand.java
index 0820581a036..d0179e885ed 100644
--- a/mcp/registry/src/main/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommand.java
+++ b/mcp/registry/src/main/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommand.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 
 import java.io.IOException;
 import java.net.URI;
@@ -40,7 +41,7 @@ import java.util.regex.Pattern;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class MCPRegistryMetadataCommand {
     
-    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
+    private static final ObjectMapper JSON_MAPPER = JsonUtils.createObjectMapper();
     
     private static final String REGISTRY_SCHEMA_URL = "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json";
     
diff --git a/mcp/registry/src/test/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommandTest.java b/mcp/registry/src/test/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommandTest.java
index 70e63c9bc01..8afb458cb92 100644
--- a/mcp/registry/src/test/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommandTest.java
+++ b/mcp/registry/src/test/java/org/apache/shardingsphere/mcp/registry/MCPRegistryMetadataCommandTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mcp.registry;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
@@ -37,7 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class MCPRegistryMetadataCommandTest {
     
-    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
+    private static final ObjectMapper JSON_MAPPER = JsonUtils.createObjectMapper();
     
     private static final String REGISTRY_SCHEMA_URL = "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json";
     
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPJsonValues.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPJsonValues.java
index 9733aa6139d..92d80600509 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPJsonValues.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPJsonValues.java
@@ -22,12 +22,14 @@ import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
+
 import java.util.Map;
 
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 final class LLMMCPJsonValues {
     
-    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final ObjectMapper OBJECT_MAPPER = JsonUtils.createObjectMapper();
     
     static Map<String, Object> parseToolArguments(final String argumentsJson) {
         try {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java
index 8b00866d207..b92da1f0c09 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java
@@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 
 import java.io.IOException;
 import java.util.LinkedHashMap;
@@ -33,7 +34,7 @@ import java.util.Map;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class MCPInteractionPayloads {
     
-    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final ObjectMapper OBJECT_MAPPER = JsonUtils.createObjectMapper();
     
     /**
      * Parse one MCP HTTP or STDIO JSON payload.
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java
index c1b3bc15968..6b5fa398856 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.test.e2e.mcp.support.transport.client;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 import org.apache.shardingsphere.test.e2e.mcp.support.artifact.MCPArtifactUtils;
 import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
 import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
@@ -52,7 +53,7 @@ abstract class AbstractProcessMCPStdioInteractionClient extends AbstractMCPInter
     
     private static final String INITIALIZE_REQUEST_ID = "init-1";
     
-    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final ObjectMapper OBJECT_MAPPER = JsonUtils.createObjectMapper();
     
     private final List<String> stdErrorMessages = new CopyOnWriteArrayList<>();
     
diff --git a/test/it/parser/pom.xml b/test/it/parser/pom.xml
index 832429450d2..85f9d982af3 100644
--- a/test/it/parser/pom.xml
+++ b/test/it/parser/pom.xml
@@ -31,6 +31,11 @@
     </properties>
     
     <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-infra-util</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-parser-sql-engine-core</artifactId>
@@ -199,8 +204,8 @@
             <scope>compile</scope>
         </dependency>
         <dependency>
-            <groupId>com.jayway.jsonpath</groupId>
-            <artifactId>json-path</artifactId>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
         </dependency>
     </dependencies>
 </project>
diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/strategy/type/GitHubTestParameterLoadStrategy.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/strategy/type/GitHubTestParameterLoadStrategy.java
index 4257f456bc7..05c8589b252 100644
--- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/strategy/type/GitHubTestParameterLoadStrategy.java
+++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/strategy/type/GitHubTestParameterLoadStrategy.java
@@ -17,10 +17,12 @@
 
 package org.apache.shardingsphere.test.it.sql.parser.external.loader.strategy.type;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.base.Strings;
-import com.jayway.jsonpath.DocumentContext;
-import com.jayway.jsonpath.JsonPath;
 import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
 import org.apache.shardingsphere.test.it.sql.parser.external.env.ExternalEnvironmentContext;
 import org.apache.shardingsphere.test.it.sql.parser.external.loader.strategy.ExternalTestParameterLoadStrategy;
 import org.apache.shardingsphere.test.it.sql.parser.external.loader.summary.FileSummary;
@@ -33,7 +35,6 @@ import java.net.URLConnection;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
-import java.util.List;
 import java.util.stream.Collectors;
 
 /**
@@ -43,6 +44,8 @@ public final class GitHubTestParameterLoadStrategy implements ExternalTestParame
     
     private static final String TOKEN_KEY = "it.github.token";
     
+    private static final ObjectMapper JSON_MAPPER = JsonUtils.createObjectMapper();
+    
     @Override
     public Collection<FileSummary> loadSQLCaseFileSummaries(final URI uri) {
         if (uri.toString().isEmpty()) {
@@ -52,20 +55,21 @@ public final class GitHubTestParameterLoadStrategy implements ExternalTestParame
         if (content.isEmpty()) {
             return Collections.emptyList();
         }
-        DocumentContext documentContext = JsonPath.parse(content);
-        if (documentContext.read("$") instanceof Collection) {
-            return getFileSummariesByArray(documentContext);
-        } else {
-            return getFileSummaries(documentContext);
-        }
+        JsonNode rootNode = parseContent(content);
+        return rootNode.isArray() ? getFileSummariesByArray(rootNode) : getFileSummaries(rootNode);
+    }
+    
+    @SneakyThrows(JsonProcessingException.class)
+    private JsonNode parseContent(final String content) {
+        return JSON_MAPPER.readTree(content);
     }
     
-    private Collection<FileSummary> getFileSummaries(final DocumentContext documentContext) {
+    private Collection<FileSummary> getFileSummaries(final JsonNode rootNode) {
         Collection<FileSummary> result = new LinkedList<>();
-        String fileName = documentContext.read("$.name");
-        String folderType = documentContext.read("$.type");
-        String downloadURL = documentContext.read("$.download_url");
-        String htmlURL = documentContext.read("$.html_url");
+        String fileName = rootNode.get("name").textValue();
+        String folderType = rootNode.get("type").textValue();
+        String downloadURL = rootNode.get("download_url").textValue();
+        String htmlURL = rootNode.get("html_url").textValue();
         if ("file".equals(folderType)) {
             result.add(new FileSummary(fileName, downloadURL));
         } else if ("dir".equals(folderType)) {
@@ -74,23 +78,10 @@ public final class GitHubTestParameterLoadStrategy implements ExternalTestParame
         return result;
     }
     
-    private Collection<FileSummary> getFileSummariesByArray(final DocumentContext documentContext) {
+    private Collection<FileSummary> getFileSummariesByArray(final JsonNode rootNode) {
         Collection<FileSummary> result = new LinkedList<>();
-        List<String> fileNames = documentContext.read("$..name");
-        List<String> folderTypes = documentContext.read("$..type");
-        List<String> downloadURLs = documentContext.read("$..download_url");
-        List<String> htmlURLs = documentContext.read("$..html_url");
-        int length = documentContext.read("$.length()");
-        for (int i = 0; i < length; i++) {
-            String fileName = fileNames.get(i);
-            String folderType = folderTypes.get(i);
-            String downloadURL = downloadURLs.get(i);
-            String htmlURL = htmlURLs.get(i);
-            if ("file".equals(folderType)) {
-                result.add(new FileSummary(fileName, downloadURL));
-            } else if ("dir".equals(folderType)) {
-                result.addAll(loadSQLCaseFileSummaries(URI.create(htmlURL)));
-            }
+        for (JsonNode each : rootNode) {
+            result.addAll(getFileSummaries(each));
         }
         return result;
     }