package org.air.app.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
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.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.air.app.bsw.controller.AppBswController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class FastJsonUtil {
private static final Logger log = LoggerFactory.getLogger(AppBswController.class);
private static ObjectMapper objectMapper = new ObjectMapper();
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
static {
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static <T> T parseObject(String jsonString, Class<T> object) {
T t = null;
try {
t = objectMapper.readValue(jsonString, object);
} catch (JsonProcessingException e) {
log.error("JsonString转为自定义对象失败:{}", e.getMessage());
}
return t;
}
public static <T> T parseObject(File file, Class<T> object) {
T t = null;
try {
t = objectMapper.readValue(file, object);
} catch (IOException e) {
log.error("从文件中读取json字符串转为自定义对象失败:{}", e.getMessage());
}
return t;
}
public static <T> T parseJSONArray(String jsonArray, TypeReference<T> reference) {
T t = null;
try {
t = objectMapper.readValue(jsonArray, reference);
} catch (JsonProcessingException e) {
log.error("JSONArray转为List列表或者Map集合失败:{}", e.getMessage());
}
return t;
}
public static String toJSONString(Object object) {
String jsonString = null;
try {
jsonString = objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("Object转JSONString失败:{}", e.getMessage());
}
return jsonString;
}
public static byte[] toByteArray(Object object) {
byte[] bytes = null;
try {
bytes = objectMapper.writeValueAsBytes(object);
} catch (JsonProcessingException e) {
log.error("Object转ByteArray失败:{}", e.getMessage());
}
return bytes;
}
public static void objectToFile(File file, Object object) {
try {
objectMapper.writeValue(file, object);
} catch (JsonProcessingException e) {
log.error("Object写入文件失败:{}", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
public static JsonNode parseJSONObject(String jsonString) {
JsonNode jsonNode = null;
try {
jsonNode = objectMapper.readTree(jsonString);
} catch (JsonProcessingException e) {
log.error("JSONString转为JsonNode失败:{}", e.getMessage());
}
return jsonNode;
}
public static JsonNode parseJSONObject(Object object) {
JsonNode jsonNode = objectMapper.valueToTree(object);
return jsonNode;
}
public static String toJSONString(JsonNode jsonNode) {
String jsonString = null;
try {
jsonString = objectMapper.writeValueAsString(jsonNode);
} catch (JsonProcessingException e) {
log.error("JsonNode转JSONString失败:{}", e.getMessage());
}
return jsonString;
}
public static ObjectNode newJSONObject() {
return objectMapper.createObjectNode();
}
public static ArrayNode newJSONArray() {
return objectMapper.createArrayNode();
}
public static String getString(JsonNode jsonObject, String key) {
String s = jsonObject.get(key).asText();
return s;
}
public static Integer getInteger(JsonNode jsonObject, String key) {
Integer i = jsonObject.get(key).asInt();
return i;
}
public static Boolean getBoolean(JsonNode jsonObject, String key) {
Boolean bool = jsonObject.get(key).asBoolean();
return bool;
}
public static JsonNode getJSONObject(JsonNode jsonObject, String key) {
JsonNode json = jsonObject.get(key);
return json;
}
}
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
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.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
@Slf4j
public class JsonUtils {
private JsonUtils() {
throw new UnsupportedOperationException("Utility classes cannot be instantiated.");
}
private final static ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.registerModule(new Jdk8Module());
MAPPER.registerModule(new JavaTimeModule());
}
public static <T> T jsonCovertToObject(String json, Class<T> clazz) {
if (json == null || clazz == null) {
return null;
}
try {
return MAPPER.readValue(json, clazz);
} catch (IOException e) {
log.error("json转换失败,原因:", e);
}
return null;
}
public static <T> T jsonCovertToObject(String json, TypeReference<T> type) {
if (json == null || type == null) {
return null;
}
try {
return MAPPER.readValue(json, type);
} catch (IOException e) {
log.error("json转换失败,原因:", e);
}
return null;
}
public static <T> T covertStreamToObject(InputStream inputStream, Class<T> clazz) {
if (inputStream == null || clazz == null) {
return null;
}
try {
return MAPPER.readValue(inputStream, clazz);
} catch (IOException e) {
log.error("json转换失败,原因:", e);
}
return null;
}
public static <T> T jsonCovertToObject(String json, Class<?> collectionClazz, Class<?>... elementsClazz) {
if (json == null || collectionClazz == null || elementsClazz == null || elementsClazz.length == 0) {
return null;
}
try {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(collectionClazz, elementsClazz);
return MAPPER.readValue(json, javaType);
} catch (IOException e) {
log.error("json转换失败,原因:", e);
}
return null;
}
public static String objectCovertToJson(Object o) {
if (o == null) {
return null;
}
try {
return o instanceof String ? (String) o : MAPPER.writeValueAsString(o);
} catch (IOException e) {
log.error("json转换失败,原因:", e);
}
return null;
}
public static <T> T objectCovertToObject(Object o, Class<?> collectionClazz, Class<?>... elementsClazz) {
String json = objectCovertToJson(o);
return jsonCovertToObject(json, collectionClazz, elementsClazz);
}
}
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
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.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.registerModules(new JavaTimeModule());
}
public static void init(ObjectMapper objectMapper) {
JsonUtils.objectMapper = objectMapper;
}
@SneakyThrows
public static String toJsonString(Object object) {
return objectMapper.writeValueAsString(object);
}
@SneakyThrows
public static byte[] toJsonByte(Object object) {
return objectMapper.writeValueAsBytes(object);
}
@SneakyThrows
public static String toJsonPrettyString(Object object) {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
}
public static <T> T parseObject(String text, Class<T> clazz) {
if (StrUtil.isEmpty(text)) {
return null;
}
try {
return objectMapper.readValue(text, clazz);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static <T> T parseObject(String text, String path, Class<T> clazz) {
if (StrUtil.isEmpty(text)) {
return null;
}
try {
JsonNode treeNode = objectMapper.readTree(text);
JsonNode pathNode = treeNode.path(path);
return objectMapper.readValue(pathNode.toString(), clazz);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static <T> T parseObject(String text, Type type) {
if (StrUtil.isEmpty(text)) {
return null;
}
try {
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructType(type));
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static <T> T parseObject2(String text, Class<T> clazz) {
if (StrUtil.isEmpty(text)) {
return null;
}
return JSONUtil.toBean(text, clazz);
}
public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
if (ArrayUtil.isEmpty(bytes)) {
return null;
}
try {
return objectMapper.readValue(bytes, clazz);
} catch (IOException e) {
log.error("json parse err,json:{}", bytes, e);
throw new RuntimeException(e);
}
}
public static <T> T parseObject(String text, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(text, typeReference);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static <T> T parseObjectQuietly(String text, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(text, typeReference);
} catch (IOException e) {
return null;
}
}
public static <T> List<T> parseArray(String text, Class<T> clazz) {
if (StrUtil.isEmpty(text)) {
return new ArrayList<>();
}
try {
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static <T> List<T> parseArray(String text, String path, Class<T> clazz) {
if (StrUtil.isEmpty(text)) {
return null;
}
try {
JsonNode treeNode = objectMapper.readTree(text);
JsonNode pathNode = treeNode.path(path);
return objectMapper.readValue(pathNode.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static JsonNode parseTree(String text) {
try {
return objectMapper.readTree(text);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static JsonNode parseTree(byte[] text) {
try {
return objectMapper.readTree(text);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
}
public static boolean isJson(String text) {
return JSONUtil.isTypeJSON(text);
}
}