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

wenjun pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new f60e3c2  [DS-6606] [common] JSONUtils#getNodeString String type (#6618)
f60e3c2 is described below

commit f60e3c219af788aadc88ae25cd9341ee3617c47e
Author: aCodingAddict <[email protected]>
AuthorDate: Sat Oct 30 16:02:02 2021 +0800

    [DS-6606] [common] JSONUtils#getNodeString String type (#6618)
---
 .../java/org/apache/dolphinscheduler/common/utils/JSONUtils.java   | 7 ++++++-
 .../org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java    | 4 +++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
index 92da8fc..7bcdf43 100644
--- 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
+++ 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.TimeZone;
 
 import org.slf4j.Logger;
@@ -231,7 +232,11 @@ public class JSONUtils {
     public static String getNodeString(String json, String nodeName) {
         try {
             JsonNode rootNode = objectMapper.readTree(json);
-            return rootNode.has(nodeName) ? rootNode.get(nodeName).toString() 
: "";
+            JsonNode jsonNode = rootNode.findValue(nodeName);
+            if (Objects.isNull(jsonNode)) {
+                return "";
+            }
+            return jsonNode.isTextual() ? jsonNode.asText() : 
jsonNode.toString();
         } catch (JsonProcessingException e) {
             return "";
         }
diff --git 
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
 
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
index 786fb76..955a7e9 100644
--- 
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
+++ 
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
@@ -151,7 +151,9 @@ public class JSONUtilsTest {
         Assert.assertEquals("", JSONUtils.getNodeString("", "key"));
         Assert.assertEquals("", JSONUtils.getNodeString("abc", "key"));
         Assert.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", 
"key"));
-        Assert.assertEquals("\"foo\"", 
JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar"));
+        Assert.assertEquals("foo", 
JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar"));
+        Assert.assertEquals("[1,2,3]", JSONUtils.getNodeString("{\"bar\": 
[1,2,3]}", "bar"));
+        Assert.assertEquals("{\"1\":\"2\",\"2\":3}", 
JSONUtils.getNodeString("{\"bar\": {\"1\":\"2\",\"2\":3}}", "bar"));
     }
     
     @Test

Reply via email to