forked from opengoofy/hippo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
使用 Jackson 替换 FastJson 组件. (opengoofy#25)
- Loading branch information
1 parent
9c44417
commit a302e5a
Showing
26 changed files
with
397 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
hippo4j-common/src/main/java/cn/hippo4j/common/api/JsonFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
hippo4j-common/src/main/java/cn/hippo4j/common/api/impl/JacksonHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/JSONUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.