io.github.nichetoolkit.rest.util.JsonUtils 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.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
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.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.helper.JsonHelper;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* JsonUtils
* @author Cyan ([email protected])
* @version v1.0.0
*/
@Slf4j
@SuppressWarnings({"TypeParameterUnusedInFormals","SameNameButDifferent"})
public class JsonUtils {
/**
* 序列化为Json字符串
* @param target 目标数据
* @param 目标类型
* @return String json字符串
*/
public static String parseJson(T target) {
try {
return JsonHelper.parseJson(target);
} catch (JsonParseException exception) {
log.error("It is failed during bean to parse as json! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
/**
* 序列化为Json字符串
* @param target 目标数据
* @param 目标类型
* @return String json字符串
*/
public static String parseJson(T target, TypeReference typeReference) {
try {
return JsonHelper.parseJson(target,typeReference);
} catch (JsonParseException exception) {
log.error("It is failed during bean to parse as json! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
public static String parseJsonIgnoreNull(T target) {
try {
return JsonHelper.parseJsonIgnoreNull(target);
} catch (JsonParseException exception) {
log.error("It is failed during bean to parse as json with ignoring null! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
/**
* json字符串解析为Bean
* @param json json字符串数据
* @param clazz bean类
* @param bean类型
* @return T Bean
*/
public static T parseBean(String json, Class clazz) {
try {
return JsonHelper.parseBean(json, clazz);
} catch (JsonParseBeanException exception) {
log.error("It is failed during json to parse as bean! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
public static T parseBean(String json, TypeReference typeReference) {
try {
return JsonHelper.parseBean(json, typeReference);
} catch (JsonParseBeanException exception) {
log.error("It is failed during json to parse as bean! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
public static T parseBean(String json, JavaType javaType) {
try {
return JsonHelper.parseBean(json, javaType);
} catch (JsonParseBeanException exception) {
log.error("It is failed during json to parse as bean! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
public static T parseBean(String json, Class clazz, Class innerClazz) {
try {
return JsonHelper.parseBean(json, clazz, innerClazz);
} catch (JsonParseBeanException exception) {
log.error("It is failed during json to parse as bean! {}", exception.getMessage());
exception.printStackTrace();
return null;
}
}
/**
* json字符串解析为BeanList
* @param json json字符串数据
* @param listType 序列化指定的CollectionType类型
* @param Bean类型
* @return List BeanList
*/
public static List parseList(String json, CollectionType listType) {
try {
return JsonHelper.parseList(json, listType);
} catch (JsonParseListException exception) {
log.error("It is failed during json to parse as list of collection! {}", exception.getMessage());
exception.printStackTrace();
return Collections.emptyList();
}
}
public static List parseList(String json, TypeReference> typeReference) {
try {
return JsonHelper.parseList(json, typeReference);
} catch (JsonParseListException exception) {
log.error("It is failed during json to parse as list of collection! {}", exception.getMessage());
exception.printStackTrace();
return Collections.emptyList();
}
}
/**
* 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) {
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) {
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) {
try {
return JsonHelper.parseSet(json, setType);
} catch (JsonParseSetException exception) {
log.error("It is failed during json to parse as set of collection! {}", exception.getMessage());
exception.printStackTrace();
return Collections.emptySet();
}
}
public static Set parseSet(String json, TypeReference> typeReference) {
try {
return JsonHelper.parseSet(json, typeReference);
} catch (JsonParseSetException exception) {
log.error("It is failed during json to parse as set of collection! {}", exception.getMessage());
exception.printStackTrace();
return Collections.emptySet();
}
}
/**
* 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) {
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) {
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) {
try {
return JsonHelper.parseMap(json, mapType);
} catch (JsonParseMapException exception) {
log.error("It is failed during json to parse as map of bean! {}", exception.getMessage());
exception.printStackTrace();
return Collections.emptyMap();
}
}
public static Map parseMap(String json, TypeReference