-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonConvert.java
More file actions
227 lines (198 loc) · 7.67 KB
/
Copy pathJsonConvert.java
File metadata and controls
227 lines (198 loc) · 7.67 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.linkcircle.project;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* @author:老薛
* @version:1.1
* @date:2023/8/17
* @description:
* @vx:laoxue004
*/
public class JsonConvert<T> {
// 待转换的实体对象的class
private Class<T> clazz;
private int currentIndex = 0;
private String json;
public JsonConvert(Class<T> clazz,String json) {
this.clazz = clazz;
this.json = json.replaceAll("\\s", "");
}
// 解析json字符串,将其封装到一个map集合中
private Map<String, Object> parseInternal() {
if(!check(json))
throw new IllegalArgumentException("json format is error" + json);
Map<String, Object> resultMap = new HashMap<>();
currentIndex++; // Skip the opening '{'
while (currentIndex < json.length()) {
char c = json.charAt(currentIndex);
if (c == '}') {
currentIndex++; // Skip the closing '}'
break;
} else if (c == '"') {
currentIndex++; // Skip the opening '"'
String key = parseKey();
currentIndex++; // Skip the colon ':'
Object value = parseValue();
resultMap.put(key, value);
if (currentIndex < json.length() && json.charAt(currentIndex) == ',') {
currentIndex++; // Skip the comma ','
}
} else {
currentIndex++;
}
}
return resultMap;
}
private String parseKey() {
int endIndex = json.indexOf('"', currentIndex);
String value = json.substring(currentIndex, endIndex);
currentIndex = endIndex + 1; // Skip the closing '"'
return unescape(value);
}
private String unescape(String value) {
return value.replaceAll("\\\\\"", "\"");
}
private String parseString(){
int endIndex = json.indexOf('"', currentIndex+1);
String value = json.substring(currentIndex+1, endIndex);
currentIndex = endIndex + 1; // Skip the closing '"'
return unescape(value);
}
private String parseList(){
int endIndex = json.indexOf('"', currentIndex+1);
String value = json.substring(currentIndex, endIndex);
currentIndex = endIndex + 1; // Skip the closing '"'
return unescape(value);
}
private Object parseValue() {
char c = json.charAt(currentIndex);
if (c == '"') {
return parseString();
} else if (c == '{') {
return parseInternal();
} else if (c == '[') {
currentIndex++; // Skip the opening '['
return parseArray();
} else {
int endIndex = json.indexOf(',', currentIndex);
int nextCommaIndex = json.indexOf(',', endIndex + 1);
int endIndexObject = json.indexOf('}', currentIndex);
int endIndexArray = json.indexOf(']', currentIndex);
if (endIndex == -1) {
endIndex = json.length();
}
if (nextCommaIndex != -1 && nextCommaIndex < endIndexObject && nextCommaIndex < endIndexArray) {
endIndex = nextCommaIndex;
} else if (endIndexObject != -1 && endIndexObject < endIndexArray) {
endIndex = endIndexObject;
} else if (endIndexArray != -1) {
endIndex = endIndexArray;
}
String value = json.substring(currentIndex, endIndex);
currentIndex = endIndex;
return value;
}
}
private List<Object> parseArray() {
List<Object> list = new ArrayList<>();
while (currentIndex < json.length()) {
char c = json.charAt(currentIndex);
if (c == ']') {
currentIndex++; // Skip the closing ']'
break;
} else if (c == '"') {
currentIndex++; // Skip the opening '"'
String value = parseList();
list.add(value);
if (currentIndex < json.length() && json.charAt(currentIndex) == ',') {
currentIndex++; // Skip the comma ','
}
} else if (c == '{') {
currentIndex++; // Skip the opening '{'
list.add(parseInternal());
if (currentIndex < json.length() && json.charAt(currentIndex) == ',') {
currentIndex++; // Skip the comma ','
}
} else {
currentIndex++;
}
}
return list;
}
/**
* 检查json字符串的格式是否满足要求
* @param json
* @return
*/
public boolean check(String json) {
if(!json.startsWith("{") || !json.endsWith("}")) {
return false;
}
Stack<Character> stack = new Stack<>();
for(char c : json.toCharArray()) {
if (c == '{' || c == '[')
stack.push(c);
else if(c=='"' && (stack.peek() != '"'))
stack.push(c);
else if (c=='"' && (stack.peek() == '"'))
stack.pop();
else if (c == '}' || c == ']' )
if (stack.isEmpty() || !isPair(stack.pop(), c))
return false;
}
return stack.isEmpty();
}
boolean isPair(char left, char right) {
if (left == '{' && right == '}') return true;
return left == '[' && right == ']';
}
// 将map中的内容转换为对应的T类型
public T convertMapToObject() {
Map<String, Object> stringObjectMap = parseInternal();
try {
T instance = clazz.getDeclaredConstructor().newInstance();
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(SeriesOtherNames.class)) {
SeriesOtherNames annotation = field.getAnnotation(SeriesOtherNames.class);
String[] names = annotation.names();
Object value = getValueFromMap(stringObjectMap, names);
if (value != null) {
field.setAccessible(true);
Object convertedValue = convertToFieldType(field.getType(),value);
field.set(instance, convertedValue);
}
}else{
Object value = stringObjectMap.get(field.getName());
if (value != null) {
field.setAccessible(true);
Object convertedValue = convertToFieldType(field.getType(),value);
field.set(instance, convertedValue);
}
}
}
return instance;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private Object getValueFromMap(Map<String, Object> map, String[] names) {
for (String name : names) {
if (map.containsKey(name)) {
return map.get(name);
}
}
return null;
}
private Object convertToFieldType(Class<?> fieldType, Object value) {
if (fieldType.equals(String.class)) {
return value.toString();
} else if (fieldType.equals(Integer.class) || fieldType.equals(int.class)) {
return Integer.parseInt(value.toString());
} else if (fieldType.equals(Double.class) || fieldType.equals(double.class)) {
return Double.parseDouble(value.toString());
}
// Add more type conversions as needed...
return value;
}
}