<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
@Slf4j
public 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;

最新文章

  1. Spring 事务
  2. WdatePicker 没有权限 不能执行已释放 Script 的代码
  3. linux磁盘存储命令 磁盘存储命令
  4. 攻破JAVA NIO技术壁垒
  5. 【T_SQL】 基础
  6. Android开发--Button的应用
  7. L1范式和L2范式的区别
  8. linux诡异的半连接(SYN_RECV)队列长度
  9. iOS Sqlite3 Demo 及 FMDB Demo
  10. ie中弹出框中元素的定位
  11. 关于ASP.NET 中站点地图sitemap 的使用
  12. sicily9162. RAZLIKA
  13. 检测CSS属性 是否支持
  14. Django基础-01
  15. SQL中的float类型的数据
  16. poj1002 大数的 n的m次
  17. springmvc接收数组类型参数
  18. 【两分钟教程】如何更改Xcode项目名称
  19. 使用pip安装Scrapy出错
  20. 【转】Android中获取应用程序(包)的信息-----PackageManager的使用(一)

热门文章

  1. VIEW当中自定义属性的使用
  2. java 京东登录
  3. HTML5: HTML5 Web SQL 数据库
  4. JVM运行时数据区及对象在内存中初始化的过程
  5. Catch and Buffer
  6. promise的基本用法
  7. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式
  8. vue组件库的基本开发步骤
  9. svnlook - Subversion 仓库检索工具
  10. 关于STLINK