添加新版收银系统支付

This commit is contained in:
韩鹏辉
2024-05-16 14:21:55 +08:00
parent 7b259ba55f
commit bdce57ca44
22 changed files with 1280 additions and 58 deletions

View File

@@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
@@ -117,7 +118,7 @@ public class JSONUtil {
}
/**
* 字符串转为JSON对象,注意数组类型会抛异常[{name:\"Stone\"}]
* 字符串转为JSON对象,注意数组类型会抛异常[{name:"Stone"}]
* 假设json字符串多了某个字段可能是新加上去的显然转换成JSONObject时会有这个字段因为JSONObject就相当于map
*
* @param jsonStr 传入的JSON字串
@@ -133,7 +134,7 @@ public class JSONUtil {
}
/**
* 字符串转为JSON数组,注意对象类型,非数组的会抛异常{name:\"Stone\"}
* 字符串转为JSON数组,注意对象类型,非数组的会抛异常{name:"Stone"}
* 假设json字符串多了某个字段可能是新加上去的显然转换成JSONArray时其元素会有这个字段因为JSONArray的元素JSONObject就相当于map
*
* @param jsonStr 传入的JSON字串
@@ -165,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);
}
/**
* 字符串转为某个类的列表

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;
}
}
}