Переглянути джерело

feat(基础模块): 优化脚本中的JSON支持

zhouhao 2 роки тому
батько
коміт
09636925a9

+ 39 - 1
jetlinks-components/common-component/src/main/java/org/jetlinks/community/utils/ObjectMappers.java

@@ -6,6 +6,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.SneakyThrows;
 import lombok.SneakyThrows;
 import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 
 
+import java.io.InputStream;
+import java.util.List;
+
 public class ObjectMappers {
 public class ObjectMappers {
 
 
     public static final ObjectMapper JSON_MAPPER;
     public static final ObjectMapper JSON_MAPPER;
@@ -54,8 +57,43 @@ public class ObjectMappers {
 
 
 
 
     @SneakyThrows
     @SneakyThrows
-    public static String toJsonString(Object data){
+    public static String toJsonString(Object data) {
         return JSON_MAPPER.writeValueAsString(data);
         return JSON_MAPPER.writeValueAsString(data);
     }
     }
 
 
+    @SneakyThrows
+    public static byte[] toJsonBytes(Object data) {
+        return JSON_MAPPER.writeValueAsBytes(data);
+    }
+
+    @SneakyThrows
+    public static <T> T parseJson(byte[] data, Class<T> type) {
+        return JSON_MAPPER.readValue(data, type);
+    }
+
+    @SneakyThrows
+    public static <T> T parseJson(InputStream data, Class<T> type) {
+        return JSON_MAPPER.readValue(data, type);
+    }
+
+    @SneakyThrows
+    public static <T> T parseJson(String data, Class<T> type) {
+        return JSON_MAPPER.readValue(data, type);
+    }
+
+    @SneakyThrows
+    public static <T> List<T> parseJsonArray(InputStream data, Class<T> type) {
+        return JSON_MAPPER.readerForListOf(type).readValue(data);
+    }
+
+    @SneakyThrows
+    public static <T> List<T> parseJsonArray(byte[] data, Class<T> type) {
+        return JSON_MAPPER.readerForListOf(type).readValue(data);
+    }
+
+    @SneakyThrows
+    public static <T> T parseJsonArray(String data, Class<T> type) {
+        return JSON_MAPPER.readerForListOf(type).readValue(data);
+    }
+
 }
 }

+ 7 - 0
jetlinks-components/script-component/pom.xml

@@ -37,6 +37,13 @@
             <scope>test</scope>
             <scope>test</scope>
         </dependency>
         </dependency>
 
 
+        <dependency>
+            <groupId>org.jetlinks.community</groupId>
+            <artifactId>common-component</artifactId>
+            <version>${project.version}</version>
+            <scope>compile</scope>
+        </dependency>
+
     </dependencies>
     </dependencies>
 
 
 
 

+ 96 - 0
jetlinks-components/script-component/src/main/java/org/jetlinks/community/script/AbstractScriptFactory.java

@@ -1,5 +1,9 @@
 package org.jetlinks.community.script;
 package org.jetlinks.community.script;
 
 
+import org.hswebframework.web.utils.DigestUtils;
+import org.jetlinks.community.utils.ObjectMappers;
+import org.jetlinks.reactor.ql.utils.CompareUtils;
+
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
 import javax.script.ScriptEngineFactory;
 import java.io.File;
 import java.io.File;
@@ -9,6 +13,7 @@ import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.math.BigInteger;
 import java.nio.file.Paths;
 import java.nio.file.Paths;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
@@ -113,6 +118,97 @@ public abstract class AbstractScriptFactory implements ScriptFactory {
             return AbstractScriptFactory.this.convertToJavaType(obj);
             return AbstractScriptFactory.this.convertToJavaType(obj);
         }
         }
 
 
+        public long now() {
+            return System.currentTimeMillis();
+        }
+
+        public String now(String format) {
+            return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
+        }
+
+        public byte[] newByteArray(int size) {
+            return new byte[size];
+        }
+
+        public int[] newIntArray(int size) {
+            return new int[size];
+        }
+
+        public long[] newLongArray(int size) {
+            return new long[size];
+        }
+
+        public Map<Object, Object> newMap() {
+            return new HashMap<>();
+        }
+
+        public List<Object> newList() {
+            return new ArrayList<>();
+        }
+
+        public String toJsonString(Object obj){
+            return ObjectMappers.toJsonString(toJavaType(obj));
+        }
+
+        public Object parseJson(String json){
+            return ObjectMappers.parseJson(json,Object.class);
+        }
+
+        public Object max(Object... params) {
+            Object max = null;
+            for (Object param : params) {
+                if (param instanceof Map) {
+                    param = ((Map<?, ?>) param).values();
+                }
+                if (param instanceof Collection) {
+                    param = max(((Collection<?>) param).toArray());
+                }
+                if (max == null) {
+                    max = param;
+                    continue;
+                }
+                if (CompareUtils.compare(max, param) < 0) {
+                    max = param;
+                }
+            }
+            return max;
+        }
+
+        public Object min(Object... params) {
+            Object min = null;
+            for (Object param : params) {
+
+                if (param instanceof Map) {
+                    param = ((Map<?, ?>) param).values();
+                }
+                if (param instanceof Collection) {
+                    param = min(((Collection<?>) param).toArray());
+                }
+                if (min == null) {
+                    min = param;
+                    continue;
+                }
+                if (CompareUtils.compare(min, param) > 0) {
+                    min = param;
+                }
+            }
+            return min;
+        }
+
+        public String sha1Hex(String obj) {
+            if (null == obj) {
+                return null;
+            }
+            return DigestUtils.sha1Hex(obj);
+        }
+
+        public String md5Hex(String obj) {
+            if (null == obj) {
+                return null;
+            }
+            return DigestUtils.md5Hex(obj);
+        }
+
     }
     }
 
 
 }
 }

+ 5 - 4
jetlinks-components/script-component/src/main/java/org/jetlinks/community/script/jsr223/JavaScriptFactory.java

@@ -30,10 +30,11 @@ public abstract class JavaScriptFactory extends Jsr223ScriptFactory {
                      "this.eval = function(e){};" +
                      "this.eval = function(e){};" +
                      "function readFully(){};" +
                      "function readFully(){};" +
                      "function readLine(){};" +
                      "function readLine(){};" +
-                     "const console=_$console;" +
-                     "const utils=_$utils;" +
-                     "const print = function(e){console.log(e)};" +
-                     "const echo = print;");
+                     "var console=_$console;" +
+                     "var utils=_$utils;" +
+                     "var print = function(e){console.log(e)};" +
+                     "var echo = print;" +
+                     "var JSON = {stringify:function(arg){return utils.toJsonString(arg)},parse:function(arg){return utils.parseJson(arg)}};");
 
 
         wrap.add("/*  script start */");
         wrap.add("/*  script start */");
 
 

+ 13 - 0
jetlinks-components/script-component/src/test/java/org/jetlinks/community/script/JavaScriptFactoryTest.java

@@ -230,6 +230,19 @@ public abstract class JavaScriptFactoryTest {
 
 
     }
     }
 
 
+    @Test
+    void testJson() {
+        JavaScriptFactory factory = getFactory();
+
+        Object json = factory.compile(Script.of("testJson",
+                                                "return JSON.parse(JSON.stringify({'a':'1'}))"))
+                             .call();
+
+        assertEquals(Collections.singletonMap("a", "1"), json);
+
+
+    }
+
     public static class MyClazz extends TestExtend {
     public static class MyClazz extends TestExtend {
 
 
     }
     }