io.gitee.putaoo.easypay.util.JsonUtils Maven / Gradle / Ivy
package io.gitee.putaoo.easypay.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 lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.TimeZone;
@Slf4j
@UtilityClass
public class JsonUtils {
public static ObjectMapper MAPPER = new ObjectMapper();
public static String KEY_PREFIX_REGEX = "^[0-9a-zA-Z].*";
static {
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
MAPPER.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
MAPPER.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
MAPPER.registerModule(new JavaTimeModule());
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public static String toJson(Object object) {
try {
return MAPPER.writeValueAsString(object);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static String toPrettyJson(Object object) {
try {
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static T fromJson(String jsonString, Class valueType) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return MAPPER.readValue(jsonString, valueType);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static T fromJsonNoEx(String jsonString, Class valueType) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return MAPPER.readValue(jsonString, valueType);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
public static T fromJson(InputStream is, Class valueType) {
try {
return MAPPER.readValue(is, valueType);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static , S> T fromJson(String jsonString, Class collectionType, Class elementType) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return MAPPER.readValue(jsonString, MAPPER.getTypeFactory().constructCollectionType(collectionType, elementType));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static T fromJson(String jsonString, TypeReference typeReference) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return MAPPER.readValue(jsonString, typeReference);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static boolean isJson(String jsonData){
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
JsonNode jsonNode = objectMapper.readTree(jsonData);
Iterator> fields = jsonNode.fields();
while (fields.hasNext()){
Map.Entry nodeEntry = fields.next();
String key = nodeEntry.getKey();
if (!key.matches(KEY_PREFIX_REGEX)){
return false;
}
}
}catch (IOException e){
e.printStackTrace();
return false;
}
return true;
}
}