Skip to content

Commit

Permalink
使用 Jackson 替换 FastJson 组件. (opengoofy#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Dec 14, 2021
1 parent 9c44417 commit a302e5a
Show file tree
Hide file tree
Showing 26 changed files with 397 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cn.hippo4j.auth.filter;

import cn.hippo4j.auth.toolkit.JwtTokenUtil;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.toolkit.UserContext;
import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.common.web.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand Down Expand Up @@ -50,7 +50,7 @@ protected void doFilterInternal(HttpServletRequest request,
// 返回 Json 形式的错误信息
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
response.getWriter().write(JSON.toJSONString(Results.failure("-1", ex.getMessage())));
response.getWriter().write(JSONUtil.toJSONString(Results.failure("-1", ex.getMessage())));
response.getWriter().flush();
return;
}
Expand Down
10 changes: 5 additions & 5 deletions hippo4j-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -35,6 +30,11 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
41 changes: 41 additions & 0 deletions hippo4j-common/src/main/java/cn/hippo4j/common/api/JsonFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cn.hippo4j.common.api;

import java.util.List;

/**
* Json facade.
*
* @author chen.ma
* @date 2021/12/13 20:01
*/
public interface JsonFacade {

/**
* To JSON string.
*
* @param object
* @return
*/
String toJSONString(Object object);

/**
* Parse object.
*
* @param text
* @param clazz
* @param <T>
* @return
*/
<T> T parseObject(String text, Class<T> clazz);

/**
* Parse array.
*
* @param text
* @param clazz
* @param <T>
* @return
*/
<T> List<T> parseArray(String text, Class<T> clazz);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cn.hippo4j.common.api.impl;

import cn.hippo4j.common.api.JsonFacade;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import lombok.SneakyThrows;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
* Jackson util.
*
* @author chen.ma
* @date 2021/12/13 20:02
*/
public class JacksonHandler implements JsonFacade {

private static ObjectMapper MAPPER = new ObjectMapper();

static {
MAPPER.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
MAPPER.setDateFormat(new SimpleDateFormat(dateTimeFormat));
MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}

@Override
@SneakyThrows
public String toJSONString(Object object) {
return MAPPER.writeValueAsString(object);
}

@Override
@SneakyThrows
public <T> T parseObject(String text, Class<T> clazz) {
JavaType javaType = MAPPER.getTypeFactory().constructType(clazz);
return MAPPER.readValue(text, javaType);
}

@Override
@SneakyThrows
public <T> List<T> parseArray(String text, Class<T> clazz) {
CollectionType collectionType = MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
return MAPPER.readValue(text, collectionType);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.hippo4j.common.toolkit;

import cn.hippo4j.common.constant.Constants;
import com.alibaba.fastjson.JSON;
import cn.hippo4j.common.model.PoolParameter;
import cn.hippo4j.common.model.PoolParameterInfo;

Expand All @@ -27,7 +26,7 @@ public static String getPoolContent(PoolParameter parameter) {
.setCapacityAlarm(parameter.getCapacityAlarm())
.setLivenessAlarm(parameter.getLivenessAlarm())
.setRejectedType(parameter.getRejectedType());
return JSON.toJSONString(poolInfo);
return JSONUtil.toJSONString(poolInfo);
}

public static String getGroupKey(PoolParameter parameter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.hippo4j.common.toolkit;

import cn.hippo4j.common.api.JsonFacade;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
* JSON util.
*
* @author chen.ma
* @date 2021/12/13 20:27
*/
public class JSONUtil {

private static JsonFacade jsonFacade;

static {
JSONUtil.jsonFacade = ApplicationContextHolder.getBean(JsonFacade.class);
}

public static String toJSONString(Object object) {
if (object == null) {
return null;
}

return jsonFacade.toJSONString(object);
}

public static <T> T parseObject(String text, Class<T> clazz) {
if (StrUtil.isBlank(text)) {
return null;
}

return jsonFacade.parseObject(text, clazz);
}

public static <T> List<T> parseArray(String text, Class<T> clazz) {
if (StrUtil.isBlank(text)) {
return null;
}

return jsonFacade.parseArray(text, clazz);
}

}
5 changes: 0 additions & 5 deletions hippo4j-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@
<artifactId>hippo4j-common</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>

<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.hippo4j.config.config;

import cn.hippo4j.common.api.JsonFacade;
import cn.hippo4j.common.api.impl.JacksonHandler;
import cn.hippo4j.common.config.ApplicationContextHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -13,6 +15,11 @@
@Configuration
public class CommonConfig {

@Bean
public JsonFacade jacksonHandler() {
return new JacksonHandler();
}

@Bean
public ApplicationContextHolder simpleApplicationContextHolder() {
return new ApplicationContextHolder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cn.hippo4j.config.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import cn.hippo4j.common.model.PoolParameter;
import cn.hippo4j.common.toolkit.JSONUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.hippo4j.common.model.PoolParameter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.Date;
Expand All @@ -26,35 +26,35 @@ public class ConfigAllInfo extends ConfigInfo implements PoolParameter {
/**
* desc
*/
@JSONField(serialize = false)
@JsonIgnore
@TableField(exist = false, fill = FieldFill.UPDATE)
private String desc;

/**
* gmtCreate
*/
@JSONField(serialize = false)
@JsonIgnore
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;

/**
* gmtModified
*/
@JSONField(serialize = false)
@JsonIgnore
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;

/**
* delFlag
*/
@TableLogic
@JSONField(serialize = false)
@JsonIgnore
@TableField(fill = FieldFill.INSERT)
private Integer delFlag;

@Override
public String toString() {
return JSON.toJSONString(this);
return JSONUtil.toJSONString(this);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.hippo4j.config.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;
Expand Down Expand Up @@ -87,13 +87,13 @@ public class ConfigInfoBase implements Serializable {
/**
* MD5
*/
@JSONField(serialize = false)
@JsonIgnore
private String md5;

/**
* content
*/
@JSONField(serialize = false)
@JsonIgnore
private String content;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.hippo4j.config.model.biz.tenant;

import com.alibaba.fastjson.JSON;
import cn.hippo4j.common.toolkit.JSONUtil;
import lombok.Data;

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ public class TenantUpdateReqDTO {

@Override
public String toString() {
return JSON.toJSONString(this);
return JSONUtil.toJSONString(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import cn.hippo4j.common.monitor.MessageTypeEnum;
import cn.hippo4j.common.monitor.RuntimeMessage;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.config.service.biz.HisRunDataService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
Expand All @@ -29,7 +28,7 @@ public String mark() {

@Override
public void execute(RuntimeMessage message) {
log.info("[{}] Perform monitoring data persistence. content :: {}", this.getClass().getName(), JSON.toJSONString(message, SerializerFeature.PrettyFormat));
log.info("[{}] Perform monitoring data persistence. content :: {}", this.getClass().getName(), JSONUtil.toJSONString(message));

hisRunDataService.save(message);
}
Expand Down
Loading

0 comments on commit a302e5a

Please sign in to comment.