io.github.nichetoolkit.rest.helper.JsonHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-toolkit-utils Show documentation
Show all versions of rest-toolkit-utils Show documentation
Rest toolkit utils project for Spring Boot
package io.github.nichetoolkit.rest.helper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.ArrayType;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.github.nichetoolkit.rest.DefaultResult;
import io.github.nichetoolkit.rest.RestResult;
import io.github.nichetoolkit.rest.error.ClassUnsupportedException;
import io.github.nichetoolkit.rest.error.json.JsonParseBeanException;
import io.github.nichetoolkit.rest.error.json.JsonParseListException;
import io.github.nichetoolkit.rest.error.json.JsonParseMapException;
import io.github.nichetoolkit.rest.error.json.JsonParseSetException;
import io.github.nichetoolkit.rest.error.supply.JsonParseException;
import io.github.nichetoolkit.rest.holder.ObjectMapperHolder;
import io.github.nichetoolkit.rest.util.GeneralUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* JsonHelper
* @author Cyan ([email protected])
* @version v1.0.0
*/
@Slf4j
public class JsonHelper {
/**
* 序列化为Json字符串
* @param target 目标数据
* @param 目标类型
* @return String json字符串
*/
public static String parseJson(T target) throws JsonParseException {
if (GeneralUtils.isEmpty(target)) {
return null;
}
try {
return ObjectMapperHolder.objectMapper().writeValueAsString(target);
} catch (JsonProcessingException exception) {
throw new JsonParseException("parseJson", target.getClass().getName(), exception.getMessage());
}
}
/**
* 序列化为Json字符串
* @param target 目标数据
* @param 目标类型
* @return String json字符串
*/
public static String parseJson(T target, TypeReference typeReference) throws JsonParseException {
if (GeneralUtils.isEmpty(target)) {
return null;
}
try {
return ObjectMapperHolder.objectMapper().writerFor(typeReference).writeValueAsString(target);
} catch (JsonProcessingException exception) {
throw new JsonParseException("parseJson", target.getClass().getName(), exception.getMessage());
}
}
/**
* 序列化为Json字符串
* @param target 目标数据
* @param 目标类型
* @return String json字符串
*/
public static String parseJsonIgnoreNull(T target) throws JsonParseException {
if (GeneralUtils.isEmpty(target)) {
return null;
}
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsString(target);
} catch (JsonProcessingException exception) {
throw new JsonParseException("parseJsonIgnoreNull", target.getClass().getName(), exception.getMessage());
}
}
/**
* json字符串解析为Bean
* @param json json字符串数据
* @param clazz bean类
* @param bean类型
* @return T Bean
*/
public static T parseBean(String json, Class clazz) throws JsonParseBeanException {
if (GeneralUtils.isEmpty(json)) {
return null;
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, clazz);
} catch (JsonProcessingException exception) {
throw new JsonParseBeanException("parseBean", clazz.getName(), json, exception.getMessage());
}
}
public static T parseBean(String json, TypeReference typeReference) throws JsonParseBeanException {
if (GeneralUtils.isEmpty(json)) {
return null;
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, typeReference);
} catch (JsonProcessingException exception) {
throw new JsonParseBeanException("parseBean", typeReference.getType().getTypeName(), json, exception.getMessage());
}
}
public static T parseBean(String json, JavaType javaType) throws JsonParseBeanException {
if (GeneralUtils.isEmpty(json)) {
return null;
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, javaType);
} catch (JsonProcessingException exception) {
throw new JsonParseBeanException("parseBean", javaType.getRawClass().getName(), json, exception.getMessage());
}
}
/**
* json字符串解析为Bean
* @param json json字符串数据
* @param clazz bean类
* @param innerClazz 内部类型
* @param bean类型
* @param 内部类型
* @return T cast [(T) T]
*/
public static T parseBean(String json, Class clazz, Class innerClazz) throws JsonParseBeanException {
if (GeneralUtils.isEmpty(json)) {
return null;
}
JavaType javaType = TypeFactory.defaultInstance().constructParametricType(clazz, innerClazz);
try {
return ObjectMapperHolder.objectMapper().readValue(json, javaType);
} catch (JsonProcessingException exception) {
throw new JsonParseBeanException("parseBean", javaType.getRawClass().getName(), json, exception.getMessage());
}
}
/**
* json字符串解析为BeanList
* @param json json字符串数据
* @param arrayType 序列化指定的ArrayType类型
* @param Bean类型
* @return List BeanList
*/
public static T[] parseArray(String json, ArrayType arrayType) throws JsonParseListException {
if (GeneralUtils.isEmpty(json)) {
return null;
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, arrayType);
} catch (JsonProcessingException exception) {
throw new JsonParseListException("parseArray", arrayType.getRawClass().getName(), json, exception.getMessage());
}
}
/**
* json字符串解析为BeanList
* @param json json字符串数据
* @param clazz Bean类
* @param List类型
* @param Bean类型
* @return List BeanList
*/
public static T[] parseArray(String json, Class clazz) throws JsonParseListException {
ArrayType arrayType = TypeFactory.defaultInstance().constructArrayType(clazz);
return parseArray(json, arrayType);
}
public static T[] parseArray(String json, TypeReference typeReference) throws JsonParseListException {
ArrayType arrayType = TypeFactory.defaultInstance().constructArrayType(TypeFactory.defaultInstance().constructType(typeReference));
return parseArray(json, arrayType);
}
/**
* json字符串解析为BeanList
* @param json json字符串数据
* @param listType 序列化指定的CollectionType类型
* @param Bean类型
* @return List BeanList
*/
public static List parseList(String json, CollectionType listType) throws JsonParseListException {
if (GeneralUtils.isEmpty(json)) {
return Collections.emptyList();
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, listType);
} catch (JsonProcessingException exception) {
throw new JsonParseListException("parseList", listType.getRawClass().getName(),json, exception.getMessage());
}
}
public static List parseList(String json, TypeReference> typeReference) throws JsonParseListException {
if (GeneralUtils.isEmpty(json)) {
return Collections.emptyList();
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, typeReference);
} catch (JsonProcessingException exception) {
throw new JsonParseListException("parseList", typeReference.getType().getTypeName(), json, exception.getMessage());
}
}
/**
* json字符串解析为BeanList
* @param json json字符串数据
* @param parseClazz List类
* @param clazz Bean类
* @param List类型
* @param Bean类型
* @return List BeanList
*/
public static List parseList(String json, Class parseClazz, Class clazz) throws JsonParseListException {
CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(parseClazz, clazz);
return parseList(json, listType);
}
/**
* json字符串解析为BeanList
* @param json json字符串数据
* @param clazz Bean类
* @param Bean类型
* @return List BeanList
*/
public static List parseList(String json, Class clazz) throws JsonParseListException {
return parseList(json, List.class, clazz);
}
/**
* json字符串解析为BeanSet
* @param json json字符串数据
* @param setType 序列化指定的CollectionType类型
* @param Bean类型
* @return Set BeanSet
*/
public static Set parseSet(String json, CollectionType setType) throws JsonParseSetException {
if (GeneralUtils.isEmpty(json)) {
return Collections.emptySet();
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, setType);
} catch (JsonProcessingException exception) {
throw new JsonParseSetException("parseSet",setType.getRawClass().getName(),json, exception.getMessage());
}
}
public static Set parseSet(String json, TypeReference> typeReference) throws JsonParseSetException {
if (GeneralUtils.isEmpty(json)) {
return Collections.emptySet();
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, typeReference);
} catch (JsonProcessingException exception) {
throw new JsonParseSetException("parseSet",typeReference.getType().getTypeName(),json, exception.getMessage());
}
}
/**
* json字符串解析为BeanSet
* @param json json字符串数据
* @param parseClazz Set类
* @param clazz Bean类
* @param Set类型
* @param Bean类型
* @return Set BeanSet
*/
public static Set parseSet(String json, Class parseClazz, Class clazz) throws JsonParseSetException {
CollectionType setType = TypeFactory.defaultInstance().constructCollectionType(parseClazz, clazz);
return parseSet(json, setType);
}
/**
* json字符串解析为BeanSet
* @param json json字符串数据
* @param clazz Bean类
* @param Bean类型
* @return Set BeanSet
*/
public static Set parseSet(String json, Class clazz) throws JsonParseSetException {
return parseSet(json, Set.class, clazz);
}
/**
* json字符串解析为BeanMap
* @param json json字符串数据
* @param mapType 序列化指定的MapType类型
* @param key类型
* @param value类型
* @return Map BeanMap
*/
public static Map parseMap(String json, MapType mapType) throws JsonParseMapException {
if (GeneralUtils.isEmpty(json)) {
return Collections.emptyMap();
}
try {
return ObjectMapperHolder.objectMapper().readValue(json, mapType);
} catch (JsonProcessingException exception) {
throw new JsonParseMapException("parseMap", mapType.getRawClass().getName(),json, exception.getMessage());
}
}
public static Map parseMap(String json, TypeReference