Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/resources/generator-mapper/generatorConfig.xml
This commit is contained in:
wangguocheng
2024-05-17 10:31:29 +08:00
22 changed files with 1281 additions and 63 deletions

View File

@@ -166,6 +166,14 @@ public class JSONUtil {
throw new RuntimeException("Invalid jsonStr,parse error:" + jsonStr, e);
}
}
public static <T> T jsonStrToObject(String jsonStr, Class<T> clazz) {
Object obj = JSONArray.parseObject(jsonStr, clazz);
return (T) obj;
}
public static <T> T jsonstrtoObject(String str,TypeReference<T> tTypeReference ){
return JSON.parseObject(str, tTypeReference);
}
public static void main(String[] args) {
String sss = "{\"bizData\":{\"amount\":1,\"currency\":\"cny\",\"ifCode\":\"sxfpay\",\"mchOrderNo\":\"CZ1715744291232\",\"mercNo\":\"B240510702030\",\"note\":\"等待用户付款\",\"payOrderId\":\"O1790587460614225921\",\"payType\":\"WECHAT\",\"settlementType\":\"D1\",\"state\":\"TRADE_AWAIT\",\"storeId\":\"S2405103298\",\"subject\":\"测试支付\",\"tradeFee\":0},\"code\":\"000000\",\"msg\":\"请求成功\",\"sign\":\"40710a3c293eeac3c7f4a1b0696a2bf6\",\"signType\":\"MD5\",\"timestamp\":\"20240515113813\"}";

View File

@@ -0,0 +1,165 @@
package com.chaozhanggui.system.cashierservice.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* The type Json operator.
*
* @author tianmaolin004
* @date 2023 /3/18
*/
@Slf4j
public class JsonUtils {
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
static {
// 1 序列化及反序列化的时间配置
JavaTimeModule timeModule = new JavaTimeModule();
timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_PATTERN)));
timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_PATTERN)));
objectMapper.registerModule(timeModule);
objectMapper.setDateFormat(new SimpleDateFormat(LOCAL_DATE_TIME_PATTERN));
//2 忽略反序列化时,对象不存在对应属性的错误,如果不存在该属性则设置值为null
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//3 忽略序列化时值为Null元素不存在该元素则字符串中无该元素而不是展示为null
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
/**
* 对象转字符串
*
* @param <T> the type parameter
* @param obj the obj
* @return the string
*/
public static <T> String obj2Str(T obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
} catch (Exception e) {
log.error("obj2Str fail");
return null;
}
}
/**
* 字符串转对象
*
* @param <T> the type parameter
* @param str the str
* @param clazz the clazz
* @return the t
*/
public static <T> T str2Obj(String str, Class<T> clazz) {
if (StringUtils.isEmpty(str) || clazz == null) {
return null;
}
try {
return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
} catch (Exception e) {
log.error("str2Obj fail");
return null;
}
}
/**
* 字符串转对象:泛型模式,一般用于集合
*
* @param <T> the type parameter
* @param str the str
* @param typeReference the type reference
* @return the t
*/
public static <T> T str2Obj(String str, TypeReference<T> typeReference) {
if (StringUtils.isEmpty(str) || typeReference == null) {
return null;
}
try {
return (T) (typeReference.getType().equals(String.class) ? str : objectMapper.readValue(str, typeReference));
} catch (Exception e) {
log.error("str2Obj fail");
return null;
}
}
/**
* 字符串转JsonNode
*
* @param str the str
* @return the json node
*/
public static JsonNode str2JsonNode(String str) {
if (StringUtils.isEmpty(str)) {
return null;
}
try {
return objectMapper.readTree(str);
} catch (Exception e) {
log.error("str2Obj fail");
return null;
}
}
/**
* 对象互转
*
* @param <T> the type parameter
* @param fromValue the from value
* @param toValueType the to value type
* @return the t
*/
public static <T> T convertValue(@NonNull Object fromValue, @NonNull Class<T> toValueType) {
try {
return objectMapper.convertValue(fromValue, toValueType);
} catch (Exception e) {
log.error("str2Obj fail");
return null;
}
}
/**
* 对象互转泛型模式
*
* @param <T> the type parameter
* @param fromValue the from value
* @param toValueTypeRef the to value type ref
* @return the t
*/
public static <T> T convertValue(@NonNull Object fromValue, @NonNull TypeReference<T> toValueTypeRef) {
try {
return objectMapper.convertValue(fromValue, toValueTypeRef);
} catch (Exception e) {
log.error("str2Obj fail");
return null;
}
}
}