wast高性能 Java 庫
wast 是一個輕量級且高性能 java 語言開發(fā)框架和工具包,集成了最快的json庫之一和最快的yaml 解析庫,代碼輕量,無任何依賴。
功能
- 內(nèi)置json模塊在性能評測上遠(yuǎn)遠(yuǎn)超過曾經(jīng)的fastjson和jackson,即使是和fastjson2的最新版本性能評測中也能占據(jù)明顯的領(lǐng)先優(yōu)勢;
- 內(nèi)置yaml解析庫性能是snakeyaml的5-20倍;
- 內(nèi)置表達(dá)式引擎解析性能比現(xiàn)有的spel高0.5倍左右;
JSON使用方法
// 序列化
Map map = new HashMap();
map.put("msg", "hello, light json !");
String result = JSON.toJsonString(map);
// 序列化到文件
Map map = new HashMap();
map.put("msg", "hello, light json !");
JSON.writeJsonTo(map, new File("/tmp/test.json"));
// 格式化輸出
Map map = new HashMap();
map.put("name", "zhangsan");
map.put("msg", "hello, light json !");
JSON.toJsonString(map, WriteOption.FormatOut);
// 反序列化map
String json = "{\"msg\":\"hello, light json !\",\"name\":\"zhangsan\"}";
Map map = (Map) JSON.parse(json);
System.out.println(map);
// 反序列化到指定類型
String json = "{\"msg\":\"hello, light json !\",\"name\":\"zhangsan\"}";
Map map = JSON.parseObject(json, Map.class);
System.out.println(map);
// IO流讀取解析
Map result = null;
// 1 讀取網(wǎng)絡(luò)資源 GET
result = JSON.read(new URL("https://developer.aliyun.com/artifact/aliyunMaven/searchArtifactByGav?groupId=spring&artifactId=&version=&repoId=all&_input_charset=utf-8"), Map.class);
// 2 讀取輸入流
InputStream inputStream = InputStreamTest.class.getResourceAsStream("/sample.json");
result = JSON.read(inputStream, Map.class);
// 3 讀取文件
result = JSON.read(new File("/tmp/smaple.json"), Map.class);
YAML使用方法
// yaml字符串
String yamlStr = StringUtils.fromResource("/yaml/t2.yaml");
// 讀取文檔
YamlDocument yamlDoc = YamlDocument.parse(yamlStr);
// 轉(zhuǎn)換為properties
Properties properties = yamlDoc.toProperties();
System.out.println(properties);
// 轉(zhuǎn)換為map
yamlDoc.toMap();
// 轉(zhuǎn)化為指定bean
YamlTest bean = yamlDoc.toEntity(YamlTest.class);
// 獲取根節(jié)點(diǎn)
YamlNode yamlRoot = yamlDoc.getRoot();
// 查找node
YamlNode nameNode = yamlRoot.get("/metadata/name");
// 獲取/metadata/name
String metadataName = yamlRoot.getPathValue("/metadata/name", String.class);
// 或者 nameNode.getValue();
System.out.println(" metadataName " + metadataName);
// 修改
yamlRoot.setPathValue("/metadata/name", "this is new Value ");
String newMetadataName = (String) nameNode.getValue();
System.out.println(newMetadataName.equals("this is new Value "));
// 反向序列化生成yaml字符串
System.out.println(yamlDoc.toYamlString());
// 輸出到文件
yamlDoc.writeTo(new File("/tmp/test.yaml"));
?表達(dá)式引擎
// 直接運(yùn)行
Expression.eval("1+1"); // 輸出2
Expression.eval("1+1+'a'"); // 輸出2a
Map map = new HashMap();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
Expression.eval("a+b+c",map); // 輸出6
// 解析運(yùn)行
Map map = new HashMap();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
Expression varExpr = Expression.parse("a + b + c"); // 只需要解析一次
varExpr.evaluate(map); // 輸出6
map.put("c", 30);
varExpr.evaluate(map); // 輸出33
// 函數(shù)使用
Map context = new HashMap();
context.put("tip", "1 ");
context.put("name", "zhangsan, %s");
context.put("msg", "hello");
context.put("type", 1);
context.put("a", 1);
context.put("b", 12);
context.put("c", 111);
context.put("B6_AvgCpuUsed", 1.0);
context.put("B5_AvgCpuUsed", 2.0);
context.put("B4_AvgCpuUsed", 3.0);
context.put("vars", new String[] {"hello"});
EvaluateEnvironment evaluateEnvironment = EvaluateEnvironment.create(context);
evaluateEnvironment.registerStaticMethods(Math.class, String.class);
evaluateEnvironment.registerFunction("min", new ExprFunction<Object, Number>() {
@Override
public Number call(Object... params) {
Arrays.sort(params);
return (Number) params[params.length - 1];
}
});
System.out.println( Expression.eval("@min(@sum(a,b,c), 50, 125, 2, -11)", evaluateEnvironment));
System.out.println( Expression.eval("@max(@sum(a,b,c), 50, 125, 55, 152)", evaluateEnvironment));
評論
圖片
表情
