All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.nichetoolkit.rest.helper.JsonHelper Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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> typeReference) throws JsonParseMapException { if (GeneralUtils.isEmpty(json)) { return Collections.emptyMap(); } try { return ObjectMapperHolder.objectMapper().readValue(json, typeReference); } catch (JsonProcessingException exception) { throw new JsonParseMapException("parseMap", typeReference.getType().getTypeName(),json, exception.getMessage()); } } /** * json字符串解析为BeanMap * @param json json字符串数据 * @param parseClazz Map类 * @param keyClazz key类 * @param valueClazz value类 * @param Map类型 * @param key类型 * @param value类型 * @return Map */ public static Map parseMap(String json, Class parseClazz, Class keyClazz, Class valueClazz) throws JsonParseMapException { MapType mapType = TypeFactory.defaultInstance().constructMapType(parseClazz, keyClazz, valueClazz); return parseMap(json, mapType); } /** * json字符串解析为BeanMap * @param json json字符串数据 * @param keyClazz key类 * @param valueClazz value类 * @param key类型 * @param value类型 * @return Map BeanMap */ public static Map parseMap(String json, Class keyClazz, Class valueClazz) throws JsonParseMapException { return parseMap(json, Map.class, keyClazz, valueClazz); } /** * json字符串解析为BeanMapList * @param json json字符串数据 * @param parseListClazz List类 * @param parseMapClazz Map类 * @param keyClazz key类 * @param valueClazz value类 * @param List类型 * @param Map类型 * @param key类型 * @param value类型 * @return Map> BeanMapList */ public static Map> parseMapList(String json, Class parseListClazz, Class parseMapClazz, Class keyClazz, Class valueClazz) throws JsonParseMapException { CollectionType collectionType = TypeFactory.defaultInstance().constructCollectionType(parseListClazz, valueClazz); MapType mapType = TypeFactory.defaultInstance().constructMapType(parseMapClazz, keyClazz, collectionType.getRawClass()); return parseMap(json, mapType); } /** * json多层map数据解析 * @param json json数据 * @param contentKeyClazz 内层mapKey * @param contentValueClazz 内层mapValue * @param 内层mapKey类型 * @param 内层mapValue类型 * @return Map> BeanMapMap */ public static List> parseListMap(String json, Class wrapKeyClazz, Class contentMapClazz, Class contentKeyClazz, Class contentValueClazz) throws JsonParseListException { MapType contentType = TypeFactory.defaultInstance().constructMapType(contentMapClazz, contentKeyClazz, contentValueClazz); CollectionType collectionType = TypeFactory.defaultInstance().constructCollectionType(wrapKeyClazz, contentType.getRawClass()); return parseList(json, collectionType); } /** * json多层map数据解析 * @param json json数据 * @param contentKeyClazz 内层mapKey * @param contentValueClazz 内层mapValue * @param 内层mapKey类型 * @param 内层mapValue类型 * @return Map> BeanMapMap */ public static List> parseListMap(String json, Class contentKeyClazz, Class contentValueClazz) throws JsonParseListException { MapType contentType = TypeFactory.defaultInstance().constructMapType(Map.class, contentKeyClazz, contentValueClazz); CollectionType collectionType = TypeFactory.defaultInstance().constructCollectionType(List.class, contentType.getRawClass()); return parseList(json, collectionType); } /** * json字符串解析为BeanMapList * @param json json字符串数据 * @param keyClazz key类 * @param valueClazz value类 * @param key类型 * @param value类型 * @return Map> BeanMapList */ public static Map> parseMapList(String json, Class keyClazz, Class valueClazz) throws JsonParseMapException { return parseMapList(json, List.class, Map.class, keyClazz, valueClazz); } /** * json字符串解析为BeanMapList * @param json json字符串数据 * @param wrapMapClazz 外层Map类 * @param contentMapClazz 内层Map类 * @param wrapKeyClazz 外层key类型 * @param contentKeyClazz 内层mapKey * @param contentValueClazz 内层mapValue * @param 外层Map类型 * @param 内层Map类型 * @param 外层mapKey类型 * @param 内层mapKey类型 * @param 内层mapValue类型 * @return Map> BeanMapMap */ public static Map> parseMapMap(String json, Class wrapMapClazz, Class contentMapClazz, Class wrapKeyClazz, Class contentKeyClazz, Class contentValueClazz) throws JsonParseMapException { MapType contentType = TypeFactory.defaultInstance().constructMapType(contentMapClazz, contentKeyClazz, contentValueClazz); MapType mapType = TypeFactory.defaultInstance().constructMapType(wrapMapClazz, wrapKeyClazz, contentType.getRawClass()); return parseMap(json, mapType); } /** * json多层map数据解析 * @param json json数据 * @param wrapKeyClazz 外层mapKey * @param contentKeyClazz 内层mapKey * @param contentValueClazz 内层mapValue * @param 外层mapKey类型 * @param 内层mapKey类型 * @param 内层mapValue类型 * @return Map> BeanMapMap */ public static Map> parseMapMap(String json, Class wrapKeyClazz, Class contentKeyClazz, Class contentValueClazz) throws JsonParseMapException { return parseMapMap(json, Map.class, Map.class, wrapKeyClazz, contentKeyClazz, contentValueClazz); } public static RestResult parseResult(String json, TypeReference> typeReference) throws JsonParseBeanException { return parseBean(json, typeReference); } public static RestResult parseResult(String json, Class clazz) throws JsonParseBeanException { JavaType javaType = TypeFactory.defaultInstance().constructParametricType(RestResult.class, clazz); return parseBean(json, javaType); } public static RestResult parseResult(String json, JavaType innerType) throws JsonParseBeanException { JavaType javaType = TypeFactory.defaultInstance().constructParametricType(RestResult.class, innerType); return parseBean(json, javaType); } public static RestResult parseResult(String json) throws JsonParseBeanException { if (GeneralUtils.isEmpty(json)) { return null; } try { JsonNode jsonNode = ObjectMapperHolder.objectMapper().readTree(json); if (GeneralUtils.isNotEmpty(jsonNode)) { RestResult restResult = new RestResult<>(); JsonNode status = jsonNode.get(DefaultResult.STATUS_NAME); if (GeneralUtils.isNotEmpty(status)) { restResult.setStatus(status.asInt()); } JsonNode message = jsonNode.get(DefaultResult.MESSAGE_NAME); if (GeneralUtils.isNotEmpty(message)) { restResult.setMessage(message.toString()); } JsonNode data = jsonNode.get(DefaultResult.DATA_NAME); if (GeneralUtils.isNotEmpty(data)) { restResult.setData(data.toString()); } return restResult; } return null; } catch (JsonProcessingException exception) { throw new JsonParseBeanException("parseResult", RestResult.class.getName(), json, exception.getMessage()); } } public static T parseConvert(Object value, Class clazz) throws ClassUnsupportedException { if (GeneralUtils.isEmpty(value)) { return null; } try { if (value.getClass().equals(clazz)) { return ObjectMapperHolder.objectMapper().convertValue(value, clazz); } else { throw new ClassUnsupportedException("parseConvert",clazz.getName(),value.getClass().getName(), clazz.getName()); } } catch (Exception exception) { throw new ClassUnsupportedException(value.getClass().getName(), clazz.getName(), exception.getMessage()); } } }