Wast
WAST is a high-performance Java toolset library package that includes JSON, YAML, CSV, HttpClient, JDBC and EL engines
Install / Use
/learn @wycst/WastREADME
介绍
WAST是一个高性能Java工具集库包,包括JSON、YAML、CSV、HttpClient、JDBC和EL引擎.
性能评测数据(github: https://github.com/wycst/wast-jmh-test) <br> https://gitee.com/xiaoch0209/wast-jmh-test
2022-09-25 json性能测试数据 <br> https://gitee.com/xiaoch0209/wast-jmh-test/blob/main/README_0925_json.md
2022-12-14 表达式引擎<br> https://gitee.com/xiaoch0209/wast-jmh-test/blob/main/README_1214_EL.md
此库不依赖任何第三方库,对JDK版本要求1.6及以上即可。
Maven
通用maven坐标
<dependency>
<groupId>io.github.wycst</groupId>
<artifactId>wast</artifactId>
<version>0.0.29.1</version>
</dependency>
JSON
1 java语言整体性能最快的json库之一;<br> 2 支持IO流文件读写,JSON节点树按需解析,序列化格式化,驼峰下划线自动转换,实体类解析绑定;<br> 3 支持自定义序列化和反序列化;<br> 4 支持JSON的xpath提取功能;<br> 5 支持基本JSONSchema校验;<br> 6 没有漏洞风险;<br>
YAML
1 目前java语言解析yaml最快的库之一;<br> 2 支持文件流,URL, 字符数组,字符串等解析;<br> 3 支持常用yaml语法以及类型转换;<br> 4 内置Yaml节点模型,支持路径查找(v0.0.4+);<br> 5 支持yaml反向转换为字符串或者文件(v0.0.4+);<br>
表达式引擎
1 java表达式引擎性能最快之一;<br> 2 支持java中所有的操作运算符(加减乘除余,位运算,逻辑运算,字符串+);<br> 3 支持**指数运算(java本身不支持); <br> 4 支持函数以及自定义函数实现; <br> 5 科学记数法支持,16进制,8进制等解析,支持大数运算(BigDecimal);<br> 6 支持三目运算;<br> 7 支持超长文本表达式执行;<br> 8 支持表达式编译模式运行;<br> 9 没有漏洞风险;<br>
JDBC
1 集成了类似JdbcTemplate,Mybatis-Plus或者JPA等操作习惯的api; <br> 2 代码轻量,简单易用;<br> 3 支持面向原始sql,sql模版,对象管理等三种操作模式用,后两种模式完全能避免SQL注入漏洞(根源解决);<br> 4 理论上支持所有提供JDBC协议的数据库;<br>
HttpClient
1 当前只支持http/1.1;<br> 2 底层核心为URLConnection,封装了http客户端常用的API;<br> 3 支持文件上传(已封装API),文件下载也能轻松处理支持;<br> 4 支持nacos和consul作为ServerZone提供源,可以通过服务实例来访问请求;<br> 5 支持负载均衡(客户端)和高可用访问调用;<br> 6 支持SSE调用;<br>
JSON
常用对象json序列化
Map map = new HashMap();
map.put("msg", "hello, wastjson !");
String result = JSON.toJsonString(map);
对象序列化到文件
Map map = new HashMap();
map.put("msg", "hello, wastjson !");
JSON.writeJsonTo(map, new File("/tmp/test.json"));
格式序列化
Map map = new HashMap();
map.put("name", "zhangsan");
map.put("msg", "hello, wastjson !");
JSON.toJsonString(map, WriteOption.FormatOut);
{
"msg":"hello, wastjson !",
"name":"zhangsan"
}
反序列化
String json = "{\"msg\":\"hello, wastjson !\",\"name\":\"zhangsan\"}";
Map map = (Map) JSON.parse(json);
System.out.println(map);
{msg=hello, wastjson !, name=zhangsan}
指定类型反序列化
String json = "{\"msg\":\"hello, wastjson !\",\"name\":\"zhangsan\"}";
Map map = JSON.parseObject(json, Map.class);
System.out.println(map);
{msg=hello, wastjson !, name=zhangsan}
个性化支持
1.针对实体bean的属性可以添加注解@JsonSerialize和@JsonDeserialize来实现定制化(使用见test模块custom目录例子)。 </br>
2.通过JSON静态方法针对不同的类型来做个性化支持或者功能支持,非常方便;
JSON.registerTypeMapper(java.time.ZoneId.class, new JSONTypeMapper<ZoneId>() {
@Override
public ZoneId readOf(Object any) {
return any == null ? null : ZoneId.of((String) any);
}
@Override
public JSONValue<?> writeAs(ZoneId zoneId, JSONConfig jsonConfig) throws Exception {
return zoneId == null ? null : JSONValue.of(zoneId.getId());
}
});
基于输入流的读取解析
Map result = null;
// 1 url
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 stream
InputStream inputStream = InputStreamTest.class.getResourceAsStream("/sample.json");
result = JSON.read(inputStream, Map.class);
// 3 file
result = JSON.read(new File("/tmp/smaple.json"), Map.class);
基于输入流的按需解析
提供JSONReader类可按需解析一个输入流,自定义解析,可随时终止(不用将整个文件流读完)。
final JSONReader reader = JSONReader.from(new File(f));
reader.read(new JSONReaderHook() {
@Override
public void parseValue(String key, Object value, Object host, int elementIndex, String path, int type) throws Exception {
if(path.equals("/features/1/properties/STREET")) {
System.out.println(value);
abort();
}
}
}, true);
强大的JSONNode功能
1、支持对大文本json的懒加载按需解析功能,当需要读取一个大文本json中一个或多个属性值时非常有用;<br> 2、支持上下文查找;<br> 3、支持在大文本json中提取部分内容作为解析上下文结果,使用JSONNode.from(source, path);<br> 4、支持对节点的属性修改,删除等,节点的JSON反向序列化;<br> 5、支持提取功能(v0.0.2+支持),参考JSONPath;
使用'/'作为路径的分隔符,数组下标使用n访问支持*, n+, n-,n等复合下标访问,例如/store/book/*/author
String json = "{\"name\": \"li lei\", \”properties\": {\"age\": 23}}";
JSONNode node = JSONNode.parse(json);
// 获取当前节点(根节点)的name属性
String name = node.getChildValue("name", String.class);
// 通过getPathValue方法可以获取任意路径上的值
int age = node.getPathValue("/properties/age", int.class);
// 通过path可以定位到任何路径节点
JSONNode anyNode = JSONNode.get(path);
// 通过root方法可以在任何节点回到根节点
JSONNode root = anyNode.root();
// root == node true
// 根据路径局部解析
JSONNode propertiesRoot = JSONNode.from(json, "/properties");
// 局部解析懒加载(一般在获取某个数组的长度或者对象的keys等特别适用)
JSONNode propertiesRoot = JSONNode.from(json, "/properties", true);
propertiesRoot.keyNames();
String json2 = `{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"attr": {
"pos": "p1"
},
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"attr": {
"pos": "p2"
},
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"attr": {
"pos": "p3"
},
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"attr": {
"pos": "p4"
},
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}`
;
// 直接提取所有的author使用*
List authors = JSONNode.extract(json2, "/store/book/*/author");
// 提取第2本书的作者author使用指定的下标n
String author = JSONNode.extract(json2, "/store/book/1/author").get(1);
(或者 JSONNode.from(json2, "/store/book/1/author").getStringValue();性能大体一致)
// 提取前2本书的作者使用下标n-(包含n)
List authors = JSONNode.extract(json2, "/store/book/1-/author").get(1);
// 提取从第2本书开始后面所有的作者使用下标n+(包含n)
List authors1 = JSONNode.extract(json2, "/store/book/1+/author");
// 使用collect可以支持递归过滤
List authors2 = JSONNode.collect(json2, "//book/1+/author");
JSONSchema
schemaJson
{
"properties": {
"name": {
"must": true,
"rules": [
{
"expression": "value.indexOf('test') > -1",
"message": "必须包含test"
}
],
"type": "string"
},
"age": {
"maximum": 100,
"minimum": 20,
"type": "number"
},
"key": {
"type": [
"number",
"boolean"
]
}
},
"type": "object"
}
校验
String schemaJson = "..."; // schemaJson
JSONSchema schema = JSONSchema.of(schemaJson);
JSONSchemaResult result = schema.validate("{\"name\":\"wyctesRtst\",\"age\": 33, \"key\": false}");
System.out.println(result);
SpringBoot(Spring MVC) 集成
supports/json-springmvc/JSONHttpMessageConverter.java
@Configuration
public class AppConfiguration implements WebMvcConfigurer {
@Bean
public HttpMessageConverters jsonHttpMessageConverters() {
JSONHttpMessageConverter jsonHttpMessageConverter = new JSONHttpMessageConverter();
jsonHttpMessageConverter.setWriteOptions(WriteOption... writeOptions);
jsonHttpMessageConverter.setReadOptions(ReadOption... readOptions);
return new HttpMessageConverters(jsonHttpMessageConverter);
}
}
序列化和反序列化支持配置
序列化配置枚举类:WriteOption
| 枚举值 | 说明 | | ------------ |--------------------------------------------------| | FormatOut | 格式化缩进输出 | | FormatOutColonSpace | 格式化缩进支持冒号后面补一个空格更加美观 | | FormatIndentUseTab | 使用tab符来缩进美化,当FormatOut启用时默认开启使用此模式 | | FormatIndentUseSpace | 使用空格符(4个空格)来缩进美化JSON | | FormatIndentUseSpace8 | 使用8个空格符代表一个缩进级别进行美化JSON | | FullProperty | 输出完整的属性字段 | | WriteDateAsTime | 默认将日期格式化输出,配置此项可以序列化为时间戳 | | SkipCircularReference | 开始后探测序列化是否会存在循环引用造成的死循环 | | BytesArrayToHex | 默认情况下byte数组会输出为base64字符串,开启配置后将bytes数组输出为16进制字符串 | | BytesArrayToNative | 默认情况下byte数组会输出为base64字符串,开启配置后将bytes数组输出原生字节数组 | | SkipGetterOfNoneField | 是否跳过不存在属性域的getter方法序列化 | | KeepOpenStream
Related Skills
node-connect
339.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
