io.github.nichetoolkit.rest.helper.JsonHelper Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
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.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
* The json helper class.
* @author Cyan ([email protected])
* @see lombok.extern.slf4j.Slf4j
* @since Jdk1.8
*/
@Slf4j
public class JsonHelper {
/**
* 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.
* @throws JsonParseException {@link io.github.nichetoolkit.rest.error.supply.JsonParseException} The json parse exception is JsonParseException
type.
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.JsonParseException
*/
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());
}
}
/**
* 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.
* @throws JsonParseException {@link io.github.nichetoolkit.rest.error.supply.JsonParseException} The json parse exception is JsonParseException
type.
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.JsonParseException
*/
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());
}
}
/**
* 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.
* @throws JsonParseException {@link io.github.nichetoolkit.rest.error.supply.JsonParseException} The json parse exception is JsonParseException
type.
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.JsonParseException
*/
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());
}
}
/**
* 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.
* @throws JsonParseBeanException {@link io.github.nichetoolkit.rest.error.json.JsonParseBeanException} The json parse bean exception is JsonParseBeanException
type.
* @see java.lang.String
* @see java.lang.Class
* @see io.github.nichetoolkit.rest.error.json.JsonParseBeanException
*/
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());
}
}
/**
* 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.
* @throws JsonParseBeanException {@link io.github.nichetoolkit.rest.error.json.JsonParseBeanException} The json parse bean exception is JsonParseBeanException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see io.github.nichetoolkit.rest.error.json.JsonParseBeanException
*/
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());
}
}
/**
* 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.
* @throws JsonParseBeanException {@link io.github.nichetoolkit.rest.error.json.JsonParseBeanException} The json parse bean exception is JsonParseBeanException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.JavaType
* @see io.github.nichetoolkit.rest.error.json.JsonParseBeanException
*/
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());
}
}
/**
* 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.
* @throws JsonParseBeanException {@link io.github.nichetoolkit.rest.error.json.JsonParseBeanException} The json parse bean exception is JsonParseBeanException
type.
* @see java.lang.String
* @see java.lang.Class
* @see io.github.nichetoolkit.rest.error.json.JsonParseBeanException
*/
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());
}
}
/**
* parseArray
* The parse array 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 arrayType {@link com.fasterxml.jackson.databind.type.ArrayType} The array type parameter is ArrayType
type.
* @return T The parse array return object is T
type.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.ArrayType
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
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());
}
}
/**
* parseArray
* The parse array 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 clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return T The parse array return object is T
type.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.util.List
* @see java.lang.String
* @see java.lang.Class
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
public static , T> T[] parseArray(String json, Class clazz) throws JsonParseListException {
ArrayType arrayType = TypeFactory.defaultInstance().constructArrayType(clazz);
return parseArray(json, arrayType);
}
/**
* parseArray
* The parse array 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 array return object is T
type.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
public static T[] parseArray(String json, TypeReference typeReference) throws JsonParseListException {
ArrayType arrayType = TypeFactory.defaultInstance().constructArrayType(TypeFactory.defaultInstance().constructType(typeReference));
return parseArray(json, arrayType);
}
/**
* 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.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.CollectionType
* @see java.util.List
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
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());
}
}
/**
* 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.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.util.List
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
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());
}
}
/**
* 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.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.util.List
* @see java.lang.String
* @see java.lang.Class
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
public static , T> List parseList(String json, Class parseClazz, Class clazz) throws JsonParseListException {
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.
* @throws JsonParseListException {@link io.github.nichetoolkit.rest.error.json.JsonParseListException} The json parse list exception is JsonParseListException
type.
* @see java.lang.String
* @see java.lang.Class
* @see java.util.List
* @see io.github.nichetoolkit.rest.error.json.JsonParseListException
*/
public static List parseList(String json, Class clazz) throws JsonParseListException {
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.
* @throws JsonParseSetException {@link io.github.nichetoolkit.rest.error.json.JsonParseSetException} The json parse set exception is JsonParseSetException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.CollectionType
* @see java.util.Set
* @see io.github.nichetoolkit.rest.error.json.JsonParseSetException
*/
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());
}
}
/**
* 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.
* @throws JsonParseSetException {@link io.github.nichetoolkit.rest.error.json.JsonParseSetException} The json parse set exception is JsonParseSetException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.util.Set
* @see io.github.nichetoolkit.rest.error.json.JsonParseSetException
*/
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());
}
}
/**
* 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.
* @throws JsonParseSetException {@link io.github.nichetoolkit.rest.error.json.JsonParseSetException} The json parse set exception is JsonParseSetException
type.
* @see java.util.Set
* @see java.lang.String
* @see java.lang.Class
* @see io.github.nichetoolkit.rest.error.json.JsonParseSetException
*/
public static , T> Set parseSet(String json, Class parseClazz, Class clazz) throws JsonParseSetException {
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.
* @throws JsonParseSetException {@link io.github.nichetoolkit.rest.error.json.JsonParseSetException} The json parse set exception is JsonParseSetException
type.
* @see java.lang.String
* @see java.lang.Class
* @see java.util.Set
* @see io.github.nichetoolkit.rest.error.json.JsonParseSetException
*/
public static Set parseSet(String json, Class clazz) throws JsonParseSetException {
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.
* @throws JsonParseMapException {@link io.github.nichetoolkit.rest.error.json.JsonParseMapException} The json parse map exception is JsonParseMapException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.databind.type.MapType
* @see java.util.Map
* @see io.github.nichetoolkit.rest.error.json.JsonParseMapException
*/
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());
}
}
/**
* 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.
* @throws JsonParseMapException {@link io.github.nichetoolkit.rest.error.json.JsonParseMapException} The json parse map exception is JsonParseMapException
type.
* @see java.lang.String
* @see com.fasterxml.jackson.core.type.TypeReference
* @see java.util.Map
* @see io.github.nichetoolkit.rest.error.json.JsonParseMapException
*/
public static Map parseMap(String json, TypeReference