You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2023/07/11 04:49:13 UTC

[dubbo] branch 3.3 updated: GsonUtils provide toJson method, GenericFilter use gson mode support toJson (#12633)

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

albumenj pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new 8bb17b85f2 GsonUtils provide toJson method, GenericFilter use gson mode support toJson (#12633)
8bb17b85f2 is described below

commit 8bb17b85f2f459da3e601b00a3fb1c6d639b37aa
Author: TomlongTK <lo...@maoyan.com>
AuthorDate: Tue Jul 11 12:49:07 2023 +0800

    GsonUtils provide toJson method, GenericFilter use gson mode support toJson (#12633)
    
    Co-authored-by: Albumen Kevin <jh...@gmail.com>
---
 .../src/main/java/org/apache/dubbo/common/json/GsonUtils.java | 11 +++++++++++
 .../main/java/org/apache/dubbo/rpc/filter/GenericFilter.java  |  7 +++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java
index 7416aa5628..4072f023a8 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java
@@ -60,6 +60,17 @@ public class GsonUtils {
         }
     }
 
+    public static String toJson(Object obj) throws RuntimeException {
+        if (!isSupportGson()) {
+            throw new RuntimeException("Gson is not supported. Please import Gson in JVM env.");
+        }
+        try {
+            return getGson().toJson(obj);
+        } catch (JsonSyntaxException ex) {
+            throw new RuntimeException(String.format("Generic serialization [%s] Json syntax exception thrown when parsing (object:%s ) error:%s", GENERIC_SERIALIZATION_GSON, obj, ex.getMessage()));
+        }
+    }
+
     private static Gson getGson() {
         if (gsonCache == null || !(gsonCache instanceof Gson)) {
             synchronized (GsonUtils.class) {
diff --git a/dubbo-plugin/dubbo-plugin-generic-invoke/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-plugin/dubbo-plugin-generic-invoke/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
index 8375298508..15cdefb2f4 100644
--- a/dubbo-plugin/dubbo-plugin-generic-invoke/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
+++ b/dubbo-plugin/dubbo-plugin-generic-invoke/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
@@ -244,6 +244,9 @@ public class GenericFilter implements Filter, Filter.Listener, ScopeModelAware {
                 }
                 appResponse.setException(appException);
             }
+            if (ProtocolUtils.isGenericReturnRawResult(generic)) {
+                return;
+            }
             if (ProtocolUtils.isJavaGenericSerialization(generic)) {
                 try {
                     UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
@@ -270,8 +273,8 @@ public class GenericFilter implements Filter, Filter.Listener, ScopeModelAware {
                         GENERIC_SERIALIZATION_PROTOBUF +
                         "] serialize result failed.", e);
                 }
-            } else if (ProtocolUtils.isGenericReturnRawResult(generic)) {
-                return;
+            } else if (ProtocolUtils.isGsonGenericSerialization(generic)) {
+                appResponse.setValue(GsonUtils.toJson(appResponse.getValue()));
             } else {
                 appResponse.setValue(PojoUtils.generalize(appResponse.getValue()));
             }