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

com.github.cosycode.ext.se.util.JsonUtils Maven / Gradle / Ivy

Go to download

扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块

The newest version!
package com.github.cosycode.ext.se.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.cosycode.common.lang.BaseRuntimeException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;

import java.lang.reflect.Array;
import java.util.*;
import java.util.function.UnaryOperator;

/**
 * Description : 
 * 

* created in 2022/11/14 *

* * @author CPF * @since 1.8 **/ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class JsonUtils { private static JsonParser jsonParser = new GsonParser(); public interface Config { static void setJsonParser(@NonNull JsonParser jsonParser) { JsonUtils.jsonParser = jsonParser; } } private static JsonParser getJsonParser() { return jsonParser; } public static String toJson(Object obj) { return getJsonParser().toJson(obj); } public static String toFormatJson(Object obj) { return getJsonParser().toFormatJson(obj); } public static T fromJson(String json, Class classOfT) { return getJsonParser().fromJson(json, classOfT); } public static List fromJsonArray(String json, Class classOfT) { return getJsonParser().fromJsonArray(json, classOfT); } public static String processJson(String json, Class tClass, UnaryOperator operator) { return getJsonParser().processJson(json, tClass, operator); } /** * 确保转换时不丢失信息, 通过多次转换json对比转换的字符串. */ public static T fromJsonStrict(String json, Class classOfT) { Objects.requireNonNull(json); Objects.requireNonNull(classOfT); T t = fromJson(json, classOfT); String jsonAfter = toJson(t); if (json.equals(jsonAfter)) { return t; } String sortJson = toJson(fromJson(json, TreeMap.class)); String sortJsonAfter = toJson(fromJson(json, TreeMap.class)); if (sortJson.equals(sortJsonAfter)) { return t; } throw new BaseRuntimeException("json converted to classOf T, information lost,\noriginal json: %s, sorted json: %s,\njson converted to %s: %s", json, sortJson, classOfT.getName(), sortJsonAfter); } /** * 确保转换时不丢失信息, 通过多次转换json对比转换的字符串. */ public static List fromJsonArrayStrict(String json, Class classOfT) { Objects.requireNonNull(json); Objects.requireNonNull(classOfT); List tList = fromJsonArray(json, classOfT); String jsonAfter = toJson(tList); if (json.equals(jsonAfter)) { return tList; } String sortJson = toJson(fromJsonArray(json, TreeMap.class)); String sortJsonAfter = toJson(fromJsonArray(json, TreeMap.class)); if (sortJson.equals(sortJsonAfter)) { return tList; } throw new BaseRuntimeException("json converted to classOf T, information lost,\noriginal json: %s, sorted json: %s,\njson converted to List<%s>: %s", json, sortJson, classOfT.getName(), sortJsonAfter); } interface JsonParser { String toJson(Object obj); String toFormatJson(Object obj); T fromJson(String json, Class classOfT); List fromJsonArray(String json, Class classOfT); /** * 将 json 转换为 类型 T, 由 operator 处理后, 再转换为 json 返回. * * @param json json字符串 * @param tClass json 字符串对应的类 * @param operator 函数式操作接口 * @param json 字符串对应的 类型 * @return null(如果 operator 返回 null), 否则会返回 operator的返回值 转化后的 json */ default String processJson(String json, Class tClass, UnaryOperator operator) { T before = fromJson(json, tClass); T after = operator.apply(before); if (after == null) { return null; } return toJson(after); } } public static class GsonParser implements JsonParser { @Override public String toJson(Object obj) { return new Gson().toJson(obj); } @Override public String toFormatJson(Object obj) { return new GsonBuilder().setPrettyPrinting().create().toJson(obj); } @Override public T fromJson(String json, Class classOfT) { return new Gson().fromJson(json, classOfT); } @Override public List fromJsonArray(String json, Class classOfT) { Class arrClass = (Class) Array.newInstance(classOfT, 0).getClass(); return nativeFromJsonArray(json, arrClass); } private List nativeFromJsonArray(String json, Class classOfT) { T[] ts = new Gson().fromJson(json, classOfT); return Arrays.asList(ts); } } public static class JacksonParser implements JsonParser { /** * jackson 转换对象, 线程安全, 可以单例 */ public static final ObjectMapper MAPPER = new ObjectMapper(); @Override public String toJson(Object obj) { if (obj == null) { return "{}"; } try { return MAPPER.writeValueAsString(obj); } catch (JsonProcessingException e) { throw new BaseRuntimeException("[JacksonParser] writeValueAsString exception", e); } } @Override public String toFormatJson(Object obj) { if (obj == null) { return "{}"; } try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException e) { throw new BaseRuntimeException("[JacksonParser] writeValueAsString exception", e); } } @Override public T fromJson(String json, Class clazz) { if (json == null) { return null; } try { return MAPPER.readValue(json, clazz); } catch (JsonProcessingException e) { throw new BaseRuntimeException("[JacksonParser] fromJson exception", e); } } @Override public List fromJsonArray(String json, Class classOfT) { Class arrClass = (Class) Array.newInstance(classOfT, 0).getClass(); try { T[] array = MAPPER.readValue(json, arrClass); List ts = new ArrayList<>((int) (array.length * 1.4)); Collections.addAll(ts, array); return ts; } catch (JsonProcessingException e) { throw new BaseRuntimeException("[JacksonParser] fromJsonArray", e); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy