mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-08 23:04:03 +08:00
[feat] 增加json化时对LocalDateTime,LocalDate,Timestamp时间的优化;
[version] 1.5.2-jdk11(未发布)
This commit is contained in:
parent
b0b8cbc7ab
commit
77a0e29916
13
pom.xml
13
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>top.yexuejc</groupId>
|
<groupId>top.yexuejc</groupId>
|
||||||
<artifactId>yexuejc-base</artifactId>
|
<artifactId>yexuejc-base</artifactId>
|
||||||
<version>1.5.1-jdk11</version>
|
<version>1.5.2-jdk11</version>
|
||||||
|
|
||||||
<name>yexuejc-base</name>
|
<name>yexuejc-base</name>
|
||||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||||
@ -51,8 +51,8 @@
|
|||||||
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
|
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
|
||||||
<guava.version>31.1-jre</guava.version>
|
<guava.version>31.1-jre</guava.version>
|
||||||
<apache-poi.version>5.2.2</apache-poi.version>
|
<apache-poi.version>5.2.2</apache-poi.version>
|
||||||
<jackson.version>2.14.0</jackson.version>
|
<jackson.version>2.14.2</jackson.version>
|
||||||
<zip4j.version>2.11.2</zip4j.version>
|
<zip4j.version>2.11.4</zip4j.version>
|
||||||
<!-- 文件拷贝时的编码 -->
|
<!-- 文件拷贝时的编码 -->
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
@ -118,6 +118,11 @@
|
|||||||
<artifactId>jackson-dataformat-csv</artifactId>
|
<artifactId>jackson-dataformat-csv</artifactId>
|
||||||
<version>${jackson.version}</version>
|
<version>${jackson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
|
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.lingala.zip4j</groupId>
|
<groupId>net.lingala.zip4j</groupId>
|
||||||
@ -128,7 +133,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<version>5.9.0</version>
|
<version>5.9.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
21
src/main/java/com/yexuejc/base/constant/DateConsts.java
Normal file
21
src/main/java/com/yexuejc/base/constant/DateConsts.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.yexuejc.base.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yexuejc
|
||||||
|
* @class-name DateConsts
|
||||||
|
* @description
|
||||||
|
* @date 2023/05/17 13:45
|
||||||
|
*/
|
||||||
|
public class DateConsts {
|
||||||
|
public static final String BAR = "-";
|
||||||
|
public static final CharSequence DATE_KEY_AM = "AM";
|
||||||
|
public static final CharSequence DATE_KEY_PM = "PM";
|
||||||
|
public static final String DATE_TIMESTAMP_LINUX = "M/dd/yy, h:mm a";
|
||||||
|
public static final CharSequence SLASH = "/";
|
||||||
|
public static final CharSequence COLON = ":";
|
||||||
|
public static final String DATE_YYYY_MM_DD_SLASH = "yyyy/MM/dd";
|
||||||
|
public static final CharSequence DATE_KEY_T = "T";
|
||||||
|
public static final String DATE_KEY_Z = "Z";
|
||||||
|
public static final String DATE_TIMESTAMP = "yyyy/MM/dd H:m:s";
|
||||||
|
public static final String DATE_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
import com.yexuejc.base.constant.DateConsts;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json转LocalDate
|
||||||
|
*
|
||||||
|
* @author yexuejc
|
||||||
|
* @date 2022/10/08
|
||||||
|
*/
|
||||||
|
public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
|
||||||
|
throws IOException {
|
||||||
|
String timeString = jsonParser.getValueAsString();
|
||||||
|
if (timeString.contains(DateConsts.BAR)) {
|
||||||
|
return LocalDate.parse(timeString, DateTimeFormatter.ISO_DATE);
|
||||||
|
} else if (timeString.contains(DateConsts.DATE_KEY_AM)
|
||||||
|
|| timeString.contains(DateConsts.DATE_KEY_PM)) {
|
||||||
|
return LocalDate.parse(timeString,
|
||||||
|
DateTimeFormatter.ofPattern(DateConsts.DATE_TIMESTAMP_LINUX, Locale.ENGLISH));
|
||||||
|
} else if (timeString.contains(DateConsts.SLASH) && timeString.contains(DateConsts.COLON)) {
|
||||||
|
return LocalDate.parse(timeString.substring(0, 10),
|
||||||
|
DateTimeFormatter.ofPattern(DateConsts.DATE_YYYY_MM_DD_SLASH));
|
||||||
|
} else if (timeString.contains(DateConsts.SLASH)) {
|
||||||
|
return LocalDate.parse(timeString, DateTimeFormatter.ofPattern(DateConsts.DATE_YYYY_MM_DD_SLASH));
|
||||||
|
} else {
|
||||||
|
return LocalDate.parse(timeString, DateTimeFormatter.BASIC_ISO_DATE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* localDate转json
|
||||||
|
*
|
||||||
|
* @author yexuejc
|
||||||
|
* @date 2022/10/08
|
||||||
|
*/
|
||||||
|
public class LocalDateSerializer extends JsonSerializer<LocalDate> {
|
||||||
|
@Override
|
||||||
|
public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
||||||
|
throws IOException {
|
||||||
|
jsonGenerator.writeString(localDate.format(DateTimeFormatter.ISO_DATE));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
import com.yexuejc.base.constant.DateConsts;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json转LocalDateTime
|
||||||
|
*
|
||||||
|
* @author yexuejc
|
||||||
|
* @date 2022/10/08
|
||||||
|
*/
|
||||||
|
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
|
||||||
|
@Override
|
||||||
|
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
|
||||||
|
throws IOException {
|
||||||
|
String timeString = jsonParser.getValueAsString();
|
||||||
|
if (timeString.contains(DateConsts.DATE_KEY_T)) {
|
||||||
|
return LocalDateTime.parse(timeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||||
|
} else if (timeString.contains(DateConsts.DATE_KEY_AM)
|
||||||
|
|| timeString.contains(DateConsts.DATE_KEY_PM)) {
|
||||||
|
return LocalDateTime.parse(timeString,
|
||||||
|
DateTimeFormatter.ofPattern(DateConsts.DATE_TIMESTAMP_LINUX, Locale.ENGLISH));
|
||||||
|
} else if (timeString.endsWith(DateConsts.DATE_KEY_Z)) {
|
||||||
|
return LocalDateTime.parse(timeString, DateTimeFormatter.ISO_INSTANT);
|
||||||
|
} else if (timeString.contains(DateConsts.SLASH)) {
|
||||||
|
return LocalDateTime.parse(timeString, DateTimeFormatter.ofPattern(DateConsts.DATE_TIMESTAMP));
|
||||||
|
} else {
|
||||||
|
return LocalDateTime.parse(timeString,
|
||||||
|
DateTimeFormatter.ofPattern(DateConsts.DATE_YYYY_MM_DD_HH_MM_SS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LocalDateTime转json
|
||||||
|
*
|
||||||
|
* @author yexuejc
|
||||||
|
* @date 2022/10/08
|
||||||
|
*/
|
||||||
|
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
|
||||||
|
@Override
|
||||||
|
public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator,
|
||||||
|
SerializerProvider serializerProvider) throws IOException {
|
||||||
|
jsonGenerator.writeString(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
import com.yexuejc.base.constant.DateConsts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json转LocalDateTime
|
||||||
|
*
|
||||||
|
* @author yexuejc
|
||||||
|
* @date 2022/10/08
|
||||||
|
*/
|
||||||
|
public class TimestampDeserializer extends JsonDeserializer<Timestamp> {
|
||||||
|
@Override
|
||||||
|
public Timestamp deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
|
||||||
|
throws IOException {
|
||||||
|
String timeString = jsonParser.getValueAsString();
|
||||||
|
if (timeString.contains(DateConsts.DATE_KEY_T)) {
|
||||||
|
return Timestamp.valueOf(LocalDateTime.parse(timeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||||
|
} else if (timeString.contains(DateConsts.DATE_KEY_AM)
|
||||||
|
|| timeString.contains(DateConsts.DATE_KEY_PM)) {
|
||||||
|
return Timestamp.valueOf(LocalDateTime.parse(timeString,
|
||||||
|
DateTimeFormatter.ofPattern(DateConsts.DATE_TIMESTAMP_LINUX, Locale.ENGLISH)));
|
||||||
|
} else if (timeString.endsWith(DateConsts.DATE_KEY_Z)) {
|
||||||
|
return Timestamp.valueOf(LocalDateTime.parse(timeString, DateTimeFormatter.ISO_INSTANT));
|
||||||
|
} else if (timeString.contains(DateConsts.SLASH)) {
|
||||||
|
return Timestamp.valueOf(
|
||||||
|
LocalDateTime.parse(timeString, DateTimeFormatter.ofPattern(DateConsts.DATE_TIMESTAMP)));
|
||||||
|
} else {
|
||||||
|
return Timestamp.valueOf(LocalDateTime.parse(timeString,
|
||||||
|
DateTimeFormatter.ofPattern(DateConsts.DATE_YYYY_MM_DD_HH_MM_SS)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.yexuejc.base.constant.DateConsts;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timestamp转json
|
||||||
|
*
|
||||||
|
* @author yexuejc
|
||||||
|
* @date 2022/10/08
|
||||||
|
*/
|
||||||
|
public class TimestampSerializer extends JsonSerializer<Timestamp> {
|
||||||
|
@Override
|
||||||
|
public void serialize(Timestamp timestamp, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
||||||
|
throws IOException {
|
||||||
|
SimpleDateFormat df = new SimpleDateFormat(DateConsts.DATE_TIMESTAMP);
|
||||||
|
jsonGenerator.writeString(df.format(timestamp));
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,21 @@
|
|||||||
package com.yexuejc.base.util;
|
package com.yexuejc.base.util;
|
||||||
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.*;
|
import com.fasterxml.jackson.databind.*;
|
||||||
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
import com.fasterxml.jackson.databind.type.MapType;
|
import com.fasterxml.jackson.databind.type.MapType;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
import com.yexuejc.base.converter.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -32,32 +37,44 @@ public class JsonUtil {
|
|||||||
/**
|
/**
|
||||||
* 作为单例全局使用
|
* 作为单例全局使用
|
||||||
*/
|
*/
|
||||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
private static JsonMapper jsonMapper = new JsonMapper();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
JsonUtil.initDefaultObjectMapper(JsonUtil.objectMapper);
|
JsonUtil.initDefaultObjectMapper(JsonUtil.jsonMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化ObjectMapper为默认属性
|
* 初始化ObjectMapper为默认属性
|
||||||
*
|
*
|
||||||
* @param objectMapper
|
* @param jsonMapper
|
||||||
*/
|
*/
|
||||||
private static void initDefaultObjectMapper(ObjectMapper objectMapper) {
|
private static void initDefaultObjectMapper(ObjectMapper jsonMapper) {
|
||||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
//值为空时,NON_NULL:舍去字段;ALWAYS:保留字段,值为“”
|
||||||
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
jsonMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
jsonMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||||
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||||
//设置一下时区,可以和程序同步避免时区问题
|
//设置一下时区,可以和程序同步避免时区问题
|
||||||
objectMapper.setTimeZone(TimeZone.getDefault());
|
jsonMapper.setTimeZone(TimeZone.getDefault());
|
||||||
objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
|
jsonMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
|
||||||
|
|
||||||
|
JavaTimeModule javaTime = new JavaTimeModule();
|
||||||
|
//java time 特殊处理
|
||||||
|
javaTime.addSerializer(LocalDate.class, new LocalDateSerializer());
|
||||||
|
javaTime.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
|
||||||
|
javaTime.addSerializer(Timestamp.class, new TimestampSerializer());
|
||||||
|
|
||||||
|
javaTime.addDeserializer(LocalDate.class, new LocalDateDeserializer());
|
||||||
|
javaTime.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
|
||||||
|
javaTime.addDeserializer(Timestamp.class, new TimestampDeserializer());
|
||||||
|
jsonMapper.registerModule(javaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO 待优化
|
//TODO 待优化
|
||||||
public static void initSnakeCase() {
|
public static void initSnakeCase() {
|
||||||
//驼峰下划线互转
|
//驼峰下划线互转
|
||||||
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
jsonMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,8 +103,8 @@ public class JsonUtil {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static ObjectMapper getObjectMapper() {
|
public static ObjectMapper getJsonMapper() {
|
||||||
return objectMapper;
|
return jsonMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,7 +118,7 @@ public class JsonUtil {
|
|||||||
T pojo = null;
|
T pojo = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
pojo = objectMapper.readValue(json, cls);
|
pojo = jsonMapper.readValue(json, cls);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
@ -123,7 +140,7 @@ public class JsonUtil {
|
|||||||
public static <T> T json2Obj(String json, Class<T> cls) {
|
public static <T> T json2Obj(String json, Class<T> cls) {
|
||||||
T pojo = null;
|
T pojo = null;
|
||||||
try {
|
try {
|
||||||
pojo = objectMapper.readValue(json, cls);
|
pojo = jsonMapper.readValue(json, cls);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
@ -147,9 +164,9 @@ public class JsonUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> T json2Obj(String json, Class<T> parametrized, Class<?>... parameterClasses) {
|
public static <T> T json2Obj(String json, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||||
T pojo = null;
|
T pojo = null;
|
||||||
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
JavaType javaType = jsonMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
||||||
try {
|
try {
|
||||||
pojo = objectMapper.readValue(json, javaType);
|
pojo = jsonMapper.readValue(json, javaType);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
@ -172,9 +189,9 @@ public class JsonUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> T json2Obj(String json, Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) {
|
public static <T> T json2Obj(String json, Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) {
|
||||||
T pojo = null;
|
T pojo = null;
|
||||||
MapType mapType = objectMapper.getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
|
MapType mapType = jsonMapper.getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
|
||||||
try {
|
try {
|
||||||
pojo = objectMapper.readValue(json, mapType);
|
pojo = jsonMapper.readValue(json, mapType);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
@ -195,7 +212,7 @@ public class JsonUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T extends Object> T json2Obj(String json, TypeReference<T> javaType) {
|
public static <T extends Object> T json2Obj(String json, TypeReference<T> javaType) {
|
||||||
try {
|
try {
|
||||||
return objectMapper.readValue(json, javaType);
|
return jsonMapper.readValue(json, javaType);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -212,10 +229,10 @@ public class JsonUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> T json2Obj(InputStream json, Class<T> parametrized, Class<?>... parameterClasses) {
|
public static <T> T json2Obj(InputStream json, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||||
T pojo = null;
|
T pojo = null;
|
||||||
JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
JavaType javaType = jsonMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
||||||
parameterClasses);
|
parameterClasses);
|
||||||
try {
|
try {
|
||||||
pojo = objectMapper.readValue(json, javaType);
|
pojo = jsonMapper.readValue(json, javaType);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
@ -235,7 +252,7 @@ public class JsonUtil {
|
|||||||
public static String obj2Json(Object pojo) {
|
public static String obj2Json(Object pojo) {
|
||||||
String json = null;
|
String json = null;
|
||||||
try {
|
try {
|
||||||
json = objectMapper.writeValueAsString(pojo);
|
json = jsonMapper.writeValueAsString(pojo);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
|
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -251,7 +268,7 @@ public class JsonUtil {
|
|||||||
public static String formatPrinter(Object obj) {
|
public static String formatPrinter(Object obj) {
|
||||||
String json = null;
|
String json = null;
|
||||||
try {
|
try {
|
||||||
json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
json = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
|
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user