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

io.github.nichetoolkit.rest.util.JsonUtils Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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> typeReference) { try { return JsonHelper.parseMap(json, typeReference); } catch (JsonParseMapException exception) { log.error("It is failed during json to parse as map of bean! {}", exception.getMessage()); exception.printStackTrace(); return Collections.emptyMap(); } } /** * json字符串解析为BeanList * @param json json字符串数据 * @param arrayType 序列化指定的ArrayType类型 * @param Bean类型 * @return List BeanList */ public static T[] parseArray(String json, ArrayType arrayType) { try { return JsonHelper.parseArray(json, arrayType); } catch (JsonParseListException exception) { log.error("It is failed during json to parse as array of bean! {}", exception.getMessage()); exception.printStackTrace(); return null; } } public static T[] parseArray(String json, TypeReference typeReference) { try { return JsonHelper.parseArray(json, typeReference); } catch (JsonParseListException exception) { log.error("It is failed during json to parse as array of bean! {}", exception.getMessage()); exception.printStackTrace(); return null; } } /** * json字符串解析为BeanList * @param json json字符串数据 * @param clazz Bean类 * @param List类型 * @param Bean类型 * @return List BeanList */ public static T[] parseArray(String json, Class clazz) { ArrayType arrayType = TypeFactory.defaultInstance().constructArrayType(clazz); return parseArray(json, arrayType); } /** * json字符串解析为BeanMap * @param json json字符串数据 * @param parseClazz Map类 * @param keyClazz key类 * @param valueClazz value类 * @param Map类型 * @param key类型 * @param value类型 * @return Map BeanMap */ public static Map parseMap(String json, Class parseClazz, Class keyClazz, Class valueClazz) { 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) { 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) { CollectionType collectionType = TypeFactory.defaultInstance().constructCollectionType(parseListClazz, valueClazz); MapType mapType = TypeFactory.defaultInstance().constructMapType(parseMapClazz, keyClazz, collectionType.getRawClass()); return parseMap(json, mapType); } /** * 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) { return parseMapList(json, List.class, Map.class, keyClazz, valueClazz); } /** * 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) { 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) { 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 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) { 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) { return parseMapMap(json, Map.class, Map.class, wrapKeyClazz, contentKeyClazz, contentValueClazz); } public static RestResult parseResult(String json, TypeReference typeReference) { JavaType javaType = TypeFactory.defaultInstance().constructType(typeReference); JavaType parametricType = TypeFactory.defaultInstance().constructParametricType(RestResult.class, javaType); try { return JsonHelper.parseResult(json, parametricType); } catch (JsonParseBeanException exception) { log.error("It is failed during json to parse as type reference result of restResult! {}", exception.getMessage()); exception.printStackTrace(); return null; } } public static RestResult parseResult(String json, JavaType javaType) { try { return JsonHelper.parseResult(json, javaType); } catch (JsonParseBeanException exception) { log.error("It is failed during json to parse as java type result of restResult! {}", exception.getMessage()); exception.printStackTrace(); return null; } } public static RestResult parseResult(String json, Class clazz) { try { return JsonHelper.parseResult(json, clazz); } catch (JsonParseBeanException exception) { log.error("It is failed during json to parse as clazz result of restResult! {}", exception.getMessage()); exception.printStackTrace(); return null; } } public static RestResult parseResult(String json) { try { return JsonHelper.parseResult(json); } catch (JsonParseBeanException exception) { log.error("It is failed during json to parse as result of restResult! {}", exception.getMessage()); exception.printStackTrace(); return null; } } public static T parseConvert(Object value, Class clazz) { try { return JsonHelper.parseConvert(value, clazz); } catch (ClassUnsupportedException exception) { log.error("It is failed during object to convert as clazz of bean! {}", exception.getMessage()); exception.printStackTrace(); return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy