Skip to content

Commit

Permalink
use jackson instead of gson (#846)
Browse files Browse the repository at this point in the history
* use jackson replace of gson

* update codeql
  • Loading branch information
tomsun28 committed Jan 16, 2024
1 parent 3b13ce9 commit 6edd9ef
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 170 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
name: "CodeQL"

on:
pull_request:
branches: [ master, dev ]
paths:
- '**/*.java'
- '**/*.ts'
schedule:
- cron: '21 13 * * 4'

jobs:
analyze:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.dromara.hertzbeat.common.entity.alerter.AlertDefineMonitorBind;
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -85,7 +85,7 @@ void addNewAlertDefine() throws Exception {
// 模拟客户端往服务端发送请求
mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define")
.contentType(MediaType.APPLICATION_JSON)
.content(GsonUtil.toJson(this.alertDefine)))
.content(JsonUtil.toJson(this.alertDefine)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -95,7 +95,7 @@ void addNewAlertDefine() throws Exception {
void modifyAlertDefine() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.put("/api/alert/define")
.contentType(MediaType.APPLICATION_JSON)
.content(GsonUtil.toJson(this.alertDefine)))
.content(JsonUtil.toJson(this.alertDefine)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand Down Expand Up @@ -126,7 +126,7 @@ void getAlertDefine() throws Exception {
void deleteAlertDefine() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/define/" + this.alertDefine.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(GsonUtil.toJson(this.alertDefine)))
.content(JsonUtil.toJson(this.alertDefine)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -136,7 +136,7 @@ void deleteAlertDefine() throws Exception {
void applyAlertDefineMonitorsBind() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define/" + this.alertDefine.getId() + "/monitors")
.contentType(MediaType.APPLICATION_JSON)
.content(GsonUtil.toJson(this.alertDefineMonitorBinds)))
.content(JsonUtil.toJson(this.alertDefineMonitorBinds)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -155,4 +155,4 @@ void getAlertDefineMonitorsBind() throws Exception {
.andExpect(jsonPath("$.data[0].monitor.id").value(alertDefineMonitorBinds.get(0).getMonitor().getId()))
.andReturn();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.dromara.hertzbeat.alert.service.AlertDefineService;
import org.dromara.hertzbeat.common.entity.alerter.AlertDefine;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -126,9 +126,9 @@ public boolean matches(PageRequest pageRequestMidden) {
void deleteAlertDefines() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/defines")
.contentType(MediaType.APPLICATION_JSON)
.content(GsonUtil.toJson(ids)))
.content(JsonUtil.toJson(ids)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -158,11 +158,11 @@ void addNewAlertReport() throws Exception {
MockMvcRequestBuilders
.post("/api/alerts/report")
.contentType(MediaType.APPLICATION_JSON)
.content(GsonUtil.toJson(AlertReport.builder().build()))
.content(JsonUtil.toJson(AlertReport.builder().build()))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}"))
.andReturn();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.dromara.hertzbeat.common.entity.job.protocol.HttpProtocol;
import org.dromara.hertzbeat.common.entity.message.CollectRep;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
Expand All @@ -23,7 +23,7 @@ public class PrometheusMatrixParser extends AbstractPrometheusParse {
@Override
public Boolean checkType(String responseStr) {
try {
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
if (DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType())) {
return true;
}
Expand All @@ -35,7 +35,7 @@ public Boolean checkType(String responseStr) {

@Override
public void parse(String resp, List<String> aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(resp, PromVectorOrMatrix.class);
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
for (PromVectorOrMatrix.Result r : result) {
for (List<Object> value : r.getValues()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.dromara.hertzbeat.common.entity.job.protocol.HttpProtocol;
import org.dromara.hertzbeat.common.entity.message.CollectRep;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
Expand All @@ -23,7 +23,7 @@ public class PrometheusVectorParser extends AbstractPrometheusParse {
@Override
public Boolean checkType(String responseStr) {
try {
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
if (DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType())) {
return true;
}
Expand All @@ -37,7 +37,7 @@ public Boolean checkType(String responseStr) {
public void parse(String resp, List<String> aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
boolean setTimeFlag = false;
boolean setValueFlag = false;
PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(resp, PromVectorOrMatrix.class);
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
for (PromVectorOrMatrix.Result r : result) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

package org.dromara.hertzbeat.collector.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.dromara.hertzbeat.common.entity.job.Configmap;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -291,8 +290,7 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map
Configmap param = configmap.get(key);
if (param != null && param.getType() == (byte) 3) {
String jsonValue = (String) param.getValue();
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map = GsonUtil.fromJson(jsonValue, type);
Map<String, String> map = JsonUtil.fromJson(jsonValue, new TypeReference<>() {});
if (map != null) {
map.forEach((name, value) -> {
if (name != null && !"".equals(name.trim())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -142,6 +142,6 @@ public class Alert {
@Override
public Alert clone() {
// deep clone
return GsonUtil.fromJson(GsonUtil.toJson(this), Alert.class);
return JsonUtil.fromJson(JsonUtil.toJson(this), Alert.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

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

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

import javax.persistence.AttributeConverter;
import java.lang.reflect.Type;
import java.util.Map;

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

@Override
public String convertToDatabaseColumn(Map<String, String> attribute) {
return GsonUtil.toJson(attribute);
return JsonUtil.toJson(attribute);
}

@Override
public Map<String, String> convertToEntityAttribute(String dbData) {
Type type = new TypeToken<Map<String, String>>(){}.getType();
return GsonUtil.fromJson(dbData, type);
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.dromara.hertzbeat.common.entity.manager.ParamDefine;
import org.dromara.hertzbeat.common.entity.message.CollectRep;
import org.dromara.hertzbeat.common.util.GsonUtil;
import org.dromara.hertzbeat.common.util.JsonUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -235,6 +235,6 @@ public void addCollectMetricsData(CollectRep.MetricsData metricsData) {
@Override
public Job clone() {
// deep clone 深度克隆
return GsonUtil.fromJson(GsonUtil.toJson(this), Job.class);
return JsonUtil.fromJson(JsonUtil.toJson(this), Job.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

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

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

import javax.persistence.AttributeConverter;
import java.lang.reflect.Type;
import java.util.List;

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

@Override
public String convertToDatabaseColumn(List<Byte> attribute) {
return GsonUtil.toJson(attribute);
return JsonUtil.toJson(attribute);
}

@Override
public List<Byte> convertToEntityAttribute(String dbData) {
Type type = new TypeToken<List<Byte>>(){}.getType();
return GsonUtil.fromJson(dbData, type);
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

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

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

import javax.persistence.AttributeConverter;
import java.lang.reflect.Type;
import java.util.List;

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

@Override
public String convertToDatabaseColumn(List<ParamDefine.Option> attribute) {
return GsonUtil.toJson(attribute);
return JsonUtil.toJson(attribute);
}

@Override
public List<ParamDefine.Option> convertToEntityAttribute(String dbData) {
Type type = new TypeToken<List<ParamDefine.Option>>(){}.getType();
return GsonUtil.fromJson(dbData, type);
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

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

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

import javax.persistence.AttributeConverter;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -35,19 +34,21 @@ public class JsonTagListAttributeConverter implements AttributeConverter<List<No

@Override
public String convertToDatabaseColumn(List<NoticeRule.TagItem> attribute) {
return GsonUtil.toJson(attribute);
return JsonUtil.toJson(attribute);
}

@Override
public List<NoticeRule.TagItem> convertToEntityAttribute(String dbData) {
try {
Type type = new TypeToken<List<NoticeRule.TagItem>>(){}.getType();
return GsonUtil.fromJson(dbData, type);
return JsonUtil.fromJson(dbData, new TypeReference<>() {});
} catch (Exception e) {
// history data handler
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map = GsonUtil.fromJson(dbData, type);
return map.entrySet().stream().map(entry -> new NoticeRule.TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList());
Map<String, String> map = JsonUtil.fromJson(dbData, new TypeReference<>() {});
if (map != null) {
return map.entrySet().stream().map(entry -> new NoticeRule.TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList());
} else {
return null;
}
}
}
}
Loading

0 comments on commit 6edd9ef

Please sign in to comment.