yaml解析工具类(java)

这是我参与11月更文挑战的第16天,活动详情查看:2021最后一次更文挑战

山有峰顶,海有彼岸,漫漫长路,终有回转,余味苦涩,终会有回甘。别被眼前的磨难打败了,或许光明就在你放弃前的那一刻。带着愉快的心情做一个愉快的梦,醒来后,又是新的一天。

简介

这是基于snakeyaml实现的一个读取yaml配置的工具类.
主要功能是获取指定层级之下的数据.

我不知道当初为什么写这个类了,但是我知道,我要是不把他发出来我就是白写了😂

主要功能和使用方法

demo.yml

1
2
3
4
5
6
7
8
9
10
11
yaml复制代码  zdc:
config:
key: value
list:
- 张三
- 李四
map:
- name: 张三map
age: 12
- name: 李四map
age: 121
  1. 支持获取指定层级之后的内容,并返回Map形式或Obj形式
    new ConfigBean("指定文件").prefix("zdc.config").getMap()

new ConfigBean("指定文件").prefix("zdc.config").getObj()

  1. 支持获取指定层级之后的对象数据,并格式化为指定类,如”zdc.config” :
    new ConfigBean("指定文件").prefix("zdc.config").getT(A.class)
  2. 支持获取list中指定数据,如”zdc.config.list.1” :
    new ConfigBean().prefix("zdc.config.list.1").getObj()
  3. 支持获取list中指定数据之后的数据,如”zdc.config.map.1.name”

new ConfigBean().prefix("zdc.config.list.1").getString()

具体代码

需要的maven包

1
2
3
4
5
6
7
8
9
10
xml复制代码<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.3</version>
</dependency>

java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
java复制代码package zdc.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
* 读取resources下的yaml配置类
*
*使用的maven包
* <dependency>
* <groupId>org.yaml</groupId>
* <artifactId>snakeyaml</artifactId>
* <version>1.25</version>
* </dependency>
* map->bean使用此包
* <dependency>
* <groupId>commons-beanutils</groupId>
* <artifactId>commons-beanutils-core</artifactId>
* <version>1.8.3</version>
* </dependency>
*
*使用方法
*eg:
* zdc:
* config:
* key: value
* list:
* - 张三
* - 李四
* map:
* - name: 张三map
* age: 12
* - name: 李四map
* age: 121
*
*支持获取指定层级之后的数据,如"zdc.config" : new ConfigBean().prefix("zdc.config").getObj()
*
*支持获取list中指定数据,如"zdc.config.list.1" : new ConfigBean().prefix("zdc.config.list.1").getObj()
*
*支持获取list中指定数据之后的数据,如"zdc.config.map.1.name" :new ConfigBean().prefix("zdc.config.list.1").getString()
*
* @author ZDC
*/
public class ConfigBean {

/**
* 读取的资源名
*/
private String fileName ="application.yml";
/**
* 获取的对象
*/
private Object temp;


/**
* 创建一个资源获取对象,默认获取resources下的application.yml文件
*/
public ConfigBean() {
this.load();
}

/**
* 创建一个资源获取对象,默认获取resources下的fileName文件
* @param fileName
*/
public ConfigBean(String fileName) {
this.fileName =fileName;
this.load();
}

/**
* 加载指定的文件
*/
private ConfigBean load() {
Yaml yaml = new Yaml();
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream(this.fileName);
this.temp= yaml.load(inputStream);
return this;
}



/**
* eg "zdc.config.list"
* eg ""
* @param prefix
*/
public ConfigBean prefix(String prefix){

if(prefix==null || "".equals(prefix.trim())){
return this;
}
//获取层级关系
String[] keys = prefix.trim().split("\\.");
for (String key : keys) {
//判断数据类型
if(this.temp instanceof Map){
this.temp= ((Map) this.temp).get(key);
}
else if(this.temp instanceof List){
if (isNumeric(key)) {
this.temp= ((List) this.temp).get(Integer.parseInt(key));
}else{
throw new RuntimeException(String.format("当前层级类型为List,不能使用[%s]获取子集数据",key));
}
}else{
throw new RuntimeException("暂时没有解析该类型或不支持再次解析");
}
}
return this;
}

/**
* 返回对象类型的数据,可能是List,Map,Obj
* @return
*/
public Object getObj(){
return this.temp;
}

/**
* 返回Map类型的数据
* @return
*/
public Map getMap() {
if(this.temp instanceof Map){
return (Map)this.temp;
}
return null;
}

/**
* 返回List类型的数据
* @return
*/
public List getList() {
if(this.temp instanceof List){
return (List)this.temp;
}
return null;
}

/**
* 返回对象类型数据,如果集成其他类库可以直接调用其他类库的map2bean方法 该处引用的是 commons-beanutils-core ,可以根据自己本地环境替换
*
* @param clazz
* @param <T>
* @return
*/
public <T> T getT(Class<T> clazz) throws Exception {
T obj = clazz.newInstance();
Map map = this.getMap();
BeanUtils.populate(obj,map);
return obj;
}

/**
* 返回String类型的数据
* @return
*/
public String getString() {
return this.temp==null ? "":this.temp.toString();
}

/**
* 返回Integer类型的数据
* @return
*/
public Integer getInteger() {
String string = getString();
return string!=null? Integer.parseInt(string):null;
}

//TODO 可以自定也解析其他类型



/**
* 判断是否是数字
* @param cs
* @return
*/
public static boolean isNumeric(final CharSequence cs) {
if (cs == null || cs.length() == 0) {
return false;
}
final int sz = cs.length();
for (int i = 0; i < sz; i++) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}


}

这个系列的文章,总体来说是一些杂乱的记录.

其中的内容可能是在之前某个需要的时候记录下来的,当时需要,当时很有用.

但是在后续更多的时间里,他被封禁到了小角落.

你看不看,他都在那里,他并非一无是处.

你看到这里就给个三连吧

1
2
3
4
arduino复制代码    作者:ZOUZDC
链接:https://juejin.cn/post/7028963866063306760
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%