This is an automated email from the ASF dual-hosted git repository.
jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push:
new f0c138cbb0 feature: unify Jackson/fastjson serialization. (#7760)
f0c138cbb0 is described below
commit f0c138cbb0ec0eb87006121ffc7fb7385818a2e1
Author: legendpei <[email protected]>
AuthorDate: Tue Jan 13 08:57:09 2026 +0800
feature: unify Jackson/fastjson serialization. (#7760)
---
changes/en-us/2.x.md | 3 +
changes/zh-cn/2.x.md | 3 +
compatible/pom.xml | 5 +
.../io/seata/saga/engine/StateMachineTests.java | 4 +-
.../seata/config/AbstractConfigurationTest.java | 1 +
integration-tx-api/pom.xml | 5 +
.../tx/api/interceptor/ActionContextUtil.java | 2 +-
.../api/interceptor/ActionInterceptorHandler.java | 2 +-
.../rm/tcc/api/BusinessActionContextUtil.java | 2 +-
{saga => json-common}/pom.xml | 36 +--
.../apache/seata/common/json/JsonSerializer.java | 91 ++++++
.../seata/common/json/JsonSerializerFactory.java | 45 +++
.../org/apache/seata/common/json/JsonUtil.java | 69 +++++
.../common/json/impl/FastjsonJsonSerializer.java | 144 +++++++++
.../seata/common/json/impl/GsonJsonSerializer.java | 116 +++++++
.../common/json/impl/JacksonJsonSerializer.java | 159 ++++++++++
.../org.apache.seata.common.json.JsonSerializer | 19 ++
.../common/json/FastjsonJsonSerializerTest.java | 278 +++++++++++++++++
.../seata/common/json/GsonJsonSerializerTest.java | 238 ++++++++++++++
.../common/json/JacksonJsonSerializerTest.java | 342 +++++++++++++++++++++
.../org/apache/seata/common/json/JsonUtilTest.java | 158 ++++++++++
json-common/src/test/resources/file.conf | 25 ++
json-common/src/test/resources/registry.conf | 34 ++
pom.xml | 1 +
rm-datasource/pom.xml | 5 +
.../seata/rm/datasource/ConnectionContext.java | 12 +-
.../org.apache.seata.common.json.JsonSerializer | 19 ++
.../org.apache.seata.common.json.JsonSerializer | 19 ++
saga/pom.xml | 6 +
.../engine/serializer/impl/ParamsSerializer.java | 20 +-
.../invoker/impl/SpringBeanServiceInvoker.java | 16 +-
.../parser/impl/StateMachineParserImpl.java | 14 +-
.../parser/utils/DesignerJsonTransformer.java | 9 +-
.../org.apache.seata.common.json.JsonSerializer | 19 ++
.../saga/statelang/parser/StateParserTests.java | 27 +-
.../parser/utils/DesignerJsonTransformerTest.java | 4 +-
.../seata/saga/engine/StateMachineTests.java | 4 +-
.../mockserver/StateMachineDBMockServerTests.java | 16 +-
38 files changed, 1889 insertions(+), 83 deletions(-)
diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 034bd47fe3..a46a3cae93 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -20,6 +20,8 @@ Add changes here for all PR submitted to the 2.x branch.
### feature:
+- [[#7760](https://github.com/apache/incubator-seata/pull/7760)] unify
Jackson/fastjson serialization
+
### bugfix:
@@ -47,6 +49,7 @@ Thanks to these contributors for their code commits. Please
report an unintended
- [slievrly](https://github.com/slievrly)
- [lokidundun](https://github.com/lokidundun)
+- [LegendPei](https://github.com/LegendPei)
Also, we receive many valuable issues, questions and advices from our
community. Thanks for you all.
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 859b4ce1f0..3feb5509e9 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -20,6 +20,8 @@
### feature:
+- [[#7760](https://github.com/apache/incubator-seata/pull/7760)]
统一Jackson/fastjson序列化器
+
### bugfix:
@@ -47,6 +49,7 @@
- [slievrly](https://github.com/slievrly)
- [lokidundun](https://github.com/lokidundun)
+- [LegendPei](https://github.com/LegendPei)
同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
diff --git a/compatible/pom.xml b/compatible/pom.xml
index b8e1d7e7ae..09ffaaba1c 100644
--- a/compatible/pom.xml
+++ b/compatible/pom.xml
@@ -40,6 +40,11 @@
<artifactId>seata-saga-engine</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.seata</groupId>
+ <artifactId>json-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-saga-engine-store</artifactId>
diff --git
a/compatible/src/test/java/io/seata/saga/engine/StateMachineTests.java
b/compatible/src/test/java/io/seata/saga/engine/StateMachineTests.java
index 1b9a4f1179..25cd0944b9 100644
--- a/compatible/src/test/java/io/seata/saga/engine/StateMachineTests.java
+++ b/compatible/src/test/java/io/seata/saga/engine/StateMachineTests.java
@@ -22,7 +22,7 @@ import io.seata.saga.engine.mock.DemoService.People;
import io.seata.saga.statelang.domain.DomainConstants;
import io.seata.saga.statelang.domain.ExecutionStatus;
import io.seata.saga.statelang.domain.StateMachineInstance;
-import org.apache.seata.saga.statelang.parser.JsonParserFactory;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -282,7 +282,7 @@ public class StateMachineTests {
map1.put("lilei4", people4);
people1.setChildrenMap(map1);
- String json =
JsonParserFactory.getJsonParser("jackson").toJsonString(people1, false, true);
+ String json =
JsonSerializerFactory.getSerializer("jackson").toJSONString(people1, false,
true);
System.out.println(json);
}
diff --git
a/config/seata-config-core/src/test/java/org/apache/seata/config/AbstractConfigurationTest.java
b/config/seata-config-core/src/test/java/org/apache/seata/config/AbstractConfigurationTest.java
index 560ed03d09..58ee45775f 100644
---
a/config/seata-config-core/src/test/java/org/apache/seata/config/AbstractConfigurationTest.java
+++
b/config/seata-config-core/src/test/java/org/apache/seata/config/AbstractConfigurationTest.java
@@ -150,6 +150,7 @@ class AbstractConfigurationTest {
String defaultValue = "default-value";
String value = configuration.getConfig("test.nonexistent.key",
defaultValue);
Assertions.assertEquals(defaultValue, value);
+
String valueCached = configuration.getConfig("test.nonexistent.key");
// due to configuration cache, may return default value instead of null
Assertions.assertNotNull(valueCached);
diff --git a/integration-tx-api/pom.xml b/integration-tx-api/pom.xml
index 69970790ae..39895c2d0a 100644
--- a/integration-tx-api/pom.xml
+++ b/integration-tx-api/pom.xml
@@ -57,6 +57,11 @@
<artifactId>seata-common</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>json-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
diff --git
a/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionContextUtil.java
b/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionContextUtil.java
index 380e3283ee..8d621b4269 100644
---
a/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionContextUtil.java
+++
b/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionContextUtil.java
@@ -17,10 +17,10 @@
package org.apache.seata.integration.tx.api.interceptor;
import org.apache.seata.common.exception.FrameworkException;
+import org.apache.seata.common.json.JsonUtil;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.ReflectionUtil;
import org.apache.seata.common.util.StringUtils;
-import org.apache.seata.integration.tx.api.util.JsonUtil;
import org.apache.seata.rm.tcc.api.BusinessActionContext;
import org.apache.seata.rm.tcc.api.BusinessActionContextParameter;
import org.apache.seata.rm.tcc.api.ParamType;
diff --git
a/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionInterceptorHandler.java
b/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionInterceptorHandler.java
index 1e47746799..d47047b71c 100644
---
a/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionInterceptorHandler.java
+++
b/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionInterceptorHandler.java
@@ -20,13 +20,13 @@ import org.apache.seata.common.Constants;
import org.apache.seata.common.exception.FrameworkException;
import org.apache.seata.common.exception.SkipCallbackWrapperException;
import org.apache.seata.common.executor.Callback;
+import org.apache.seata.common.json.JsonUtil;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.integration.tx.api.fence.DefaultCommonFenceHandler;
import org.apache.seata.integration.tx.api.fence.hook.TccHook;
import org.apache.seata.integration.tx.api.fence.hook.TccHookManager;
-import org.apache.seata.integration.tx.api.util.JsonUtil;
import org.apache.seata.rm.DefaultResourceManager;
import org.apache.seata.rm.tcc.api.BusinessActionContext;
import org.apache.seata.rm.tcc.api.BusinessActionContextParameter;
diff --git
a/integration-tx-api/src/main/java/org/apache/seata/rm/tcc/api/BusinessActionContextUtil.java
b/integration-tx-api/src/main/java/org/apache/seata/rm/tcc/api/BusinessActionContextUtil.java
index be65b12377..14c73cc41f 100644
---
a/integration-tx-api/src/main/java/org/apache/seata/rm/tcc/api/BusinessActionContextUtil.java
+++
b/integration-tx-api/src/main/java/org/apache/seata/rm/tcc/api/BusinessActionContextUtil.java
@@ -18,12 +18,12 @@ package org.apache.seata.rm.tcc.api;
import org.apache.seata.common.Constants;
import org.apache.seata.common.exception.FrameworkException;
+import org.apache.seata.common.json.JsonUtil;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.BranchStatus;
import org.apache.seata.integration.tx.api.interceptor.ActionContextUtil;
-import org.apache.seata.integration.tx.api.util.JsonUtil;
import org.apache.seata.rm.DefaultResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/saga/pom.xml b/json-common/pom.xml
similarity index 74%
copy from saga/pom.xml
copy to json-common/pom.xml
index 0d05dbb5fa..a1588df199 100644
--- a/saga/pom.xml
+++ b/json-common/pom.xml
@@ -26,20 +26,10 @@
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <artifactId>seata-saga</artifactId>
- <name>seata-saga ${project.version}</name>
- <description>saga top parent for Seata built with Maven</description>
-
- <modules>
- <module>seata-saga-processctrl</module>
- <module>seata-saga-statelang</module>
- <module>seata-saga-engine</module>
- <module>seata-saga-rm</module>
- <module>seata-saga-engine-store</module>
- <module>seata-saga-spring</module>
- <module>seata-saga-annotation</module>
- </modules>
+ <artifactId>json-common</artifactId>
+ <packaging>jar</packaging>
+ <name>json-common ${project.version}</name>
+ <description>jsonUtil for Seata modules</description>
<dependencies>
<dependency>
@@ -47,22 +37,28 @@
<artifactId>seata-common</artifactId>
<version>${project.version}</version>
</dependency>
-
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>seata-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<scope>provided</scope>
</dependency>
-
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
-
<dependency>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy-all</artifactId>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <scope>provided</scope>
</dependency>
</dependencies>
-</project>
+
+
+
+</project>
\ No newline at end of file
diff --git
a/json-common/src/main/java/org/apache/seata/common/json/JsonSerializer.java
b/json-common/src/main/java/org/apache/seata/common/json/JsonSerializer.java
new file mode 100644
index 0000000000..f4a77bbe59
--- /dev/null
+++ b/json-common/src/main/java/org/apache/seata/common/json/JsonSerializer.java
@@ -0,0 +1,91 @@
+/*
+ * 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.seata.common.json;
+
+import java.lang.reflect.Type;
+
+/**
+ * The json serializer interface.
+ */
+public interface JsonSerializer {
+
+ /**
+ * Serializes the specified object into its JSON string representation.
+ *
+ * @param object the object to serialize
+ * @return the JSON string representation of the object
+ */
+ String toJSONString(Object object);
+
+ /**
+ * Deserializes the specified JSON string into an object of the given
class type.
+ *
+ * @param text the JSON string to parse
+ * @param clazz the class of T
+ * @param <T> the type of the desired object
+ * @return the deserialized object of type T
+ */
+ <T> T parseObject(String text, Class<T> clazz);
+
+ /**
+ * Deserializes the specified JSON string into an object of the given type.
+ *
+ * @param text the JSON string to parse
+ * @param type the type to deserialize into
+ * @param <T> the type of the desired object
+ * @return the deserialized object of type T
+ */
+ <T> T parseObjectWithType(String text, Type type);
+
+ /**
+ * Checks whether the given JSON string uses auto type features (such as
type information for polymorphic deserialization).
+ *
+ * @param json the JSON string to check
+ * @return true if auto type is used in the JSON, false otherwise
+ */
+ boolean useAutoType(String json);
+
+ /**
+ * Serializes the specified object into its JSON string representation.
+ *
+ * @param o the object to serialize
+ * @param prettyPrint whether to format the JSON string for readability
+ * @return the JSON string representation of the object
+ */
+ String toJSONString(Object o, boolean prettyPrint);
+
+ /**
+ * Serializes the specified object into its JSON string representation,
with options to ignore auto type and pretty print.
+ *
+ * @param o the object to serialize
+ * @param ignoreAutoType whether to ignore auto type information during
serialization
+ * @param prettyPrint whether to format the JSON string for readability
+ * @return the JSON string representation of the object
+ */
+ String toJSONString(Object o, boolean ignoreAutoType, boolean prettyPrint);
+
+ /**
+ * Deserializes the specified JSON string into an object of the given
class type, with an option to ignore auto type information.
+ *
+ * @param json the JSON string to parse
+ * @param type the class of T
+ * @param ignoreAutoType whether to ignore auto type information during
deserialization
+ * @param <T> the type of the desired object
+ * @return the deserialized object of type T
+ */
+ <T> T parseObject(String json, Class<T> type, boolean ignoreAutoType);
+}
diff --git
a/json-common/src/main/java/org/apache/seata/common/json/JsonSerializerFactory.java
b/json-common/src/main/java/org/apache/seata/common/json/JsonSerializerFactory.java
new file mode 100644
index 0000000000..bc2cee5847
--- /dev/null
+++
b/json-common/src/main/java/org/apache/seata/common/json/JsonSerializerFactory.java
@@ -0,0 +1,45 @@
+/*
+ * 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.seata.common.json;
+
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.CollectionUtils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class JsonSerializerFactory {
+
+ private static final String DEFAULT_SERIALIZER = "jackson";
+
+ private static final Map<String, JsonSerializer> INSTANCES = new
ConcurrentHashMap<>();
+
+ private JsonSerializerFactory() {}
+
+ /**
+ * Get JsonSerializer instance by name.
+ *
+ * @param name the serializer name (e.g., "fastjson", "jackson", "gson")
+ * @return the JsonSerializer instance
+ */
+ public static JsonSerializer getSerializer(String name) {
+ final String serializerName =
Optional.ofNullable(name).orElse(DEFAULT_SERIALIZER);
+ return CollectionUtils.computeIfAbsent(
+ INSTANCES, serializerName, key ->
EnhancedServiceLoader.load(JsonSerializer.class, key));
+ }
+}
diff --git
a/json-common/src/main/java/org/apache/seata/common/json/JsonUtil.java
b/json-common/src/main/java/org/apache/seata/common/json/JsonUtil.java
new file mode 100644
index 0000000000..33dc6b16a8
--- /dev/null
+++ b/json-common/src/main/java/org/apache/seata/common/json/JsonUtil.java
@@ -0,0 +1,69 @@
+/*
+ * 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.seata.common.json;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.common.Constants;
+import org.apache.seata.common.DefaultValues;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.config.ConfigurationFactory;
+
+import java.util.Objects;
+
+/**
+ * Unified JSON utility class
+ */
+public final class JsonUtil {
+
+ private static final String CONFIG_JSON_PARSER_NAME =
ConfigurationFactory.getInstance()
+ .getConfig(
+
ConfigurationKeys.TCC_BUSINESS_ACTION_CONTEXT_JSON_PARSER_NAME,
+
DefaultValues.DEFAULT_TCC_BUSINESS_ACTION_CONTEXT_JSON_PARSER);
+
+ private static final JsonSerializer DEFAULT_SERIALIZER =
+ JsonSerializerFactory.getSerializer(CONFIG_JSON_PARSER_NAME);
+
+ /**
+ * Serialize the given object to JSON string
+ *
+ * @param object the object to serialize
+ * @return the JSON string representation
+ * @throws JsonParseException if serialization fails
+ */
+ public static String toJSONString(Object object) {
+ return DEFAULT_SERIALIZER.toJSONString(object);
+ }
+
+ /**
+ * Deserialize the given JSON string to an object of the specified class
+ *
+ * @param <T> the type of the object
+ * @param text the JSON string
+ * @param clazz the class to deserialize to
+ * @return the deserialized object
+ * @throws JsonParseException if deserialization fails
+ */
+ public static <T> T parseObject(String text, Class<T> clazz) {
+ if (Objects.isNull(text) || Objects.isNull(clazz)) {
+ return null;
+ }
+ String jsonParseName =
text.startsWith(Constants.JACKSON_JSON_TEXT_PREFIX)
+ ? Constants.JACKSON_JSON_PARSER_NAME
+ : CONFIG_JSON_PARSER_NAME;
+ return
JsonSerializerFactory.getSerializer(jsonParseName).parseObject(text, clazz);
+ }
+}
diff --git
a/json-common/src/main/java/org/apache/seata/common/json/impl/FastjsonJsonSerializer.java
b/json-common/src/main/java/org/apache/seata/common/json/impl/FastjsonJsonSerializer.java
new file mode 100644
index 0000000000..cac1cf7ac7
--- /dev/null
+++
b/json-common/src/main/java/org/apache/seata/common/json/impl/FastjsonJsonSerializer.java
@@ -0,0 +1,144 @@
+/*
+ * 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.seata.common.json.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.parser.Feature;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.lang.reflect.Type;
+
+/**
+ * FastJSON implementation of JsonSerializer
+ */
+@LoadLevel(name = FastjsonJsonSerializer.NAME)
+public class FastjsonJsonSerializer implements JsonSerializer {
+
+ private static final SerializerFeature[] SERIALIZER_FEATURES = new
SerializerFeature[] {
+ SerializerFeature.DisableCircularReferenceDetect,
+ SerializerFeature.WriteDateUseDateFormat,
+ SerializerFeature.WriteClassName
+ };
+
+ private static final SerializerFeature[] SERIALIZER_FEATURES_PRETTY = new
SerializerFeature[] {
+ SerializerFeature.DisableCircularReferenceDetect,
+ SerializerFeature.WriteDateUseDateFormat,
+ SerializerFeature.WriteClassName,
+ SerializerFeature.PrettyFormat
+ };
+
+ private static final SerializerFeature[] FEATURES_PRETTY = new
SerializerFeature[] {
+ SerializerFeature.DisableCircularReferenceDetect,
+ SerializerFeature.WriteDateUseDateFormat,
+ SerializerFeature.PrettyFormat
+ };
+
+ private static final Feature[] READER_FEATURES_SUPPORT_AUTO_TYPE =
+ new Feature[] {Feature.SupportAutoType, Feature.OrderedField};
+
+ private static final Feature[] READER_FEATURES_IGNORE_AUTO_TYPE =
+ new Feature[] {Feature.IgnoreAutoType, Feature.OrderedField};
+
+ public static final String NAME = "fastjson";
+
+ @Override
+ public String toJSONString(Object object) {
+ try {
+ return JSON.toJSONString(object);
+ } catch (Exception e) {
+ throw new JsonParseException("FastJSON serialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObject(String text, Class<T> clazz) {
+ if (text == null || clazz == null) {
+ return null;
+ }
+ try {
+ return JSON.parseObject(text, clazz);
+ } catch (Exception e) {
+ throw new JsonParseException("FastJSON deserialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObjectWithType(String text, Type type) {
+ if (text == null || type == null) {
+ return null;
+ }
+ try {
+ return JSON.parseObject(text, type);
+ } catch (Exception e) {
+ throw new JsonParseException("FastJSON deserialize error", e);
+ }
+ }
+
+ // advanced methods for Saga
+ @Override
+ public boolean useAutoType(String json) {
+ return json != null && json.contains("\"@type\"");
+ }
+
+ @Override
+ public String toJSONString(Object object, boolean prettyPrint) {
+ return toJSONString(object, false, prettyPrint);
+ }
+
+ @Override
+ public String toJSONString(Object object, boolean ignoreAutoType, boolean
prettyPrint) {
+ try {
+ if (prettyPrint) {
+ if (ignoreAutoType) {
+ return JSON.toJSONString(object, FEATURES_PRETTY);
+ } else {
+ return JSON.toJSONString(object,
SERIALIZER_FEATURES_PRETTY);
+ }
+ } else {
+ if (ignoreAutoType) {
+ return JSON.toJSONString(object);
+ } else {
+ return JSON.toJSONString(object, SERIALIZER_FEATURES);
+ }
+ }
+ } catch (Exception e) {
+ throw new JsonParseException("FastJSON serialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObject(String text, Class<T> type, boolean
ignoreAutoType) {
+ if (text == null || type == null) {
+ return null;
+ }
+ try {
+ if ("[]".equals(text)) {
+ return (T) new java.util.ArrayList<>();
+ }
+ if (ignoreAutoType) {
+ return JSON.parseObject(text, type,
READER_FEATURES_IGNORE_AUTO_TYPE);
+ } else {
+ return JSON.parseObject(text, type,
READER_FEATURES_SUPPORT_AUTO_TYPE);
+ }
+ } catch (Exception e) {
+ throw new JsonParseException("FastJSON deserialize error", e);
+ }
+ }
+}
diff --git
a/json-common/src/main/java/org/apache/seata/common/json/impl/GsonJsonSerializer.java
b/json-common/src/main/java/org/apache/seata/common/json/impl/GsonJsonSerializer.java
new file mode 100644
index 0000000000..c04193a838
--- /dev/null
+++
b/json-common/src/main/java/org/apache/seata/common/json/impl/GsonJsonSerializer.java
@@ -0,0 +1,116 @@
+/*
+ * 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.seata.common.json.impl;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+
+/**
+ * Gson implementation of JsonSerializer
+ */
+@LoadLevel(name = GsonJsonSerializer.NAME)
+public class GsonJsonSerializer implements JsonSerializer {
+
+ private final Gson gson = new GsonBuilder()
+ .excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT)
+ .create();
+
+ private final Gson gsonPretty = new GsonBuilder()
+ .excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT)
+ .setPrettyPrinting()
+ .create();
+
+ public static final String NAME = "gson";
+
+ @Override
+ public String toJSONString(Object object) {
+ try {
+ return gson.toJson(object);
+ } catch (Exception e) {
+ throw new JsonParseException("Gson serialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObject(String text, Class<T> clazz) {
+ if (text == null || clazz == null) {
+ return null;
+ }
+ try {
+ return gson.fromJson(text, clazz);
+ } catch (Exception e) {
+ throw new JsonParseException("Gson deserialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObjectWithType(String text, Type type) {
+ if (text == null || type == null) {
+ return null;
+ }
+ try {
+ return gson.fromJson(text, type);
+ } catch (Exception e) {
+ throw new JsonParseException("Gson deserialize error", e);
+ }
+ }
+
+ // advanced methods for Saga
+ @Override
+ public boolean useAutoType(String json) {
+ return false;
+ }
+
+ @Override
+ public String toJSONString(Object object, boolean prettyPrint) {
+ return toJSONString(object, false, prettyPrint);
+ }
+
+ @Override
+ public String toJSONString(Object object, boolean ignoreAutoType, boolean
prettyPrint) {
+ try {
+ if (prettyPrint) {
+ return gsonPretty.toJson(object);
+ } else {
+ return gson.toJson(object);
+ }
+ } catch (Exception e) {
+ throw new JsonParseException("Gson serialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObject(String text, Class<T> type, boolean
ignoreAutoType) {
+ if (text == null || type == null) {
+ return null;
+ }
+ try {
+ if ("[]".equals(text)) {
+ return (T) new java.util.ArrayList<>();
+ }
+ return gson.fromJson(text, type);
+ } catch (Exception e) {
+ throw new JsonParseException("Gson deserialize error", e);
+ }
+ }
+}
diff --git
a/json-common/src/main/java/org/apache/seata/common/json/impl/JacksonJsonSerializer.java
b/json-common/src/main/java/org/apache/seata/common/json/impl/JacksonJsonSerializer.java
new file mode 100644
index 0000000000..62213320fb
--- /dev/null
+++
b/json-common/src/main/java/org/apache/seata/common/json/impl/JacksonJsonSerializer.java
@@ -0,0 +1,159 @@
+/*
+ * 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.seata.common.json.impl;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Jackson implementation of JsonSerializer
+ */
+@LoadLevel(name = JacksonJsonSerializer.NAME)
+public class JacksonJsonSerializer implements JsonSerializer {
+ public static final String NAME = "jackson";
+
+ private final ObjectMapper defaultObjectMapper;
+
+ private final ObjectMapper objectMapperWithAutoType;
+
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ public JacksonJsonSerializer() {
+ this.defaultObjectMapper = new ObjectMapper()
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false)
+ .disableDefaultTyping()
+ .enable(MapperFeature.PROPAGATE_TRANSIENT_MARKER)
+ .setSerializationInclusion(JsonInclude.Include.NON_NULL);
+
+ this.objectMapperWithAutoType = new ObjectMapper()
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false)
+ .enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL,
"@type")
+ .enable(MapperFeature.PROPAGATE_TRANSIENT_MARKER)
+ .setSerializationInclusion(JsonInclude.Include.NON_NULL);
+
+
this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ this.mapper.activateDefaultTyping(
+ this.mapper.getPolymorphicTypeValidator(),
+ ObjectMapper.DefaultTyping.NON_FINAL,
+ JsonTypeInfo.As.PROPERTY);
+
this.mapper.setConfig(this.mapper.getSerializationConfig().with(MapperFeature.PROPAGATE_TRANSIENT_MARKER));
+
this.mapper.setConfig(this.mapper.getDeserializationConfig().with(MapperFeature.PROPAGATE_TRANSIENT_MARKER));
+ this.mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ }
+
+ @Override
+ public String toJSONString(Object object) {
+ try {
+ return mapper.writeValueAsString(object);
+ } catch (JsonProcessingException e) {
+ throw new JsonParseException("Jackson serialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObject(String text, Class<T> clazz) {
+ if (text == null || clazz == null) {
+ return null;
+ }
+ try {
+ return mapper.readValue(text, clazz);
+ } catch (IOException e) {
+ throw new JsonParseException("Jackson deserialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObjectWithType(String text, Type type) {
+ if (text == null || type == null) {
+ return null;
+ }
+ try {
+ return objectMapperWithAutoType.readValue(text,
objectMapperWithAutoType.constructType(type));
+ } catch (IOException e) {
+ throw new JsonParseException("Jackson deserialize error", e);
+ }
+ }
+
+ // advanced methods for Saga
+ @Override
+ public boolean useAutoType(String json) {
+ return json != null && json.contains("\"@type\"");
+ }
+
+ @Override
+ public String toJSONString(Object o, boolean prettyPrint) {
+ return toJSONString(o, false, prettyPrint);
+ }
+
+ @Override
+ public String toJSONString(Object o, boolean ignoreAutoType, boolean
prettyPrint) {
+ try {
+ if (o instanceof List && ((List<?>) o).isEmpty()) {
+ return "[]";
+ }
+ if (prettyPrint) {
+ if (ignoreAutoType) {
+ return
defaultObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
+ } else {
+ return objectMapperWithAutoType
+ .writerWithDefaultPrettyPrinter()
+ .writeValueAsString(o);
+ }
+ } else {
+ if (ignoreAutoType) {
+ return defaultObjectMapper.writeValueAsString(o);
+ } else {
+ return objectMapperWithAutoType.writeValueAsString(o);
+ }
+ }
+ } catch (JsonProcessingException e) {
+ throw new JsonParseException("Jackson serialize error", e);
+ }
+ }
+
+ @Override
+ public <T> T parseObject(String json, Class<T> type, boolean
ignoreAutoType) {
+ if (json == null || type == null) {
+ return null;
+ }
+ try {
+ if ("[]".equals(json)) {
+ return (T) new ArrayList<>(0);
+ }
+ if (ignoreAutoType) {
+ return defaultObjectMapper.readValue(json, type);
+ } else {
+ return objectMapperWithAutoType.readValue(json, type);
+ }
+ } catch (IOException e) {
+ throw new JsonParseException("Jackson deserialize error", e);
+ }
+ }
+}
diff --git
a/json-common/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
b/json-common/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
new file mode 100644
index 0000000000..f723b14b68
--- /dev/null
+++
b/json-common/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+org.apache.seata.common.json.impl.FastjsonJsonSerializer
+org.apache.seata.common.json.impl.JacksonJsonSerializer
+org.apache.seata.common.json.impl.GsonJsonSerializer
\ No newline at end of file
diff --git
a/json-common/src/test/java/org/apache/seata/common/json/FastjsonJsonSerializerTest.java
b/json-common/src/test/java/org/apache/seata/common/json/FastjsonJsonSerializerTest.java
new file mode 100644
index 0000000000..ff364e059b
--- /dev/null
+++
b/json-common/src/test/java/org/apache/seata/common/json/FastjsonJsonSerializerTest.java
@@ -0,0 +1,278 @@
+/*
+ * 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.seata.common.json;
+
+import com.alibaba.fastjson.TypeReference;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.impl.FastjsonJsonSerializer;
+import org.apache.seata.common.json.impl.JacksonJsonSerializer;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class FastjsonJsonSerializerTest {
+
+ private JsonSerializer jsonSerializer;
+
+ @BeforeEach
+ void setUp() {
+ // Use factory to get FastJSON serializer by name
+ jsonSerializer = JsonSerializerFactory.getSerializer("fastjson");
+ }
+
+ @Test
+ public void testToJSONString_basicObject() {
+ TestObject obj = new TestObject("test", 123);
+ String json = jsonSerializer.toJSONString(obj);
+
+ assertThat(json).contains("\"name\":\"test\"");
+ assertThat(json).contains("\"value\":123");
+ }
+
+ @Test
+ public void testParseObject_basicObject() {
+ String json = "{\"name\":\"test\",\"value\":123}";
+ TestObject obj = jsonSerializer.parseObject(json, TestObject.class);
+
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("test");
+ assertThat(obj.getValue()).isEqualTo(123);
+ }
+
+ @Test
+ public void testToJSONString_and_parseObject() {
+ TestObject original = new TestObject("school", 456);
+ String json = jsonSerializer.toJSONString(original);
+ TestObject restored = jsonSerializer.parseObject(json,
TestObject.class);
+
+ assertThat(restored.getName()).isEqualTo(original.getName());
+ assertThat(restored.getValue()).isEqualTo(original.getValue());
+ }
+
+ @Test
+ public void testUseAutoType_withType() {
+ String json = "{\"@type\":\"some.type\",\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(json);
+ assertThat(hasAutoType).isTrue();
+ }
+
+ @Test
+ public void testUseAutoType_withoutType() {
+ String json = "{\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(json);
+ assertThat(hasAutoType).isFalse();
+ }
+
+ @Test
+ public void testToJSONString_prettyPrint() {
+ TestObject obj = new TestObject("pretty", 789);
+ String prettyJson = jsonSerializer.toJSONString(obj, true);
+
+ assertThat(prettyJson).contains("\n");
+ assertThat(prettyJson).contains("\t");
+ }
+
+ @Test
+ public void testToJSONString_ignoreAutoType() {
+ TestObject obj = new TestObject("noType", 111);
+ String jsonWithoutType = jsonSerializer.toJSONString(obj, true, true);
+
+ assertThat(jsonWithoutType).doesNotContain("@type");
+ }
+
+ @Test
+ public void testParseObject_ignoreAutoType() {
+ String json = jsonSerializer.toJSONString(new TestObject("ignored",
222));
+
+ TestObject obj = jsonSerializer.parseObject(json, TestObject.class,
true);
+
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("ignored");
+ assertThat(obj.getValue()).isEqualTo(222);
+ }
+
+ @Test
+ public void testEmptyList_serialization() {
+ List<String> emptyList = new ArrayList<>();
+ String json = jsonSerializer.toJSONString(emptyList, false, false);
+ assertThat(json).isEqualTo("[]");
+ }
+
+ @Test
+ public void testEmptyList_deserialization() {
+ String json = "[]";
+ List<?> list = jsonSerializer.parseObject(json, List.class, false);
+ assertThat(list).isEmpty();
+ }
+
+ @Test
+ public void testNullInput_toJSONString() {
+ String json = jsonSerializer.toJSONString(null);
+ assertThat(json).isEqualTo("null");
+ }
+
+ @Test
+ public void testNullInput_parseObject() {
+ TestObject obj = jsonSerializer.parseObject(null, TestObject.class);
+ assertThat(obj).isNull();
+
+ TestObject obj2 = jsonSerializer.parseObject("{}", null);
+ assertThat(obj2).isNull();
+ }
+
+ @Test
+ public void testParseObject_invalidJson() {
+ assertThatThrownBy(() -> jsonSerializer.parseObject("{invalid json}",
TestObject.class))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("FastJSON deserialize error");
+ }
+
+ @Test
+ public void testFactoryReturnsCorrectInstance() {
+ JsonSerializer serializer =
JsonSerializerFactory.getSerializer("fastjson");
+ assertThat(serializer).isNotNull();
+ assertThat(serializer).isInstanceOf(FastjsonJsonSerializer.class);
+ }
+
+ @Test
+ public void testFactoryReturnsDefaultInstance() {
+ JsonSerializer serializer = JsonSerializerFactory.getSerializer(null);
+ assertThat(serializer).isNotNull();
+ assertThat(serializer).isInstanceOf(JacksonJsonSerializer.class);
+ }
+
+ @Test
+ public void testToJSONString_emptyList() {
+ List<String> emptyList = new ArrayList<>();
+ String json = jsonSerializer.toJSONString(emptyList, false, false);
+ assertThat(json).isEqualTo("[]");
+ }
+
+ @Test
+ public void testToJSONString_withAutoType() {
+ TestObject obj = new TestObject("withType", 789);
+ String jsonWithAutoType = jsonSerializer.toJSONString(obj, false,
false);
+
+ assertThat(jsonWithAutoType).contains("@type");
+ }
+
+ @Test
+ public void testParseObject_nullJson() {
+ assertThat(jsonSerializer.parseObject(null, TestObject.class,
false)).isNull();
+ }
+
+ @Test
+ public void testParseObject_emptyList() {
+ String json = "[]";
+ List<?> list = jsonSerializer.parseObject(json, List.class, false);
+ assertThat(list).isEmpty();
+ }
+
+ @Test
+ public void testParseObject_withAutoType() {
+ TestObject original = new TestObject("autoTypeTest", 999);
+ String jsonWithAutoType = jsonSerializer.toJSONString(original, false,
false);
+
+ TestObject restored = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, false);
+
+ assertThat(restored).isNotNull();
+ assertThat(restored.getName()).isEqualTo("autoTypeTest");
+ assertThat(restored.getValue()).isEqualTo(999);
+ }
+
+ @Test
+ public void testParseObject_withType() {
+
+ String json = "{\"name\":\"test\",\"value\":123}";
+ Type type = new TypeReference<TestObject>() {}.getType();
+
+ TestObject obj = jsonSerializer.parseObjectWithType(json, type);
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("test");
+ assertThat(obj.getValue()).isEqualTo(123);
+
+ String listJson =
"[{\"name\":\"item1\",\"value\":1},{\"name\":\"item2\",\"value\":2}]";
+ Type listType = new TypeReference<List<TestObject>>() {}.getType();
+
+ List<TestObject> list = jsonSerializer.parseObjectWithType(listJson,
listType);
+ assertThat(list).hasSize(2);
+ assertThat(list.get(0).getName()).isEqualTo("item1");
+ assertThat(list.get(1).getValue()).isEqualTo(2);
+ }
+
+ @Test
+ public void testParseObject_withIgnoreAutoType() {
+
+ String jsonWithAutoType = jsonSerializer.toJSONString(new
TestObject("ignored", 222));
+
+ TestObject objIgnore = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, true);
+ assertThat(objIgnore).isNotNull();
+ assertThat(objIgnore.getName()).isEqualTo("ignored");
+ assertThat(objIgnore.getValue()).isEqualTo(222);
+
+ TestObject objNoIgnore = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, false);
+ assertThat(objNoIgnore).isNotNull();
+ assertThat(objNoIgnore.getName()).isEqualTo("ignored");
+ assertThat(objNoIgnore.getValue()).isEqualTo(222);
+
+ List<?> emptyListResult = jsonSerializer.parseObject("", List.class,
false);
+ assertThat(emptyListResult).isNull();
+
+ List<?> emptyList = jsonSerializer.parseObject("[]", List.class,
false);
+ assertThat(emptyList).isEmpty();
+
+ assertThat(jsonSerializer.parseObject(null, TestObject.class,
false)).isNull();
+
+ assertThatThrownBy(() -> jsonSerializer.parseObject("{invalid json}",
TestObject.class, false))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("FastJSON deserialize error");
+ }
+
+ public static class TestObject {
+ private String name;
+ private int value;
+
+ public TestObject() {}
+
+ public TestObject(String name, int value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+ }
+}
diff --git
a/json-common/src/test/java/org/apache/seata/common/json/GsonJsonSerializerTest.java
b/json-common/src/test/java/org/apache/seata/common/json/GsonJsonSerializerTest.java
new file mode 100644
index 0000000000..b2b432acf3
--- /dev/null
+++
b/json-common/src/test/java/org/apache/seata/common/json/GsonJsonSerializerTest.java
@@ -0,0 +1,238 @@
+/*
+ * 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.seata.common.json;
+
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.impl.GsonJsonSerializer;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class GsonJsonSerializerTest {
+
+ private JsonSerializer jsonSerializer;
+
+ @BeforeEach
+ void setUp() {
+ // Use factory to get Gson serializer by name
+ jsonSerializer = JsonSerializerFactory.getSerializer("gson");
+ }
+
+ @Test
+ public void testToJSONString_basicObject() {
+ TestObject obj = new TestObject("test", 123);
+ String json = jsonSerializer.toJSONString(obj);
+
+ assertThat(json).contains("\"name\":\"test\"");
+ assertThat(json).contains("\"value\":123");
+ assertThat(json).doesNotContain("@type");
+ }
+
+ @Test
+ public void testParseObject_basicObject() {
+ String json = "{\"name\":\"test\",\"value\":123}";
+ TestObject obj = jsonSerializer.parseObject(json, TestObject.class);
+
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("test");
+ assertThat(obj.getValue()).isEqualTo(123);
+ }
+
+ @Test
+ public void testToJSONString_and_parseObject() {
+ TestObject original = new TestObject("school", 456);
+ String json = jsonSerializer.toJSONString(original);
+ TestObject restored = jsonSerializer.parseObject(json,
TestObject.class);
+
+ assertThat(restored.getName()).isEqualTo(original.getName());
+ assertThat(restored.getValue()).isEqualTo(original.getValue());
+ }
+
+ @Test
+ public void testUseAutoType_withType() {
+ String json = "{\"@type\":\"some.type\",\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(json);
+ assertThat(hasAutoType).isFalse();
+ }
+
+ @Test
+ public void testUseAutoType_withoutType() {
+ String json = "{\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(json);
+ assertThat(hasAutoType).isFalse();
+ }
+
+ @Test
+ public void testToJSONString_prettyPrint() {
+ TestObject obj = new TestObject("pretty", 789);
+ String prettyJson = jsonSerializer.toJSONString(obj, true);
+
+ assertThat(prettyJson).contains("\n");
+ assertThat(prettyJson).contains(" \"");
+ }
+
+ @Test
+ public void testToJSONString_ignoreAutoType() {
+ TestObject obj = new TestObject("noType", 111);
+ String jsonWithoutType = jsonSerializer.toJSONString(obj, true, true);
+
+ assertThat(jsonWithoutType).doesNotContain("@type");
+ }
+
+ @Test
+ public void testParseObject_ignoreAutoType() {
+ TestObject obj = new TestObject("ignored", 222);
+ String json = jsonSerializer.toJSONString(obj);
+
+ TestObject restored = jsonSerializer.parseObject(json,
TestObject.class, true);
+
+ assertThat(restored).isNotNull();
+ assertThat(restored.getName()).isEqualTo("ignored");
+ assertThat(restored.getValue()).isEqualTo(222);
+ }
+
+ @Test
+ public void testEmptyList_serialization() {
+ List<String> emptyList = new ArrayList<>();
+ String json = jsonSerializer.toJSONString(emptyList, false, false);
+ assertThat(json).isEqualTo("[]");
+ }
+
+ @Test
+ public void testEmptyList_deserialization() {
+ String json = "[]";
+ List<?> list = jsonSerializer.parseObject(json, List.class, false);
+ assertThat(list).isEmpty();
+ }
+
+ @Test
+ public void testNullInput_toJSONString() {
+ String json = jsonSerializer.toJSONString(null);
+ assertThat(json).isEqualTo("null");
+ }
+
+ @Test
+ public void testNullInput_parseObject() {
+ TestObject obj = jsonSerializer.parseObject(null, TestObject.class);
+ assertThat(obj).isNull();
+
+ TestObject obj2 = jsonSerializer.parseObject("{}", null);
+ assertThat(obj2).isNull();
+ }
+
+ @Test
+ public void testParseObject_invalidJson() {
+ assertThatThrownBy(() -> jsonSerializer.parseObject("{invalid json}",
TestObject.class))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("Gson deserialize error");
+ }
+
+ @Test
+ public void testFactoryReturnsCorrectInstance() {
+ JsonSerializer serializer =
JsonSerializerFactory.getSerializer("gson");
+ assertThat(serializer).isNotNull();
+ assertThat(serializer).isInstanceOf(GsonJsonSerializer.class);
+ }
+
+ @Test
+ public void testParseObject_nullText() {
+ assertThat(jsonSerializer.parseObject(null, String.class)).isNull();
+ }
+
+ @Test
+ public void testToJSONString_emptyList() {
+ List<String> emptyList = new ArrayList<>();
+ String json = jsonSerializer.toJSONString(emptyList, false, false);
+ assertThat(json).isEqualTo("[]");
+ }
+
+ @Test
+ public void testToJSONString_withAutoType() {
+ TestObject obj = new TestObject("withType", 789);
+ String jsonWithAutoType = jsonSerializer.toJSONString(obj, false,
false);
+
+ assertThat(jsonWithAutoType).doesNotContain("@type");
+ }
+
+ @Test
+ public void testToJsonString_prettyPrint() {
+ TestObject obj = new TestObject("pretty", 789);
+ String prettyJson = jsonSerializer.toJSONString(obj, false, true);
+
+ // Pretty JSON should contain newlines and indentation
+ assertThat(prettyJson).contains("\n");
+ }
+
+ @Test
+ public void testParseObject_nullJson() {
+ assertThat(jsonSerializer.parseObject(null, TestObject.class,
false)).isNull();
+ }
+
+ @Test
+ public void testParseObject_emptyList() {
+ String json = "[]";
+ List<?> list = jsonSerializer.parseObject(json, List.class, false);
+ assertThat(list).isEmpty();
+ }
+
+ @Test
+ public void testParseObject_withAutoType() {
+ TestObject original = new TestObject("autoTypeTest", 999);
+ String jsonWithAutoType = jsonSerializer.toJSONString(original, false,
false);
+
+ TestObject restored = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, false);
+
+ assertThat(restored).isNotNull();
+ assertThat(restored.getName()).isEqualTo("autoTypeTest");
+ assertThat(restored.getValue()).isEqualTo(999);
+ }
+
+ public static class TestObject {
+ private String name;
+ private int value;
+
+ public TestObject() {
+ // Default constructor required for Gson
+ }
+
+ public TestObject(String name, int value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+ }
+}
diff --git
a/json-common/src/test/java/org/apache/seata/common/json/JacksonJsonSerializerTest.java
b/json-common/src/test/java/org/apache/seata/common/json/JacksonJsonSerializerTest.java
new file mode 100644
index 0000000000..2b20a1e670
--- /dev/null
+++
b/json-common/src/test/java/org/apache/seata/common/json/JacksonJsonSerializerTest.java
@@ -0,0 +1,342 @@
+/*
+ * 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.seata.common.json;
+
+import com.alibaba.fastjson.TypeReference;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.impl.JacksonJsonSerializer;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class JacksonJsonSerializerTest {
+
+ private JsonSerializer jsonSerializer;
+
+ @BeforeEach
+ void setUp() {
+ // Use factory to get Jackson serializer by name
+ jsonSerializer = JsonSerializerFactory.getSerializer("jackson");
+ }
+
+ @Test
+ public void testToJSONString_basicObject() {
+ TestObject obj = new TestObject("test", 123);
+ String json = jsonSerializer.toJSONString(obj);
+
+ assertThat(json).contains("\"name\":\"test\"");
+ assertThat(json).contains("\"value\":123");
+ }
+
+ @Test
+ public void testParseObject_basicObject() {
+ String json =
+
"{\"@class\":\"org.apache.seata.common.json.JacksonJsonSerializerTest$TestObject\",\"name\":\"test\",\"value\":123}";
+ TestObject obj = jsonSerializer.parseObject(json, TestObject.class);
+
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("test");
+ assertThat(obj.getValue()).isEqualTo(123);
+ }
+
+ @Test
+ public void testToJSONString_and_parseObject() {
+ TestObject original = new TestObject("school", 456);
+ String json = jsonSerializer.toJSONString(original);
+ TestObject restored = jsonSerializer.parseObject(json,
TestObject.class);
+
+ assertThat(restored.getName()).isEqualTo(original.getName());
+ assertThat(restored.getValue()).isEqualTo(original.getValue());
+ }
+
+ @Test
+ public void testUseAutoType_withType() {
+ String json = "{\"@type\":\"some.type\",\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(json);
+ assertThat(hasAutoType).isTrue();
+ }
+
+ @Test
+ public void testUseAutoType_withoutType() {
+ String json = "{\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(json);
+ assertThat(hasAutoType).isFalse();
+ }
+
+ @Test
+ public void testToJSONString_withAutoType() {
+ TestObject obj = new TestObject("withType", 789);
+ String jsonWithAutoType = jsonSerializer.toJSONString(obj, false,
false);
+
+ assertThat(jsonWithAutoType).isNotNull();
+ assertThat(jsonWithAutoType).contains("@type");
+ assertThat(jsonWithAutoType).contains("\"name\":\"withType\"");
+ assertThat(jsonWithAutoType).contains("\"value\":789");
+ }
+
+ @Test
+ public void testParseObject_withAutoType() {
+ TestObject original = new TestObject("autoTypeTest", 999);
+ // Serialize with autoType enabled
+ String jsonWithAutoType = jsonSerializer.toJSONString(original, false,
false);
+
+ // Deserialize with autoType enabled
+ TestObject restored = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, false);
+
+ assertThat(restored).isNotNull();
+ assertThat(restored.getName()).isEqualTo("autoTypeTest");
+ assertThat(restored.getValue()).isEqualTo(999);
+ }
+
+ @Test
+ public void testEmptyList_serialization() {
+ List<String> emptyList = new ArrayList<>();
+ String json = jsonSerializer.toJSONString(emptyList, false, false);
+ assertThat(json).isEqualTo("[]");
+ }
+
+ @Test
+ public void testEmptyList_deserialization() {
+ String json = "[]";
+ List<?> list = jsonSerializer.parseObject(json, List.class, false);
+ assertThat(list).isEmpty();
+ }
+
+ @Test
+ public void testNullInput_toJSONString() {
+ String json = jsonSerializer.toJSONString(null);
+ assertThat(json).isEqualTo("null");
+ }
+
+ @Test
+ public void testNullInput_parseObject() {
+ TestObject obj = jsonSerializer.parseObject(null, TestObject.class);
+ assertThat(obj).isNull();
+
+ TestObject obj2 = jsonSerializer.parseObject("{}", null);
+ assertThat(obj2).isNull();
+ }
+
+ @Test
+ public void testParseObject_invalidJson() {
+ assertThatThrownBy(() -> jsonSerializer.parseObject("{invalid json}",
TestObject.class))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("Jackson deserialize error");
+ }
+
+ @Test
+ public void testFactoryReturnsCorrectInstance() {
+ JsonSerializer serializer =
JsonSerializerFactory.getSerializer("jackson");
+ assertThat(serializer).isNotNull();
+ assertThat(serializer).isInstanceOf(JacksonJsonSerializer.class);
+ }
+
+ @Test
+ public void testFactoryReturnsDefaultInstance() {
+ JsonSerializer serializer = JsonSerializerFactory.getSerializer(null);
+ assertThat(serializer).isNotNull();
+ }
+
+ @Test
+ public void testToJSONString_throwsException() {
+ Object unserializable = new Object() {
+ private final java.io.InputStream stream = System.in;
+ };
+
+ assertThatThrownBy(() -> jsonSerializer.toJSONString(unserializable))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("Jackson serialize error");
+ }
+
+ @Test
+ public void testParseObject_nullText() {
+ assertThat(jsonSerializer.parseObject(null, String.class)).isNull();
+ }
+
+ @Test
+ public void testToJSONString_prettyPrint() {
+ TestObject obj = new TestObject("pretty", 789);
+ String prettyJson = jsonSerializer.toJSONString(obj, true);
+
+ // Pretty JSON should contain newlines and indentation
+ assertThat(prettyJson).contains("\n");
+ }
+
+ @Test
+ public void testParseObject_nullJson() {
+ assertThat(jsonSerializer.parseObject(null, TestObject.class,
false)).isNull();
+ }
+
+ @Test
+ public void testParseObject_emptyList() {
+ String json = "[]";
+ List<?> list = jsonSerializer.parseObject(json, List.class, false);
+ assertThat(list).isEmpty();
+ }
+
+ @Test
+ public void testParseObject_ignoreAutoType() {
+ TestObject obj = new TestObject("ignored", 222);
+ String json = jsonSerializer.toJSONString(obj);
+
+ TestObject restored = jsonSerializer.parseObject(json,
TestObject.class, true);
+
+ assertThat(restored).isNotNull();
+ assertThat(restored.getName()).isEqualTo("ignored");
+ assertThat(restored.getValue()).isEqualTo(222);
+ }
+
+ @Test
+ public void testParseObject_withType() {
+ String json =
+
"{\"@type\":\"org.apache.seata.common.json.JacksonJsonSerializerTest$TestObject\",\"name\":\"test\",\"value\":123}";
+ Type type = new TypeReference<TestObject>() {}.getType();
+
+ TestObject obj = jsonSerializer.parseObjectWithType(json, type);
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("test");
+ assertThat(obj.getValue()).isEqualTo(123);
+
+ assertThatThrownBy(() -> jsonSerializer.parseObjectWithType("{invalid
json}", type))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("Jackson deserialize error");
+ }
+
+ @Test
+ public void testUseAutoType() {
+ String jsonWithAutoType =
"{\"@type\":\"some.type\",\"name\":\"test\"}";
+ boolean hasAutoType = jsonSerializer.useAutoType(jsonWithAutoType);
+ assertThat(hasAutoType).isTrue();
+
+ String jsonWithoutAutoType = "{\"name\":\"test\"}";
+ boolean noAutoType = jsonSerializer.useAutoType(jsonWithoutAutoType);
+ assertThat(noAutoType).isFalse();
+
+ boolean nullAutoType = jsonSerializer.useAutoType(null);
+ assertThat(nullAutoType).isFalse();
+
+ boolean emptyAutoType = jsonSerializer.useAutoType("");
+ assertThat(emptyAutoType).isFalse();
+ }
+
+ @Test
+ public void testToJSONString_withPrettyPrint() {
+ TestObject obj = new TestObject("pretty", 789);
+
+ String prettyJson = jsonSerializer.toJSONString(obj, true);
+ assertThat(prettyJson).contains("\n");
+ assertThat(prettyJson).contains("@type");
+
+ String normalJson = jsonSerializer.toJSONString(obj, false);
+ assertThat(normalJson).doesNotContain("\n");
+ assertThat(normalJson).contains("@type");
+ }
+
+ @Test
+ public void testToJSONString_withIgnoreAutoTypeAndPrettyPrint() {
+ TestObject obj = new TestObject("noType", 111);
+
+ String jsonIgnorePretty = jsonSerializer.toJSONString(obj, true, true);
+ assertThat(jsonIgnorePretty).contains("\n");
+ assertThat(jsonIgnorePretty).doesNotContain("@type");
+
+ String jsonIgnoreNormal = jsonSerializer.toJSONString(obj, true,
false);
+ assertThat(jsonIgnoreNormal).doesNotContain("\n");
+ assertThat(jsonIgnoreNormal).doesNotContain("@type");
+
+ String jsonNoIgnorePretty = jsonSerializer.toJSONString(obj, false,
true);
+ assertThat(jsonNoIgnorePretty).contains("\n");
+ assertThat(jsonNoIgnorePretty).contains("@type");
+
+ String jsonNoIgnoreNormal = jsonSerializer.toJSONString(obj, false,
false);
+ assertThat(jsonNoIgnoreNormal).doesNotContain("\n");
+ assertThat(jsonNoIgnoreNormal).contains("@type");
+
+ List<String> emptyList = new ArrayList<>();
+ String emptyListJson = jsonSerializer.toJSONString(emptyList, false,
false);
+ assertThat(emptyListJson).isEqualTo("[]");
+
+ Object invalidObject = new Object() {
+ private final java.io.InputStream stream = System.in;
+ };
+
+ assertThatThrownBy(() -> jsonSerializer.toJSONString(invalidObject,
false, false))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("Jackson serialize error");
+ }
+
+ @Test
+ public void testParseObject_withIgnoreAutoType() {
+ String jsonWithAutoType = jsonSerializer.toJSONString(new
TestObject("ignored", 222), false, false);
+
+ TestObject objIgnore = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, true);
+ assertThat(objIgnore).isNotNull();
+ assertThat(objIgnore.getName()).isEqualTo("ignored");
+ assertThat(objIgnore.getValue()).isEqualTo(222);
+
+ TestObject objNoIgnore = jsonSerializer.parseObject(jsonWithAutoType,
TestObject.class, false);
+ assertThat(objNoIgnore).isNotNull();
+ assertThat(objNoIgnore.getName()).isEqualTo("ignored");
+ assertThat(objNoIgnore.getValue()).isEqualTo(222);
+
+ List<?> emptyList = jsonSerializer.parseObject("[]", List.class,
false);
+ assertThat(emptyList).isEmpty();
+
+ List<?> emptyListIgnore = jsonSerializer.parseObject("[]", List.class,
true);
+ assertThat(emptyListIgnore).isEmpty();
+
+ assertThat(jsonSerializer.parseObject(null, TestObject.class,
false)).isNull();
+
+ assertThatThrownBy(() -> jsonSerializer.parseObject("{invalid json}",
TestObject.class, false))
+ .isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("Jackson deserialize error");
+ }
+
+ public static class TestObject {
+ private String name;
+ private int value;
+
+ public TestObject() {}
+
+ public TestObject(String name, int value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+ }
+}
diff --git
a/json-common/src/test/java/org/apache/seata/common/json/JsonUtilTest.java
b/json-common/src/test/java/org/apache/seata/common/json/JsonUtilTest.java
new file mode 100644
index 0000000000..54e0c8afb7
--- /dev/null
+++ b/json-common/src/test/java/org/apache/seata/common/json/JsonUtilTest.java
@@ -0,0 +1,158 @@
+/*
+ * 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.seata.common.json;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class JsonUtilTest {
+
+ @BeforeEach
+ void setUp() {}
+
+ @Test
+ public void testToJSONString_basicObject() {
+ TestObject obj = new TestObject("test", 123);
+ String json = JsonUtil.toJSONString(obj);
+
+ assertThat(json).isNotNull();
+ assertThat(json).contains("\"name\":\"test\"");
+ assertThat(json).contains("\"value\":123");
+ }
+
+ @Test
+ public void testParseObject_basicObject() {
+ String json = "{\"name\":\"test\",\"value\":123}";
+ TestObject obj = JsonUtil.parseObject(json, TestObject.class);
+
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("test");
+ assertThat(obj.getValue()).isEqualTo(123);
+ }
+
+ @Test
+ public void testToJSONString_and_parseObject_apple() {
+ TestObject original = new TestObject("apple", 456);
+ String json = JsonUtil.toJSONString(original);
+ TestObject restored = JsonUtil.parseObject(json, TestObject.class);
+
+ assertThat(restored.getName()).isEqualTo(original.getName());
+ assertThat(restored.getValue()).isEqualTo(original.getValue());
+ }
+
+ @Test
+ public void testParseObject_nullInputs() {
+ TestObject obj1 = JsonUtil.parseObject(null, TestObject.class);
+ assertThat(obj1).isNull();
+
+ TestObject obj2 = JsonUtil.parseObject("{\"name\":\"test\"}", null);
+ assertThat(obj2).isNull();
+ }
+
+ @Test
+ public void testParseObject_prefixLogic() {
+ String normalJson = "{\"name\":\"normalTest\",\"value\":888}";
+ TestObject obj = JsonUtil.parseObject(normalJson, TestObject.class);
+ assertThat(obj).isNotNull();
+ assertThat(obj.getName()).isEqualTo("normalTest");
+ assertThat(obj.getValue()).isEqualTo(888);
+ }
+
+ @Test
+ public void testToJSONString_nullObject() {
+ String json = JsonUtil.toJSONString(null);
+ assertThat(json).isEqualTo("null");
+ }
+
+ @Test
+ public void testParseObject_complexObject() {
+ ComplexTestObject complexObj = new ComplexTestObject();
+ complexObj.setName("complex");
+ complexObj.setValue(789);
+ complexObj.setNested(new TestObject("nested", 1));
+
+ String json = JsonUtil.toJSONString(complexObj);
+ ComplexTestObject restored = JsonUtil.parseObject(json,
ComplexTestObject.class);
+
+ assertThat(restored).isNotNull();
+ assertThat(restored.getName()).isEqualTo("complex");
+ assertThat(restored.getValue()).isEqualTo(789);
+ assertThat(restored.getNested()).isNotNull();
+ assertThat(restored.getNested().getName()).isEqualTo("nested");
+ assertThat(restored.getNested().getValue()).isEqualTo(1);
+ }
+
+ public static class TestObject {
+ private String name;
+ private int value;
+
+ public TestObject() {}
+
+ public TestObject(String name, int value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+ }
+
+ public static class ComplexTestObject {
+ private String name;
+ private int value;
+ private TestObject nested;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ public TestObject getNested() {
+ return nested;
+ }
+
+ public void setNested(TestObject nested) {
+ this.nested = nested;
+ }
+ }
+}
diff --git a/json-common/src/test/resources/file.conf
b/json-common/src/test/resources/file.conf
new file mode 100644
index 0000000000..46c3e0401c
--- /dev/null
+++ b/json-common/src/test/resources/file.conf
@@ -0,0 +1,25 @@
+#
+# 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.
+#
+
+service {
+ #transaction service group mapping
+ vgroupMapping.default_tx_group = "default"
+ #only support when registry.type=file, please don't set multiple addresses
+ default.grouplist = "127.0.0.1:8091"
+ #disable seata
+ disableGlobalTransaction = false
+}
\ No newline at end of file
diff --git a/json-common/src/test/resources/registry.conf
b/json-common/src/test/resources/registry.conf
new file mode 100644
index 0000000000..5ad014bf55
--- /dev/null
+++ b/json-common/src/test/resources/registry.conf
@@ -0,0 +1,34 @@
+#
+# 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.
+#
+
+registry {
+ # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
+ type = "file"
+
+ file {
+ name = "file.conf"
+ }
+}
+
+config {
+ # file、nacos 、apollo、zk、consul、etcd3
+ type = "file"
+
+ file {
+ name = "file.conf"
+ }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 74e2fc1f52..4cac88df59 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,7 @@
<!-- <module>namingserver</module>-->
<module>test-suite/test-new-version</module>
<module>test-suite/test-old-version</module>
+ <module>json-common</module>
<!--<module>seata-plugin</module>-->
</modules>
diff --git a/rm-datasource/pom.xml b/rm-datasource/pom.xml
index 90411c220d..e22d54adb8 100644
--- a/rm-datasource/pom.xml
+++ b/rm-datasource/pom.xml
@@ -163,5 +163,10 @@
<artifactId>oscarJDBC8</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>json-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
</project>
diff --git
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/ConnectionContext.java
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/ConnectionContext.java
index 0501af60fa..04b61b543a 100644
---
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/ConnectionContext.java
+++
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/ConnectionContext.java
@@ -16,10 +16,11 @@
*/
package org.apache.seata.rm.datasource;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.seata.common.LockStrategyMode;
+import org.apache.seata.common.exception.JsonParseException;
import org.apache.seata.common.exception.ShouldNeverHappenException;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.core.context.GlobalLockConfigHolder;
@@ -59,10 +60,9 @@ public class ConnectionContext {
}
};
- private static final ObjectMapper MAPPER = new ObjectMapper();
-
private String xid;
private Long branchId;
+ private final JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer("jackson");
private boolean isGlobalLockRequire;
private Savepoint currentSavepoint = DEFAULT_SAVEPOINT;
private boolean autoCommitChanged;
@@ -304,8 +304,8 @@ public class ConnectionContext {
if (!this.applicationData.isEmpty()) {
try {
- return MAPPER.writeValueAsString(this.applicationData);
- } catch (JsonProcessingException e) {
+ return jsonSerializer.toJSONString(this.applicationData, true,
false);
+ } catch (JsonParseException e) {
throw new TransactionException(e.getMessage(), e);
}
}
diff --git
a/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
b/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
new file mode 100644
index 0000000000..f723b14b68
--- /dev/null
+++
b/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+org.apache.seata.common.json.impl.FastjsonJsonSerializer
+org.apache.seata.common.json.impl.JacksonJsonSerializer
+org.apache.seata.common.json.impl.GsonJsonSerializer
\ No newline at end of file
diff --git
a/rm-datasource/src/test/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
b/rm-datasource/src/test/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
new file mode 100644
index 0000000000..f723b14b68
--- /dev/null
+++
b/rm-datasource/src/test/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+org.apache.seata.common.json.impl.FastjsonJsonSerializer
+org.apache.seata.common.json.impl.JacksonJsonSerializer
+org.apache.seata.common.json.impl.GsonJsonSerializer
\ No newline at end of file
diff --git a/saga/pom.xml b/saga/pom.xml
index 0d05dbb5fa..a8b639e8b3 100644
--- a/saga/pom.xml
+++ b/saga/pom.xml
@@ -48,6 +48,12 @@
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>json-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
diff --git
a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/serializer/impl/ParamsSerializer.java
b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/serializer/impl/ParamsSerializer.java
index fa396475a2..e989c5e62b 100644
---
a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/serializer/impl/ParamsSerializer.java
+++
b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/serializer/impl/ParamsSerializer.java
@@ -16,10 +16,10 @@
*/
package org.apache.seata.saga.engine.serializer.impl;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.saga.engine.serializer.Serializer;
import org.apache.seata.saga.statelang.domain.DomainConstants;
-import org.apache.seata.saga.statelang.parser.JsonParser;
-import org.apache.seata.saga.statelang.parser.JsonParserFactory;
/**
* Parameter serializer based on Fastjson
@@ -32,11 +32,11 @@ public class ParamsSerializer implements Serializer<Object,
String> {
@Override
public String serialize(Object params) {
if (params != null) {
- JsonParser jsonParser =
JsonParserFactory.getJsonParser(jsonParserName);
- if (jsonParser == null) {
- throw new RuntimeException("Cannot find JsonParer by name: " +
jsonParserName);
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer(jsonParserName);
+ if (jsonSerializer == null) {
+ throw new RuntimeException("Cannot find JsonSerializer by
name: " + jsonParserName);
}
- return jsonParser.toJsonString(params, false);
+ return jsonSerializer.toJSONString(params, false);
}
return null;
}
@@ -44,11 +44,11 @@ public class ParamsSerializer implements Serializer<Object,
String> {
@Override
public Object deserialize(String json) {
if (json != null) {
- JsonParser jsonParser =
JsonParserFactory.getJsonParser(jsonParserName);
- if (jsonParser == null) {
- throw new RuntimeException("Cannot find JsonParer by name: " +
jsonParserName);
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer(jsonParserName);
+ if (jsonSerializer == null) {
+ throw new RuntimeException("Cannot find JsonSerializer by
name: " + jsonParserName);
}
- return jsonParser.parse(json, Object.class, false);
+ return jsonSerializer.parseObject(json, Object.class, false);
}
return null;
}
diff --git
a/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvoker.java
b/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvoker.java
index 48a3e9f2bd..b97cb064bf 100644
---
a/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvoker.java
+++
b/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvoker.java
@@ -17,6 +17,8 @@
package org.apache.seata.saga.engine.invoker.impl;
import org.apache.seata.common.exception.FrameworkErrorCode;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.saga.engine.exception.EngineExecutionException;
import org.apache.seata.saga.engine.invoker.ServiceInvoker;
@@ -25,8 +27,6 @@ import org.apache.seata.saga.engine.utils.ExceptionUtils;
import org.apache.seata.saga.statelang.domain.ServiceTaskState;
import org.apache.seata.saga.statelang.domain.TaskState.Retry;
import org.apache.seata.saga.statelang.domain.impl.ServiceTaskStateImpl;
-import org.apache.seata.saga.statelang.parser.JsonParser;
-import org.apache.seata.saga.statelang.parser.JsonParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
@@ -311,16 +311,16 @@ public class SpringBeanServiceInvoker implements
ServiceInvoker, ApplicationCont
} else if (isPrimitive(paramType)) {
return value;
} else {
- JsonParser jsonParser =
JsonParserFactory.getJsonParser(getSagaJsonParser());
- if (jsonParser == null) {
- throw new RuntimeException("Cannot get JsonParser by name : "
+ getSagaJsonParser());
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer(getSagaJsonParser());
+ if (jsonSerializer == null) {
+ throw new RuntimeException("Cannot get JsonSerializer by name
: " + getSagaJsonParser());
}
- String jsonValue = jsonParser.toJsonString(value, true, false);
+ String jsonValue = jsonSerializer.toJSONString(value, true, false);
// compatible history autoType serialize json
- boolean useAutoType = jsonParser.useAutoType(jsonValue);
+ boolean useAutoType = jsonSerializer.useAutoType(jsonValue);
- return jsonParser.parse(jsonValue, paramType, !useAutoType);
+ return jsonSerializer.parseObject(jsonValue, paramType,
!useAutoType);
}
}
diff --git
a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java
b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java
index 1ac3b01eff..16c925f687 100644
---
a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java
+++
b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java
@@ -16,6 +16,8 @@
*/
package org.apache.seata.saga.statelang.parser.impl;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.RecoverStrategy;
@@ -25,8 +27,6 @@ import org.apache.seata.saga.statelang.domain.StateType;
import org.apache.seata.saga.statelang.domain.impl.AbstractTaskState;
import org.apache.seata.saga.statelang.domain.impl.BaseState;
import org.apache.seata.saga.statelang.domain.impl.StateMachineImpl;
-import org.apache.seata.saga.statelang.parser.JsonParser;
-import org.apache.seata.saga.statelang.parser.JsonParserFactory;
import org.apache.seata.saga.statelang.parser.StateMachineParser;
import org.apache.seata.saga.statelang.parser.StateParser;
import org.apache.seata.saga.statelang.parser.StateParserFactory;
@@ -58,15 +58,15 @@ public class StateMachineParserImpl implements
StateMachineParser {
@Override
public StateMachine parse(String json) {
- JsonParser jsonParser =
JsonParserFactory.getJsonParser(jsonParserName);
- if (jsonParser == null) {
- throw new RuntimeException("Cannot find JsonParer by name: " +
jsonParserName);
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer(jsonParserName);
+ if (jsonSerializer == null) {
+ throw new RuntimeException("Cannot find JsonSerializer by name: "
+ jsonParserName);
}
- Map<String, Object> node = jsonParser.parse(json, Map.class, true);
+ Map<String, Object> node = jsonSerializer.parseObject(json, Map.class,
true);
if (DesignerJsonTransformer.isDesignerJson(node)) {
node = DesignerJsonTransformer.toStandardJson(node);
if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("===== Transformed standard state language:\n{}",
jsonParser.toJsonString(node, true));
+ LOGGER.debug("===== Transformed standard state language:\n{}",
jsonSerializer.toJSONString(node, true));
}
}
diff --git
a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java
b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java
index b38ad2ad34..d33dd31048 100644
---
a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java
+++
b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java
@@ -18,12 +18,12 @@ package org.apache.seata.saga.statelang.parser.utils;
import org.apache.seata.common.exception.FrameworkErrorCode;
import org.apache.seata.common.exception.FrameworkException;
+import org.apache.seata.common.json.JsonSerializer;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.saga.statelang.domain.ExecutionStatus;
import org.apache.seata.saga.statelang.domain.StateInstance;
import org.apache.seata.saga.statelang.domain.StateMachineInstance;
-import org.apache.seata.saga.statelang.parser.JsonParser;
import java.util.ArrayList;
import java.util.HashMap;
@@ -223,7 +223,8 @@ public class DesignerJsonTransformer {
* @return the tracing graph json
*/
@SuppressWarnings("lgtm[java/dereferenced-value-may-be-null]")
- public static String generateTracingGraphJson(StateMachineInstance
stateMachineInstance, JsonParser jsonParser) {
+ public static String generateTracingGraphJson(
+ StateMachineInstance stateMachineInstance, JsonSerializer
jsonParser) {
if (stateMachineInstance == null) {
throw new FrameworkException(
@@ -234,7 +235,7 @@ public class DesignerJsonTransformer {
throw new FrameworkException("Cannot get StateMachine Json",
FrameworkErrorCode.ObjectNotExists);
}
- Map<String, Object> stateMachineJsonObj =
jsonParser.parse(stateMachineJson, Map.class, true);
+ Map<String, Object> stateMachineJsonObj =
jsonParser.parseObject(stateMachineJson, Map.class, true);
if (!DesignerJsonTransformer.isDesignerJson(stateMachineJsonObj)) {
throw new FrameworkException(
"StateMachine Json is not generated by Designer",
FrameworkErrorCode.InvalidConfiguration);
@@ -290,7 +291,7 @@ public class DesignerJsonTransformer {
if (stateMachineJsonObj != null) {
/*lgtm[java/useless-null-check]*/
- return jsonParser.toJsonString(stateMachineJsonObj, true);
+ return jsonParser.toJSONString(stateMachineJsonObj, true);
}
return "";
}
diff --git
a/saga/seata-saga-statelang/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
b/saga/seata-saga-statelang/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
new file mode 100644
index 0000000000..f723b14b68
--- /dev/null
+++
b/saga/seata-saga-statelang/src/main/resources/META-INF/services/org.apache.seata.common.json.JsonSerializer
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+org.apache.seata.common.json.impl.FastjsonJsonSerializer
+org.apache.seata.common.json.impl.JacksonJsonSerializer
+org.apache.seata.common.json.impl.GsonJsonSerializer
\ No newline at end of file
diff --git
a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java
b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java
index 7b6daab72e..592d536644 100644
---
a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java
+++
b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java
@@ -16,6 +16,8 @@
*/
package org.apache.seata.saga.statelang.parser;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.common.util.BeanUtils;
import org.apache.seata.saga.statelang.domain.StateMachine;
import org.apache.seata.saga.statelang.domain.StateMachineInstance;
@@ -46,12 +48,12 @@ public class StateParserTests {
stateMachine.setGmtCreate(new Date());
Assertions.assertNotNull(stateMachine);
- JsonParser jsonParser = JsonParserFactory.getJsonParser("jackson");
- String outputJson = jsonParser.toJsonString(stateMachine, true);
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer("jackson");
+ String outputJson = jsonSerializer.toJSONString(stateMachine, true);
System.out.println(outputJson);
- JsonParser fastjsonParser =
JsonParserFactory.getJsonParser("fastjson");
- String fastjsonOutputJson = fastjsonParser.toJsonString(stateMachine,
true);
+ JsonSerializer fastjsonSerializer =
JsonSerializerFactory.getSerializer("fastjson");
+ String fastjsonOutputJson =
fastjsonSerializer.toJSONString(stateMachine, true);
System.out.println(fastjsonOutputJson);
Assertions.assertEquals("simpleTestStateMachine",
stateMachine.getName());
@@ -62,19 +64,20 @@ public class StateParserTests {
public void testDesignerJsonTransformer() throws IOException {
InputStream inputStream =
getInputStreamByPath("statelang/simple_statemachine_with_layout.json");
String json = IOUtils.toString(inputStream, "UTF-8");
- JsonParser jsonParser = JsonParserFactory.getJsonParser("jackson");
- Map<String, Object> parsedObj =
DesignerJsonTransformer.toStandardJson(jsonParser.parse(json, Map.class, true));
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer("jackson");
+ Map<String, Object> parsedObj =
+
DesignerJsonTransformer.toStandardJson(jsonSerializer.parseObject(json,
Map.class, true));
Assertions.assertNotNull(parsedObj);
- String outputJson = jsonParser.toJsonString(parsedObj, true);
+ String outputJson = jsonSerializer.toJSONString(parsedObj, true);
System.out.println(outputJson);
- JsonParser fastjsonParser =
JsonParserFactory.getJsonParser("fastjson");
+ JsonSerializer fastjsonSerializer =
JsonSerializerFactory.getSerializer("fastjson");
Map<String, Object> fastjsonParsedObj =
-
DesignerJsonTransformer.toStandardJson(fastjsonParser.parse(json, Map.class,
true));
+
DesignerJsonTransformer.toStandardJson(fastjsonSerializer.parseObject(json,
Map.class, true));
Assertions.assertNotNull(fastjsonParsedObj);
- String fastjsonOutputJson =
fastjsonParser.toJsonString(fastjsonParsedObj, true);
+ String fastjsonOutputJson =
fastjsonSerializer.toJSONString(fastjsonParsedObj, true);
System.out.println(fastjsonOutputJson);
}
@@ -135,8 +138,8 @@ public class StateParserTests {
context.put("test", "test");
stateMachine.setContent(json);
instance.setStateMachine(stateMachine);
- JsonParser jsonParser = JsonParserFactory.getJsonParser("fastjson");
- String graphJson =
DesignerJsonTransformer.generateTracingGraphJson(instance, jsonParser);
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer("fastjson");
+ String graphJson =
DesignerJsonTransformer.generateTracingGraphJson(instance, jsonSerializer);
Assertions.assertNotNull(graphJson);
}
diff --git
a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformerTest.java
b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformerTest.java
index 677fe19c29..23dc3f463a 100644
---
a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformerTest.java
+++
b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformerTest.java
@@ -17,6 +17,8 @@
package org.apache.seata.saga.statelang.parser.utils;
import org.apache.seata.common.exception.FrameworkException;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.saga.statelang.parser.JsonParser;
import org.apache.seata.saga.statelang.parser.JsonParserFactory;
import org.junit.jupiter.api.Assertions;
@@ -57,7 +59,7 @@ public class DesignerJsonTransformerTest {
@Test
public void testGenerateTracingGraphJsonWithNullInstance() {
- JsonParser parser = JsonParserFactory.getJsonParser("jackson");
+ JsonSerializer parser = JsonSerializerFactory.getSerializer("jackson");
FrameworkException e = Assertions.assertThrows(
FrameworkException.class, () ->
DesignerJsonTransformer.generateTracingGraphJson(null, parser));
Assertions.assertEquals("StateMachineInstance is not exits",
e.getMessage());
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/StateMachineTests.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/StateMachineTests.java
index b107494a3f..e22680f995 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/StateMachineTests.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/StateMachineTests.java
@@ -17,12 +17,12 @@
package org.apache.seata.saga.engine;
import org.apache.seata.common.SagaCostPrint;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.saga.engine.mock.DemoService.Engineer;
import org.apache.seata.saga.engine.mock.DemoService.People;
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.ExecutionStatus;
import org.apache.seata.saga.statelang.domain.StateMachineInstance;
-import org.apache.seata.saga.statelang.parser.JsonParserFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -263,7 +263,7 @@ public class StateMachineTests {
map1.put("lilei4", people4);
people1.setChildrenMap(map1);
- String json =
JsonParserFactory.getJsonParser("jackson").toJsonString(people1, false, true);
+ String json =
JsonSerializerFactory.getSerializer("jackson").toJSONString(people1, false,
true);
System.out.println(json);
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java
index d7ab2123fe..8cad9df61d 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java
@@ -17,6 +17,8 @@
package org.apache.seata.saga.engine.db.mockserver;
import org.apache.seata.common.SagaCostPrint;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.json.JsonSerializerFactory;
import org.apache.seata.saga.engine.StateMachineEngine;
import org.apache.seata.saga.engine.mock.DemoService.Engineer;
import org.apache.seata.saga.engine.mock.DemoService.People;
@@ -24,8 +26,6 @@ import org.apache.seata.saga.rm.StateMachineEngineHolder;
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.ExecutionStatus;
import org.apache.seata.saga.statelang.domain.StateMachineInstance;
-import org.apache.seata.saga.statelang.parser.JsonParser;
-import org.apache.seata.saga.statelang.parser.JsonParserFactory;
import org.apache.seata.saga.statelang.parser.utils.DesignerJsonTransformer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@@ -315,7 +315,7 @@ public class StateMachineDBMockServerTests {
public void testForwardSubStateMachineWithLayout() throws Exception {
String stateMachineName =
"simpleStateMachineWithCompensationAndSubMachine_layout";
- JsonParser jsonParser = JsonParserFactory.getJsonParser("jackson");
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer("jackson");
StateMachineInstance inst0 = SagaCostPrint.executeAndPrint("5-20", ()
-> {
Map<String, Object> paramMap = new HashMap<>(2);
@@ -326,7 +326,7 @@ public class StateMachineDBMockServerTests {
Assertions.assertEquals(ExecutionStatus.UN, inst.getStatus());
- String graphJson =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonParser);
+ String graphJson =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonSerializer);
Assertions.assertNotNull(graphJson);
System.out.println(graphJson);
@@ -341,7 +341,7 @@ public class StateMachineDBMockServerTests {
Assertions.assertEquals(ExecutionStatus.SU, inst.getStatus());
- String graphJson2 =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonParser);
+ String graphJson2 =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonSerializer);
Assertions.assertNotNull(graphJson2);
System.out.println(graphJson2);
});
@@ -375,7 +375,7 @@ public class StateMachineDBMockServerTests {
public void testCompensateSubStateMachineWithLayout() throws Exception {
String stateMachineName =
"simpleStateMachineWithCompensationAndSubMachine_layout";
- JsonParser jsonParser = JsonParserFactory.getJsonParser("jackson");
+ JsonSerializer jsonSerializer =
JsonSerializerFactory.getSerializer("jackson");
Map<String, Object> paramMap = new HashMap<>(3);
paramMap.put("a", 2);
@@ -387,7 +387,7 @@ public class StateMachineDBMockServerTests {
Assertions.assertEquals(ExecutionStatus.UN, inst.getStatus());
- String graphJson =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonParser);
+ String graphJson =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonSerializer);
Assertions.assertNotNull(graphJson);
System.out.println(graphJson);
@@ -399,7 +399,7 @@ public class StateMachineDBMockServerTests {
Assertions.assertEquals(ExecutionStatus.UN,
inst.getCompensationStatus());
- String graphJson2 =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonParser);
+ String graphJson2 =
DesignerJsonTransformer.generateTracingGraphJson(inst, jsonSerializer);
Assertions.assertNotNull(graphJson2);
System.out.println(graphJson2);
});
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]