io.github.nichetoolkit.rest.util.JsonUtils Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
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
* The json utils class.
* @author Cyan ([email protected])
* @see lombok.extern.slf4j.Slf4j
* @see java.lang.SuppressWarnings
* @since Jdk1.8
*/
@Slf4j
@SuppressWarnings({"TypeParameterUnusedInFormals","SameNameButDifferent"})
public class JsonUtils {
/**
* parseJson
* The parse json method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param target T The target parameter is T
type.
* @return {@link java.lang.String} The parse json return object is String
type.
* @see java.lang.String
*/
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());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseJson
* The parse json method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param target T The target parameter is T
type.
* @param typeReference {@link com.fasterxml.jackson.core.type.TypeReference} The type reference parameter is TypeReference
type.
* @return {@link java.lang.String} The parse json return object is String
type.
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.lang.String
*/
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 with type reference! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseJsonIgnoreNull
* The parse json ignore null method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param target T The target parameter is T
type.
* @return {@link java.lang.String} The parse json ignore null return object is String
type.
* @see java.lang.String
*/
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());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseBean
* The parse bean method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return T The parse bean return object is T
type.
* @see java.lang.String
* @see java.lang.Class
*/
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 with class type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseBean
* The parse bean method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param typeReference {@link com.fasterxml.jackson.core.type.TypeReference} The type reference parameter is TypeReference
type.
* @return T The parse bean return object is T
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
*/
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 with type reference! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseBean
* The parse bean method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param javaType {@link com.fasterxml.jackson.databind.JavaType} The java type parameter is JavaType
type.
* @return T The parse bean return object is T
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.JavaType
*/
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());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseBean
* The parse bean method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @param innerClazz {@link java.lang.Class} The inner clazz parameter is Class
type.
* @return T The parse bean return object is T
type.
* @see java.lang.String
* @see java.lang.Class
*/
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());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* parseList
* The parse list method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param listType {@link com.fasterxml.jackson.databind.type.CollectionType} The list type parameter is CollectionType
type.
* @return {@link java.util.List} The parse list return object is List
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.CollectionType
* @see java.util.List
*/
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 with collection type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return Collections.emptyList();
}
}
/**
* parseList
* The parse list method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param typeReference {@link com.fasterxml.jackson.core.type.TypeReference} The type reference parameter is TypeReference
type.
* @return {@link java.util.List} The parse list return object is List
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.util.List
*/
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());
GeneralUtils.printStackTrace(exception);
return Collections.emptyList();
}
}
/**
* parseList
* The parse list method.
* @param {@link java.util.List} The generic parameter is List
type.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param parseClazz {@link java.lang.Class} The parse clazz parameter is Class
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return {@link java.util.List} The parse list return object is List
type.
* @see java.util.List
* @see java.lang.String
* @see java.lang.Class
*/
public static , T> List parseList(String json, Class parseClazz, Class clazz) {
CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(parseClazz, clazz);
return parseList(json, listType);
}
/**
* parseList
* The parse list method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return {@link java.util.List} The parse list return object is List
type.
* @see java.lang.String
* @see java.lang.Class
* @see java.util.List
*/
public static List parseList(String json, Class clazz) {
return parseList(json, List.class, clazz);
}
/**
* parseSet
* The parse set method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param setType {@link com.fasterxml.jackson.databind.type.CollectionType} The set type parameter is CollectionType
type.
* @return {@link java.util.Set} The parse set return object is Set
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.CollectionType
* @see java.util.Set
*/
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 with collection type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return Collections.emptySet();
}
}
/**
* parseSet
* The parse set method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param typeReference {@link com.fasterxml.jackson.core.type.TypeReference} The type reference parameter is TypeReference
type.
* @return {@link java.util.Set} The parse set return object is Set
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.util.Set
*/
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());
GeneralUtils.printStackTrace(exception);
return Collections.emptySet();
}
}
/**
* parseSet
* The parse set method.
* @param {@link java.util.Set} The generic parameter is Set
type.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param parseClazz {@link java.lang.Class} The parse clazz parameter is Class
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return {@link java.util.Set} The parse set return object is Set
type.
* @see java.util.Set
* @see java.lang.String
* @see java.lang.Class
*/
public static , T> Set parseSet(String json, Class parseClazz, Class clazz) {
CollectionType setType = TypeFactory.defaultInstance().constructCollectionType(parseClazz, clazz);
return parseSet(json, setType);
}
/**
* parseSet
* The parse set method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return {@link java.util.Set} The parse set return object is Set
type.
* @see java.lang.String
* @see java.lang.Class
* @see java.util.Set
*/
public static Set parseSet(String json, Class clazz) {
return parseSet(json, Set.class, clazz);
}
/**
* parseMap
* The parse map method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param mapType {@link com.fasterxml.jackson.databind.type.MapType} The map type parameter is MapType
type.
* @return {@link java.util.Map} The parse map return object is Map
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.MapType
* @see java.util.Map
*/
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);
GeneralUtils.printStackTrace(exception);
return Collections.emptyMap();
}
}
/**
* parseMap
* The parse map method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @param typeReference {@link com.fasterxml.jackson.core.type.TypeReference} The type reference parameter is TypeReference
type.
* @return {@link java.util.Map} The parse map return object is Map
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.util.Map
*/
public static Map parseMap(String json, TypeReference