<dependency>
<groupID>com.eclipsesource.j2v8</groupID>
<artifactID>j2v8_win32_x86_64</artifactID>
<version>4.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupID>com.eclipsesource.j2v8</groupID>
<artifactID>j2v8_linux_x86_64</artifactID>
<version>4.6.0</version>
<scope>compile</scope>
</dependency>
package org.rx.util;import com.alibaba.fastJson.JsONObject;import com.eclipsesource.v8.V8;import com.eclipsesource.v8.V8Object;import com.Google.common.annotations.Beta;import com.Google.common.base.Strings;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.rx.annotation.ErrorCode;import org.rx.common.*;import java.net.URL;import java.nio.file.Path;import java.nio.file.Paths;import java.util.Collections;import java.util.HashMap;import java.util.Map;import static org.rx.common.Contract.*;@Beta@Slf4jpublic final class JsonMapper extends disposable { private static class V8Console { public voID log(String message) { log.info("[V8] {}",message); } public voID error(String message) { log.error("[V8] {}",message); } } public static final JsonMapper Default = new JsonMapper("Jscript/"); private static final String scriptFunc = "(function(){var $={},$val=JsON.parse(_x); %s; return JsON.stringify($);})()"; private Map<String,Object> settings; private Lazy<V8> runtime; public JsonMapper(String configPath) { settings = refreshSettings(configPath); runtime = new Lazy<>(() -> { V8 v8 = V8.createV8Runtime(); V8Object v8Console = new V8Object(v8); v8.add("console",v8Console); V8Console console = new V8Console(); Class[] argTypes = new Class[]{String.class}; v8Console.registerJavaMethod(console,"log","log",argTypes); v8Console.registerJavaMethod(console,"error","error",argTypes); v8.executeScript("console.log(‘V8 start..‘);"); return v8; }); } @OverrIDe protected voID freeObjects() { if (runtime.isValueCreated()) { runtime.getValue().release(); runtime = null; } } public <F,T> T convertTo(Class<T> toType,F from) { require(toType,from); return convertTo(toType,from.getClass().getname(),from); } public <T> T convertTo(Class<T> toType,String configKey,Object sourceValue) { require(toType,configKey); String script = getScript(toType,configKey); V8 v8 = runtime.getValue(); v8.add("_x",toJsonString(sourceValue)); String jResult = v8.executeStringScript(String.format(scriptFunc,script)); return JsONObject.parSEObject(jResult,toType); } @ErrorCode(value = "keyError",messageKeys = {"$key"}) private String getScript(Class tType,String key) { String tKey = tType.getname(); Map<String,Object> v = as(settings.get(tKey),Map.class); if (v == null) { throw new SystemException(values(tKey),"keyError"); } String script = (String) v.get(key); if (Strings.isNullOrEmpty(script)) { throw new SystemException(values(tKey + "." + key),"keyError"); } return script; } @SneakyThrows private Map<String,Object> refreshSettings(String configPath) { URL path = App.getClassLoader().getResource(configPath); if (path == null) { return Collections.emptyMap(); } Map<String,Object> map = new HashMap<>(); for (Path p : App.fileStream(Paths.get(path.toURI()))) { try { map.putAll(App.loadYaml(p.toString())); } catch (Exception e) { log.error("refreshSettings",e); } } return map; }}
org.rx.util.DemoBean: demoKey: > $.ID=$val.ID;总结
以上是内存溢出为你收集整理的基于V8的JsonMapper全部内容,希望文章能够帮你解决基于V8的JsonMapper所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)