LegendPei commented on code in PR #7760:
URL: https://github.com/apache/incubator-seata/pull/7760#discussion_r2558477207


##########
json-common/src/main/java/org/apache/seata/common/json/JsonUtil.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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);
+
+    /**
+     * 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 
JsonSerializerFactory.getSerializer(CONFIG_JSON_PARSER_NAME).toJSONString(object);

Review Comment:
   先缓存默认解析器实例怎么样,这样的话如果使用的是默认的解析器,就直接调用,不然就通过工厂生成,并且工厂内部已使用 ConcurrentHashMap 
缓存实例,工厂内的 INSTANCES 会确保同一个 name 只会创建一次 JsonSerializer,后续调用直接返回缓存实例
   
   How about caching the default parser instance first? This way, if the 
default parser is used, it can be directly invoked. Otherwise, it will be 
generated through a factory, and the factory internally uses a 
ConcurrentHashMap to cache instances. The INSTANCES within the factory will 
ensure that only one JsonSerializer will be created for the same name, and 
subsequent calls will directly return the cached instance



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

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


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

Reply via email to