Skip to content

Commit 0550faf

Browse files
authored
use jackson instead of gson (#846)
* use jackson replace of gson * update codeql
1 parent b98bb2d commit 0550faf

File tree

25 files changed

+176
-170
lines changed

25 files changed

+176
-170
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
name: "CodeQL"
1313

1414
on:
15-
pull_request:
16-
branches: [ master, dev ]
17-
paths:
18-
- '**/*.java'
19-
- '**/*.ts'
15+
schedule:
16+
- cron: '21 13 * * 4'
2017

2118
jobs:
2219
analyze:

alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefineControllerTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.dromara.hertzbeat.common.entity.alerter.AlertDefineMonitorBind;
66
import org.dromara.hertzbeat.common.entity.manager.Monitor;
77
import org.dromara.hertzbeat.common.util.CommonConstants;
8-
import org.dromara.hertzbeat.common.util.GsonUtil;
8+
import org.dromara.hertzbeat.common.util.JsonUtil;
99
import org.junit.jupiter.api.BeforeEach;
1010
import org.junit.jupiter.api.Test;
1111
import org.junit.jupiter.api.extension.ExtendWith;
@@ -85,7 +85,7 @@ void addNewAlertDefine() throws Exception {
8585
// 模拟客户端往服务端发送请求
8686
mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define")
8787
.contentType(MediaType.APPLICATION_JSON)
88-
.content(GsonUtil.toJson(this.alertDefine)))
88+
.content(JsonUtil.toJson(this.alertDefine)))
8989
.andExpect(status().isOk())
9090
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
9191
.andReturn();
@@ -95,7 +95,7 @@ void addNewAlertDefine() throws Exception {
9595
void modifyAlertDefine() throws Exception {
9696
mockMvc.perform(MockMvcRequestBuilders.put("/api/alert/define")
9797
.contentType(MediaType.APPLICATION_JSON)
98-
.content(GsonUtil.toJson(this.alertDefine)))
98+
.content(JsonUtil.toJson(this.alertDefine)))
9999
.andExpect(status().isOk())
100100
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
101101
.andReturn();
@@ -126,7 +126,7 @@ void getAlertDefine() throws Exception {
126126
void deleteAlertDefine() throws Exception {
127127
mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/define/" + this.alertDefine.getId())
128128
.contentType(MediaType.APPLICATION_JSON)
129-
.content(GsonUtil.toJson(this.alertDefine)))
129+
.content(JsonUtil.toJson(this.alertDefine)))
130130
.andExpect(status().isOk())
131131
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
132132
.andReturn();
@@ -136,7 +136,7 @@ void deleteAlertDefine() throws Exception {
136136
void applyAlertDefineMonitorsBind() throws Exception {
137137
mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define/" + this.alertDefine.getId() + "/monitors")
138138
.contentType(MediaType.APPLICATION_JSON)
139-
.content(GsonUtil.toJson(this.alertDefineMonitorBinds)))
139+
.content(JsonUtil.toJson(this.alertDefineMonitorBinds)))
140140
.andExpect(status().isOk())
141141
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
142142
.andReturn();
@@ -155,4 +155,4 @@ void getAlertDefineMonitorsBind() throws Exception {
155155
.andExpect(jsonPath("$.data[0].monitor.id").value(alertDefineMonitorBinds.get(0).getMonitor().getId()))
156156
.andReturn();
157157
}
158-
}
158+
}

alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefinesControllerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.dromara.hertzbeat.alert.service.AlertDefineService;
44
import org.dromara.hertzbeat.common.entity.alerter.AlertDefine;
55
import org.dromara.hertzbeat.common.util.CommonConstants;
6-
import org.dromara.hertzbeat.common.util.GsonUtil;
6+
import org.dromara.hertzbeat.common.util.JsonUtil;
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
99
import org.junit.jupiter.api.extension.ExtendWith;
@@ -126,9 +126,9 @@ public boolean matches(PageRequest pageRequestMidden) {
126126
void deleteAlertDefines() throws Exception {
127127
this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/defines")
128128
.contentType(MediaType.APPLICATION_JSON)
129-
.content(GsonUtil.toJson(ids)))
129+
.content(JsonUtil.toJson(ids)))
130130
.andExpect(status().isOk())
131131
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
132132
.andReturn();
133133
}
134-
}
134+
}

alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertsControllerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.dromara.hertzbeat.common.entity.alerter.Alert;
66
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
77
import org.dromara.hertzbeat.common.util.CommonConstants;
8-
import org.dromara.hertzbeat.common.util.GsonUtil;
8+
import org.dromara.hertzbeat.common.util.JsonUtil;
99
import org.junit.jupiter.api.BeforeEach;
1010
import org.junit.jupiter.api.Test;
1111
import org.junit.jupiter.api.extension.ExtendWith;
@@ -158,11 +158,11 @@ void addNewAlertReport() throws Exception {
158158
MockMvcRequestBuilders
159159
.post("/api/alerts/report")
160160
.contentType(MediaType.APPLICATION_JSON)
161-
.content(GsonUtil.toJson(AlertReport.builder().build()))
161+
.content(JsonUtil.toJson(AlertReport.builder().build()))
162162
)
163163
.andExpect(status().isOk())
164164
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
165165
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}"))
166166
.andReturn();
167167
}
168-
}
168+
}

collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusMatrixParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.dromara.hertzbeat.common.entity.job.protocol.HttpProtocol;
88
import org.dromara.hertzbeat.common.entity.message.CollectRep;
99
import org.dromara.hertzbeat.common.util.CommonConstants;
10-
import org.dromara.hertzbeat.common.util.GsonUtil;
10+
import org.dromara.hertzbeat.common.util.JsonUtil;
1111
import lombok.NoArgsConstructor;
1212

1313
import java.math.BigDecimal;
@@ -23,7 +23,7 @@ public class PrometheusMatrixParser extends AbstractPrometheusParse {
2323
@Override
2424
public Boolean checkType(String responseStr) {
2525
try {
26-
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
26+
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
2727
if (DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType())) {
2828
return true;
2929
}
@@ -35,7 +35,7 @@ public Boolean checkType(String responseStr) {
3535

3636
@Override
3737
public void parse(String resp, List<String> aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
38-
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(resp, PromVectorOrMatrix.class);
38+
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
3939
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
4040
for (PromVectorOrMatrix.Result r : result) {
4141
for (List<Object> value : r.getValues()) {

collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusVectorParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.dromara.hertzbeat.common.entity.job.protocol.HttpProtocol;
88
import org.dromara.hertzbeat.common.entity.message.CollectRep;
99
import org.dromara.hertzbeat.common.util.CommonConstants;
10-
import org.dromara.hertzbeat.common.util.GsonUtil;
10+
import org.dromara.hertzbeat.common.util.JsonUtil;
1111
import lombok.NoArgsConstructor;
1212

1313
import java.math.BigDecimal;
@@ -23,7 +23,7 @@ public class PrometheusVectorParser extends AbstractPrometheusParse {
2323
@Override
2424
public Boolean checkType(String responseStr) {
2525
try {
26-
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
26+
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
2727
if (DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType())) {
2828
return true;
2929
}
@@ -37,7 +37,7 @@ public Boolean checkType(String responseStr) {
3737
public void parse(String resp, List<String> aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
3838
boolean setTimeFlag = false;
3939
boolean setValueFlag = false;
40-
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(resp, PromVectorOrMatrix.class);
40+
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
4141
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
4242
for (PromVectorOrMatrix.Result r : result) {
4343
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();

collector/src/main/java/org/dromara/hertzbeat/collector/util/CollectUtil.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
package org.dromara.hertzbeat.collector.util;
1919

20+
import com.fasterxml.jackson.core.type.TypeReference;
2021
import com.google.gson.*;
21-
import com.google.gson.reflect.TypeToken;
2222
import org.dromara.hertzbeat.common.entity.job.Configmap;
2323
import org.dromara.hertzbeat.common.util.CommonConstants;
24-
import org.dromara.hertzbeat.common.util.GsonUtil;
24+
import org.dromara.hertzbeat.common.util.JsonUtil;
2525
import lombok.extern.slf4j.Slf4j;
2626

27-
import java.lang.reflect.Type;
2827
import java.util.Arrays;
2928
import java.util.Iterator;
3029
import java.util.List;
@@ -291,8 +290,7 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map
291290
Configmap param = configmap.get(key);
292291
if (param != null && param.getType() == (byte) 3) {
293292
String jsonValue = (String) param.getValue();
294-
Type type = new TypeToken<Map<String, String>>(){}.getType();
295-
Map<String, String> map = GsonUtil.fromJson(jsonValue, type);
293+
Map<String, String> map = JsonUtil.fromJson(jsonValue, new TypeReference<>() {});
296294
if (map != null) {
297295
map.forEach((name, value) -> {
298296
if (name != null && !"".equals(name.trim())) {

common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/Alert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.dromara.hertzbeat.common.entity.alerter;
1919

20-
import org.dromara.hertzbeat.common.util.GsonUtil;
20+
import org.dromara.hertzbeat.common.util.JsonUtil;
2121
import io.swagger.v3.oas.annotations.media.Schema;
2222
import lombok.AllArgsConstructor;
2323
import lombok.Builder;
@@ -142,6 +142,6 @@ public class Alert {
142142
@Override
143143
public Alert clone() {
144144
// deep clone
145-
return GsonUtil.fromJson(GsonUtil.toJson(this), Alert.class);
145+
return JsonUtil.fromJson(JsonUtil.toJson(this), Alert.class);
146146
}
147147
}

common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/JsonMapAttributeConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
package org.dromara.hertzbeat.common.entity.alerter;
1919

20-
import com.google.gson.reflect.TypeToken;
21-
import org.dromara.hertzbeat.common.util.GsonUtil;
20+
import org.dromara.hertzbeat.common.util.JsonUtil;
21+
import com.fasterxml.jackson.core.type.TypeReference;
2222

2323
import javax.persistence.AttributeConverter;
24-
import java.lang.reflect.Type;
2524
import java.util.Map;
2625

2726
/**
@@ -33,12 +32,11 @@ public class JsonMapAttributeConverter implements AttributeConverter<Map<String,
3332

3433
@Override
3534
public String convertToDatabaseColumn(Map<String, String> attribute) {
36-
return GsonUtil.toJson(attribute);
35+
return JsonUtil.toJson(attribute);
3736
}
3837

3938
@Override
4039
public Map<String, String> convertToEntityAttribute(String dbData) {
41-
Type type = new TypeToken<Map<String, String>>(){}.getType();
42-
return GsonUtil.fromJson(dbData, type);
40+
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
4341
}
4442
}

common/src/main/java/org/dromara/hertzbeat/common/entity/job/Job.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.fasterxml.jackson.annotation.JsonIgnore;
2222
import org.dromara.hertzbeat.common.entity.manager.ParamDefine;
2323
import org.dromara.hertzbeat.common.entity.message.CollectRep;
24-
import org.dromara.hertzbeat.common.util.GsonUtil;
24+
import org.dromara.hertzbeat.common.util.JsonUtil;
2525
import lombok.AllArgsConstructor;
2626
import lombok.Builder;
2727
import lombok.Data;
@@ -235,6 +235,6 @@ public void addCollectMetricsData(CollectRep.MetricsData metricsData) {
235235
@Override
236236
public Job clone() {
237237
// deep clone 深度克隆
238-
return GsonUtil.fromJson(GsonUtil.toJson(this), Job.class);
238+
return JsonUtil.fromJson(JsonUtil.toJson(this), Job.class);
239239
}
240240
}

common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonByteListAttributeConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
package org.dromara.hertzbeat.common.entity.manager;
1919

20-
import com.google.gson.reflect.TypeToken;
21-
import org.dromara.hertzbeat.common.util.GsonUtil;
20+
import com.fasterxml.jackson.core.type.TypeReference;
21+
import org.dromara.hertzbeat.common.util.JsonUtil;
2222

2323
import javax.persistence.AttributeConverter;
24-
import java.lang.reflect.Type;
2524
import java.util.List;
2625

2726
/**
@@ -33,12 +32,11 @@ public class JsonByteListAttributeConverter implements AttributeConverter<List<B
3332

3433
@Override
3534
public String convertToDatabaseColumn(List<Byte> attribute) {
36-
return GsonUtil.toJson(attribute);
35+
return JsonUtil.toJson(attribute);
3736
}
3837

3938
@Override
4039
public List<Byte> convertToEntityAttribute(String dbData) {
41-
Type type = new TypeToken<List<Byte>>(){}.getType();
42-
return GsonUtil.fromJson(dbData, type);
40+
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
4341
}
4442
}

common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonOptionListAttributeConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
package org.dromara.hertzbeat.common.entity.manager;
1919

20-
import com.google.gson.reflect.TypeToken;
21-
import org.dromara.hertzbeat.common.util.GsonUtil;
20+
import com.fasterxml.jackson.core.type.TypeReference;
21+
import org.dromara.hertzbeat.common.util.JsonUtil;
2222

2323
import javax.persistence.AttributeConverter;
24-
import java.lang.reflect.Type;
2524
import java.util.List;
2625

2726
/**
@@ -33,12 +32,11 @@ public class JsonOptionListAttributeConverter implements AttributeConverter<List
3332

3433
@Override
3534
public String convertToDatabaseColumn(List<ParamDefine.Option> attribute) {
36-
return GsonUtil.toJson(attribute);
35+
return JsonUtil.toJson(attribute);
3736
}
3837

3938
@Override
4039
public List<ParamDefine.Option> convertToEntityAttribute(String dbData) {
41-
Type type = new TypeToken<List<ParamDefine.Option>>(){}.getType();
42-
return GsonUtil.fromJson(dbData, type);
40+
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
4341
}
4442
}

common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonTagListAttributeConverter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
package org.dromara.hertzbeat.common.entity.manager;
1919

20-
import com.google.gson.reflect.TypeToken;
21-
import org.dromara.hertzbeat.common.util.GsonUtil;
20+
import com.fasterxml.jackson.core.type.TypeReference;
21+
import org.dromara.hertzbeat.common.util.JsonUtil;
2222

2323
import javax.persistence.AttributeConverter;
24-
import java.lang.reflect.Type;
2524
import java.util.List;
2625
import java.util.Map;
2726
import java.util.stream.Collectors;
@@ -35,19 +34,21 @@ public class JsonTagListAttributeConverter implements AttributeConverter<List<No
3534

3635
@Override
3736
public String convertToDatabaseColumn(List<NoticeRule.TagItem> attribute) {
38-
return GsonUtil.toJson(attribute);
37+
return JsonUtil.toJson(attribute);
3938
}
4039

4140
@Override
4241
public List<NoticeRule.TagItem> convertToEntityAttribute(String dbData) {
4342
try {
44-
Type type = new TypeToken<List<NoticeRule.TagItem>>(){}.getType();
45-
return GsonUtil.fromJson(dbData, type);
43+
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
4644
} catch (Exception e) {
4745
// history data handler
48-
Type type = new TypeToken<Map<String, String>>(){}.getType();
49-
Map<String, String> map = GsonUtil.fromJson(dbData, type);
50-
return map.entrySet().stream().map(entry -> new NoticeRule.TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList());
46+
Map<String, String> map = JsonUtil.fromJson(dbData, new TypeReference<>() {});
47+
if (map != null) {
48+
return map.entrySet().stream().map(entry -> new NoticeRule.TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList());
49+
} else {
50+
return null;
51+
}
5152
}
5253
}
5354
}

0 commit comments

Comments
 (0)