wast is a lightweight and high-performance java language development framework and toolkit that integrates one of the fastest json libraries and the fastest yaml parsing library, with lightweight code and no dependencies.
Function
- The built-in json module far surpasses the former fastjson and jackson in performance evaluation, and can occupy a clear lead even in the performance evaluation of the latest version of fastjson2;
- The built-in yaml parsing library performance is5-20 times of snakeyaml;
- The parsing performance of the built-in expression engine is about 0.5 times higher than the existing spel;
How to use 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 读取网络资源 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);
How to use YAML
// yaml字符串
String yamlStr = StringUtils.fromResource("/yaml/t2.yaml");
// 读取文档
YamlDocument yamlDoc = YamlDocument.parse(yamlStr);
// 转换为properties
Properties properties = yamlDoc.toProperties();
System.out.println(properties);
// 转换为map
yamlDoc.toMap();
// 转化为指定bean
YamlTest bean = yamlDoc.toEntity(YamlTest.class);
// 获取根节点
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"));
Expression engine
// 直接运行
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
// 解析运行
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
// 函数使用
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));
#wast #Homepage #Documentation #Downloads #High #Performance #Java #Library #News Fast Delivery