Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d118ed98d
|
||
|
|
6b7555d727
|
||
|
|
baa5aafe23
|
||
|
|
2c29e40e47
|
||
|
|
3dbb686a66
|
||
|
|
06ae1d0179
|
||
|
|
972ed6d338
|
+1
-1
@@ -22,4 +22,4 @@
|
|||||||
JDKVERSIONS=1.8
|
JDKVERSIONS=1.8
|
||||||
GROUPSID=com.mingliqiye.utils
|
GROUPSID=com.mingliqiye.utils
|
||||||
ARTIFACTID=mingli-utils
|
ARTIFACTID=mingli-utils
|
||||||
VERSIONS=3.1.2
|
VERSIONS=3.2.5
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* AES 加密
|
||||||
|
* @author MingLiPro
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.mingliqiye.utils.aes;
|
||||||
@@ -23,13 +23,14 @@
|
|||||||
package com.mingliqiye.utils.collection;
|
package com.mingliqiye.utils.collection;
|
||||||
|
|
||||||
import com.mingliqiye.utils.stream.SuperStream;
|
import com.mingliqiye.utils.stream.SuperStream;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 集合工具类,提供对列表和数组的常用操作方法。
|
* 集合工具类,提供对列表和数组的常用操作方法。
|
||||||
@@ -337,4 +338,10 @@ public class Collection {
|
|||||||
) {
|
) {
|
||||||
return new ArrayList<>(collection);
|
return new ArrayList<>(collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> SuperStream<T> toSuperStream(
|
||||||
|
@NotNull java.util.Collection<T> list
|
||||||
|
) {
|
||||||
|
return SuperStream.of(list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package com.mingliqiye.utils.json;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
|
|
||||||
|
public class GsonJsonApi implements JsonApi {
|
||||||
|
|
||||||
|
private final Gson gson;
|
||||||
|
private final Gson gsonUnicode;
|
||||||
|
private final Gson gsonPretty;
|
||||||
|
private final Gson gsonPrettyUnicode;
|
||||||
|
|
||||||
|
public GsonJsonApi() {
|
||||||
|
gson = new GsonBuilder()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
gsonUnicode = new GsonBuilder()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
gsonPretty = new GsonBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
gsonPrettyUnicode = new GsonBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GsonJsonApi(Gson gson) {
|
||||||
|
this.gson = gson
|
||||||
|
.newBuilder()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
this.gsonUnicode = gson
|
||||||
|
.newBuilder()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
this.gsonPretty = gson
|
||||||
|
.newBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
this.gsonPrettyUnicode = gson
|
||||||
|
.newBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||||
|
.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T parse(String json, Class<T> clazz) {
|
||||||
|
return gson.fromJson(json, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T parse(String json, JsonTypeReference<T> type) {
|
||||||
|
return gson.fromJson(json, type.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String format(Object object) {
|
||||||
|
return gson.toJson(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String formatUnicode(Object object) {
|
||||||
|
return gsonUnicode.toJson(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String formatPretty(Object object) {
|
||||||
|
return gsonPretty.toJson(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String formatPrettyUnicode(Object object) {
|
||||||
|
return gsonPrettyUnicode.toJson(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValidJson(String json) {
|
||||||
|
try {
|
||||||
|
JsonElement element = JsonParser.parseString(json);
|
||||||
|
return true;
|
||||||
|
} catch (JsonSyntaxException e) {
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String merge(String... jsons) {
|
||||||
|
JsonObject merged = new JsonObject();
|
||||||
|
for (String json : jsons) {
|
||||||
|
if (json == null || json.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
|
||||||
|
for (String key : obj.keySet()) {
|
||||||
|
merged.add(key, obj.get(key));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 忽略无效的 JSON 字符串
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return gson.toJson(merged);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNodeValue(String json, String path) {
|
||||||
|
try {
|
||||||
|
JsonElement element = JsonParser.parseString(json);
|
||||||
|
String[] paths = path.split("\\.");
|
||||||
|
JsonElement current = element;
|
||||||
|
|
||||||
|
for (String p : paths) {
|
||||||
|
if (current.isJsonObject()) {
|
||||||
|
current = current.getAsJsonObject().get(p);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return current.isJsonPrimitive()
|
||||||
|
? current.getAsString()
|
||||||
|
: current.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String updateNodeValue(String json, String path, Object newValue) {
|
||||||
|
try {
|
||||||
|
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
|
||||||
|
String[] paths = path.split("\\.");
|
||||||
|
JsonObject current = obj;
|
||||||
|
|
||||||
|
// 导航到倒数第二层
|
||||||
|
for (int i = 0; i < paths.length - 1; i++) {
|
||||||
|
String p = paths[i];
|
||||||
|
if (!current.has(p) || !current.get(p).isJsonObject()) {
|
||||||
|
current.add(p, new JsonObject());
|
||||||
|
}
|
||||||
|
current = current.getAsJsonObject(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置最后一层的值
|
||||||
|
String lastPath = paths[paths.length - 1];
|
||||||
|
if (newValue == null) {
|
||||||
|
current.remove(lastPath);
|
||||||
|
} else {
|
||||||
|
JsonElement element = gson.toJsonTree(newValue);
|
||||||
|
current.add(lastPath, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gson.toJson(obj);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T, D> D convert(T source, Class<D> destinationClass) {
|
||||||
|
String json = gson.toJson(source);
|
||||||
|
return gson.fromJson(json, destinationClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T, D> D convert(T source, JsonTypeReference<D> destinationType) {
|
||||||
|
String json = gson.toJson(source);
|
||||||
|
return gson.fromJson(json, destinationType.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,7 +22,6 @@
|
|||||||
|
|
||||||
package com.mingliqiye.utils.json;
|
package com.mingliqiye.utils.json;
|
||||||
|
|
||||||
import com.mingliqiye.utils.collection.Maps;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -34,8 +33,6 @@ import java.util.Map;
|
|||||||
* JSON处理接口,提供JSON字符串与Java对象之间的相互转换功能
|
* JSON处理接口,提供JSON字符串与Java对象之间的相互转换功能
|
||||||
*/
|
*/
|
||||||
public interface JsonApi {
|
public interface JsonApi {
|
||||||
final Map<String, String> UNICODE_BIND = Maps.of("1", "");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定类型的对象
|
* 将JSON字符串解析为指定类型的对象
|
||||||
*
|
*
|
||||||
|
|||||||
+70
@@ -0,0 +1,70 @@
|
|||||||
|
package com.mingliqiye.utils.json.converters;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONReader;
|
||||||
|
import com.alibaba.fastjson2.JSONWriter;
|
||||||
|
import com.alibaba.fastjson2.reader.ObjectReader;
|
||||||
|
import com.alibaba.fastjson2.writer.ObjectWriter;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
public class FastjsonJsonStringConverterAdapter<
|
||||||
|
T extends JsonStringConverter<TT>,
|
||||||
|
TT
|
||||||
|
> {
|
||||||
|
|
||||||
|
private final JsonStringConverter<TT> jsonStringConverter;
|
||||||
|
|
||||||
|
public FastjsonJsonStringConverterAdapter(T jsonStringConverter) {
|
||||||
|
this.jsonStringConverter = jsonStringConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <
|
||||||
|
T extends JsonStringConverter<TT>,
|
||||||
|
TT
|
||||||
|
> FastjsonJsonStringConverterAdapter<T, TT> of(T t) {
|
||||||
|
return new FastjsonJsonStringConverterAdapter<>(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取FastJson对象写入器
|
||||||
|
*
|
||||||
|
* @return FastJson的ObjectWriter实例
|
||||||
|
*/
|
||||||
|
public ObjectWriter<T> getFastJsonObjectWriter() {
|
||||||
|
return (
|
||||||
|
JSONWriter writer,
|
||||||
|
Object object,
|
||||||
|
Object fieldName,
|
||||||
|
Type fieldType,
|
||||||
|
long features
|
||||||
|
) -> {
|
||||||
|
// 如果对象为null则写入null
|
||||||
|
if (object == null) {
|
||||||
|
writer.writeNull();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
writer.writeString(jsonStringConverter.convert((TT) object));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取FastJson对象读取器
|
||||||
|
*
|
||||||
|
* @return FastJson的ObjectReader实例
|
||||||
|
*/
|
||||||
|
public ObjectReader<TT> getFastJsonObjectReader() {
|
||||||
|
return (
|
||||||
|
JSONReader reader,
|
||||||
|
Type fieldType,
|
||||||
|
Object fieldName,
|
||||||
|
long features
|
||||||
|
) -> {
|
||||||
|
String value = reader.readString();
|
||||||
|
return jsonStringConverter.deConvert(value);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package com.mingliqiye.utils.json.converters;
|
||||||
|
|
||||||
|
import com.google.gson.TypeAdapter;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class GsonJsonStringConverterAdapter<
|
||||||
|
T extends JsonStringConverter<TT>,
|
||||||
|
TT
|
||||||
|
> {
|
||||||
|
|
||||||
|
private final JsonStringConverter<TT> jsonStringConverter;
|
||||||
|
|
||||||
|
public GsonJsonStringConverterAdapter(T jsonStringConverter) {
|
||||||
|
this.jsonStringConverter = jsonStringConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <
|
||||||
|
T extends JsonStringConverter<TT>,
|
||||||
|
TT
|
||||||
|
> GsonJsonStringConverterAdapter<T, TT> of(T t) {
|
||||||
|
return new GsonJsonStringConverterAdapter<>(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Gson类型适配器
|
||||||
|
*
|
||||||
|
* @return Gson的TypeAdapter实例
|
||||||
|
*/
|
||||||
|
public TypeAdapter<TT> getGsonTypeAdapter() {
|
||||||
|
return new TypeAdapter<TT>() {
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter out, TT value) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
out.nullValue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
out.value(jsonStringConverter.convert(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TT read(JsonReader in) throws IOException {
|
||||||
|
String value = in.nextString();
|
||||||
|
return jsonStringConverter.deConvert(value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
+91
@@ -0,0 +1,91 @@
|
|||||||
|
package com.mingliqiye.utils.json.converters;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.*;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON转换器的适配器
|
||||||
|
* @param <T> JSON转换器
|
||||||
|
* @param <TT> JSON转换器的泛型
|
||||||
|
* @author MingLiPro
|
||||||
|
*/
|
||||||
|
public class JacksonJsonStringConverterAdapter<
|
||||||
|
T extends JsonStringConverter<TT>,
|
||||||
|
TT
|
||||||
|
> {
|
||||||
|
|
||||||
|
private final JsonStringConverter<TT> jsonStringConverter;
|
||||||
|
|
||||||
|
private JacksonJsonStringConverterAdapter(T jsonStringConverter) {
|
||||||
|
this.jsonStringConverter = jsonStringConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param t JSON转换器实例
|
||||||
|
* @return JSON转换器的适配器
|
||||||
|
* @param <T> JSON转换器
|
||||||
|
* @param <TT> JSON转换器的泛型
|
||||||
|
*/
|
||||||
|
public static <
|
||||||
|
T extends JsonStringConverter<TT>,
|
||||||
|
TT
|
||||||
|
> JacksonJsonStringConverterAdapter<T, TT> of(T t) {
|
||||||
|
return new JacksonJsonStringConverterAdapter<>(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Jackson反序列化器
|
||||||
|
*
|
||||||
|
* @return Jackson的JsonDeserializer实例
|
||||||
|
*/
|
||||||
|
public JsonDeserializer<TT> getJacksonJsonDeserializer() {
|
||||||
|
return new JsonDeserializer<TT>() {
|
||||||
|
@Override
|
||||||
|
public TT deserialize(JsonParser p, DeserializationContext ctxt)
|
||||||
|
throws IOException {
|
||||||
|
if (p.isNaN()) return null;
|
||||||
|
return jsonStringConverter.deConvert(p.getValueAsString());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Jackson序列化器
|
||||||
|
*
|
||||||
|
* @return Jackson的JsonSerializer实例
|
||||||
|
*/
|
||||||
|
public JsonSerializer<TT> getJacksonJsonSerializer() {
|
||||||
|
return new JsonSerializer<TT>() {
|
||||||
|
@Override
|
||||||
|
public void serialize(
|
||||||
|
TT value,
|
||||||
|
JsonGenerator gen,
|
||||||
|
SerializerProvider serializers
|
||||||
|
) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
gen.writeNull();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
gen.writeString(jsonStringConverter.convert(value));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 获取 Jackson 的格式化模块
|
||||||
|
*
|
||||||
|
* @return 格式化模块
|
||||||
|
*/
|
||||||
|
public Module getJacksonModule() {
|
||||||
|
Class<TT> tClass = jsonStringConverter.getTClass();
|
||||||
|
SimpleModule m = new SimpleModule(tClass.getSimpleName());
|
||||||
|
m.addSerializer(tClass, getJacksonJsonSerializer());
|
||||||
|
m.addDeserializer(tClass, getJacksonJsonDeserializer());
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +1,5 @@
|
|||||||
package com.mingliqiye.utils.json.converters;
|
package com.mingliqiye.utils.json.converters;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONReader;
|
|
||||||
import com.alibaba.fastjson2.JSONWriter;
|
|
||||||
import com.alibaba.fastjson2.reader.ObjectReader;
|
|
||||||
import com.alibaba.fastjson2.writer.ObjectWriter;
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
|
||||||
import com.fasterxml.jackson.databind.*;
|
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
||||||
import com.google.gson.TypeAdapter;
|
|
||||||
import com.google.gson.stream.JsonReader;
|
|
||||||
import com.google.gson.stream.JsonWriter;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON转换器接口,提供对象与字符串之间的相互转换功能,并支持多种JSON库
|
* JSON转换器接口,提供对象与字符串之间的相互转换功能,并支持多种JSON库
|
||||||
*
|
*
|
||||||
@@ -41,118 +26,35 @@ public abstract class JsonStringConverter<T> {
|
|||||||
abstract T deConvert(String string);
|
abstract T deConvert(String string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Jackson反序列化器
|
* 获取 Fastjson 的适配器
|
||||||
*
|
* @return 适配器实例
|
||||||
* @return Jackson的JsonDeserializer实例
|
|
||||||
*/
|
*/
|
||||||
public JsonDeserializer<T> getJacksonJsonDeserializer() {
|
public FastjsonJsonStringConverterAdapter<
|
||||||
return new JsonDeserializer<T>() {
|
JsonStringConverter<T>,
|
||||||
@Override
|
T
|
||||||
public T deserialize(JsonParser p, DeserializationContext ctxt)
|
> getFastjsonJsonStringConverterAdapter() {
|
||||||
throws IOException {
|
return FastjsonJsonStringConverterAdapter.of(this);
|
||||||
// 如果是NaN则返回null
|
|
||||||
if (p.isNaN()) return null;
|
|
||||||
return deConvert(p.getValueAsString());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Jackson序列化器
|
* 获取 Gson 的适配器
|
||||||
*
|
* @return 适配器实例
|
||||||
* @return Jackson的JsonSerializer实例
|
|
||||||
*/
|
*/
|
||||||
public JsonSerializer<T> getJacksonJsonSerializer() {
|
public GsonJsonStringConverterAdapter<
|
||||||
return new JsonSerializer<T>() {
|
JsonStringConverter<T>,
|
||||||
@Override
|
T
|
||||||
public void serialize(
|
> getGsonJsonStringConverterAdapter() {
|
||||||
T value,
|
return GsonJsonStringConverterAdapter.of(this);
|
||||||
JsonGenerator gen,
|
|
||||||
SerializerProvider serializers
|
|
||||||
) throws IOException {
|
|
||||||
// 如果值为null则写入null
|
|
||||||
if (value == null) {
|
|
||||||
gen.writeNull();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
gen.writeString(convert(value));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public Module getJacksonModule() {
|
|
||||||
Class<T> tClass = getTClass();
|
|
||||||
SimpleModule m = new SimpleModule(tClass.getSimpleName());
|
|
||||||
m.addSerializer(tClass, getJacksonJsonSerializer());
|
|
||||||
m.addDeserializer(tClass, getJacksonJsonDeserializer());
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Gson类型适配器
|
* 获取 Jackson 的适配器
|
||||||
*
|
* @return 适配器实例
|
||||||
* @return Gson的TypeAdapter实例
|
|
||||||
*/
|
*/
|
||||||
public TypeAdapter<T> getGsonTypeAdapter() {
|
public JacksonJsonStringConverterAdapter<
|
||||||
return new TypeAdapter<T>() {
|
JsonStringConverter<T>,
|
||||||
@Override
|
T
|
||||||
public void write(JsonWriter out, T value) throws IOException {
|
> getJacksonJsonStringConverterAdapter() {
|
||||||
// 如果值为null则写入null值
|
return JacksonJsonStringConverterAdapter.of(this);
|
||||||
if (value == null) {
|
|
||||||
out.nullValue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
out.value(convert(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T read(JsonReader in) throws IOException {
|
|
||||||
String value = in.nextString();
|
|
||||||
return deConvert(value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FastJson 支持
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取FastJson对象写入器
|
|
||||||
*
|
|
||||||
* @return FastJson的ObjectWriter实例
|
|
||||||
*/
|
|
||||||
public ObjectWriter<T> getFastJsonObjectWriter() {
|
|
||||||
return (
|
|
||||||
JSONWriter writer,
|
|
||||||
Object object,
|
|
||||||
Object fieldName,
|
|
||||||
Type fieldType,
|
|
||||||
long features
|
|
||||||
) -> {
|
|
||||||
// 如果对象为null则写入null
|
|
||||||
if (object == null) {
|
|
||||||
writer.writeNull();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
writer.writeString(convert((T) object));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取FastJson对象读取器
|
|
||||||
*
|
|
||||||
* @return FastJson的ObjectReader实例
|
|
||||||
*/
|
|
||||||
public ObjectReader<T> getFastJsonObjectReader() {
|
|
||||||
return (
|
|
||||||
JSONReader reader,
|
|
||||||
Type fieldType,
|
|
||||||
Object fieldName,
|
|
||||||
long features
|
|
||||||
) -> {
|
|
||||||
String value = reader.readString();
|
|
||||||
return deConvert(value);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,14 +25,13 @@ package com.mingliqiye.utils.springboot.autoconfigure;
|
|||||||
import com.mingliqiye.utils.collection.ForEach;
|
import com.mingliqiye.utils.collection.ForEach;
|
||||||
import com.mingliqiye.utils.time.DateTime;
|
import com.mingliqiye.utils.time.DateTime;
|
||||||
import com.mingliqiye.utils.time.Formatter;
|
import com.mingliqiye.utils.time.Formatter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
@org.springframework.boot.autoconfigure.AutoConfiguration
|
@org.springframework.boot.autoconfigure.AutoConfiguration
|
||||||
@EnableConfigurationProperties(AutoConfiguration.class)
|
@EnableConfigurationProperties(AutoConfiguration.class)
|
||||||
@ComponentScan(
|
@ComponentScan(
|
||||||
|
|||||||
+26
-11
@@ -2,6 +2,8 @@ package com.mingliqiye.utils.springboot.autoconfigure;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.mingliqiye.utils.json.GsonJsonApi;
|
||||||
|
import com.mingliqiye.utils.json.JsonApi;
|
||||||
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter;
|
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter;
|
||||||
import com.mingliqiye.utils.json.converters.JsonStringConverter;
|
import com.mingliqiye.utils.json.converters.JsonStringConverter;
|
||||||
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter;
|
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter;
|
||||||
@@ -13,14 +15,15 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.gson.GsonBuilderCustomizer;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
|
|
||||||
@ConditionalOnClass(Gson.class)
|
@ConditionalOnClass(Gson.class)
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
@AutoConfigureAfter(
|
@AutoConfigureAfter(
|
||||||
name = {
|
name = {
|
||||||
"org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration",
|
"org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration",
|
||||||
|
"com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public class GsonAutoConfiguration {
|
public class GsonAutoConfiguration {
|
||||||
@@ -34,25 +37,37 @@ public class GsonAutoConfiguration {
|
|||||||
new DateTimeJsonConverter();
|
new DateTimeJsonConverter();
|
||||||
JsonStringConverter<UUID> uuidJsonStringConverter =
|
JsonStringConverter<UUID> uuidJsonStringConverter =
|
||||||
new UUIDJsonStringConverter();
|
new UUIDJsonStringConverter();
|
||||||
|
|
||||||
|
try {
|
||||||
return gsonBuilder
|
return gsonBuilder
|
||||||
.registerTypeAdapter(
|
.registerTypeAdapter(
|
||||||
uuidJsonStringConverter.getTClass(),
|
uuidJsonStringConverter.getTClass(),
|
||||||
uuidJsonStringConverter.getGsonTypeAdapter()
|
dateTimeJsonConverter
|
||||||
|
.getGsonJsonStringConverterAdapter()
|
||||||
|
.getGsonTypeAdapter()
|
||||||
)
|
)
|
||||||
.registerTypeAdapter(
|
.registerTypeAdapter(
|
||||||
dateTimeJsonConverter.getTClass(),
|
dateTimeJsonConverter.getTClass(),
|
||||||
dateTimeJsonConverter.getGsonTypeAdapter()
|
dateTimeJsonConverter
|
||||||
|
.getGsonJsonStringConverterAdapter()
|
||||||
|
.getGsonTypeAdapter()
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
public GsonBuilder gsonBuilder() {
|
|
||||||
try {
|
|
||||||
return addTypeAdapter(new GsonBuilder());
|
|
||||||
} finally {
|
} finally {
|
||||||
log.info("MingliUtils GsonBuilder TypeAdapter add");
|
log.info("MingliUtils GsonBuilder TypeAdapter add");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public GsonBuilderCustomizer mingliGsonCustomizer() {
|
||||||
|
return GsonAutoConfiguration::addTypeAdapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public JsonApi jsonApi(Gson gson) {
|
||||||
|
log.info(
|
||||||
|
"MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created."
|
||||||
|
);
|
||||||
|
return new GsonJsonApi(gson);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-2
@@ -1,6 +1,8 @@
|
|||||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.mingliqiye.utils.json.JacksonJsonApi;
|
||||||
|
import com.mingliqiye.utils.json.JsonApi;
|
||||||
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter;
|
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter;
|
||||||
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter;
|
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -8,6 +10,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
@ConditionalOnClass(ObjectMapper.class)
|
@ConditionalOnClass(ObjectMapper.class)
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
@@ -29,7 +34,25 @@ public class JacksonAutoConfiguration {
|
|||||||
|
|
||||||
public static ObjectMapper addModules(ObjectMapper objectMapper) {
|
public static ObjectMapper addModules(ObjectMapper objectMapper) {
|
||||||
return objectMapper
|
return objectMapper
|
||||||
.registerModule(new UUIDJsonStringConverter().getJacksonModule())
|
.registerModule(
|
||||||
.registerModule(new DateTimeJsonConverter().getJacksonModule());
|
new DateTimeJsonConverter()
|
||||||
|
.getJacksonJsonStringConverterAdapter()
|
||||||
|
.getJacksonModule()
|
||||||
|
)
|
||||||
|
.registerModule(
|
||||||
|
new UUIDJsonStringConverter()
|
||||||
|
.getJacksonJsonStringConverterAdapter()
|
||||||
|
.getJacksonModule()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public JsonApi jsonApi(ObjectMapper objectMapper) {
|
||||||
|
log.info(
|
||||||
|
"MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created."
|
||||||
|
);
|
||||||
|
return new JacksonJsonApi(objectMapper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
package com.mingliqiye.utils.springboot.converters;
|
package com.mingliqiye.utils.springboot.converters;
|
||||||
|
|
||||||
|
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME;
|
||||||
|
|
||||||
import com.mingliqiye.utils.time.DateTime;
|
import com.mingliqiye.utils.time.DateTime;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* spring boot DateTime到字符串转换器
|
* spring boot DateTime到字符串转换器
|
||||||
*
|
*
|
||||||
|
|||||||
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
package com.mingliqiye.utils.springboot.converters;
|
package com.mingliqiye.utils.springboot.converters;
|
||||||
|
|
||||||
|
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME_MILLISECOUND7;
|
||||||
|
|
||||||
import com.mingliqiye.utils.time.DateTime;
|
import com.mingliqiye.utils.time.DateTime;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME_MILLISECOUND7;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* spring boot 字符串到DateTime转换器
|
* spring boot 字符串到DateTime转换器
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package com.mingliqiye.utils.stream;
|
package com.mingliqiye.utils.stream;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.List;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class InputStreanWrapper extends InputStream implements AutoCloseable {
|
public class InputStreanWrapper extends InputStream implements AutoCloseable {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@@ -72,9 +75,9 @@ public class InputStreanWrapper extends InputStream implements AutoCloseable {
|
|||||||
* jdk8 兼容实现 jdk9+ <br>
|
* jdk8 兼容实现 jdk9+ <br>
|
||||||
* 请使用 InputStream.transferTo()
|
* 请使用 InputStream.transferTo()
|
||||||
*
|
*
|
||||||
* @param outputStream
|
* @param outputStream 输出流
|
||||||
* @return 转换的字节数
|
* @return 转换的字节数
|
||||||
* @throws IOException
|
* @throws IOException IO错误
|
||||||
*/
|
*/
|
||||||
public long transferToOutputStream(OutputStream outputStream)
|
public long transferToOutputStream(OutputStream outputStream)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|||||||
@@ -295,8 +295,9 @@ public class SuperStream<T> implements Stream<T> {
|
|||||||
*
|
*
|
||||||
* @param predicate 谓词函数
|
* @param predicate 谓词函数
|
||||||
* @return SuperStream 实例
|
* @return SuperStream 实例
|
||||||
|
* @since 3.2.5
|
||||||
*/
|
*/
|
||||||
public SuperStream<T> filter(Function<T, Boolean> predicate) {
|
public SuperStream<T> filterBoolean(Function<T, Boolean> predicate) {
|
||||||
return new SuperStream<>(stream.filter(predicate::apply));
|
return new SuperStream<>(stream.filter(predicate::apply));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1430,6 +1431,8 @@ public class SuperStream<T> implements Stream<T> {
|
|||||||
* 返回一个收集器,将元素收集到 List 中。
|
* 返回一个收集器,将元素收集到 List 中。
|
||||||
*
|
*
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
|
* @param <K> Map.Key
|
||||||
|
* @param <V> Map.Value
|
||||||
* @return 收集器
|
* @return 收集器
|
||||||
*/
|
*/
|
||||||
public static <T extends Map.Entry<K, V>, K, V> Collector<
|
public static <T extends Map.Entry<K, V>, K, V> Collector<
|
||||||
|
|||||||
+4
-5
@@ -24,15 +24,14 @@ package com.mingliqiye.utils.uuid.typehandlers.mysql;
|
|||||||
|
|
||||||
import com.mingliqiye.utils.uuid.UUID;
|
import com.mingliqiye.utils.uuid.UUID;
|
||||||
import com.mingliqiye.utils.uuid.typehandlers.UUIDConverter;
|
import com.mingliqiye.utils.uuid.typehandlers.UUIDConverter;
|
||||||
import org.apache.ibatis.type.BaseTypeHandler;
|
|
||||||
import org.apache.ibatis.type.JdbcType;
|
|
||||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
|
||||||
import org.apache.ibatis.type.MappedTypes;
|
|
||||||
|
|
||||||
import java.sql.CallableStatement;
|
import java.sql.CallableStatement;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import org.apache.ibatis.type.BaseTypeHandler;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||||
|
import org.apache.ibatis.type.MappedTypes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UUIDBinaryTypeHandler 类用于处理 UUID 类型与数据库 BINARY 类型之间的转换
|
* UUIDBinaryTypeHandler 类用于处理 UUID 类型与数据库 BINARY 类型之间的转换
|
||||||
|
|||||||
+4
-5
@@ -24,15 +24,14 @@ package com.mingliqiye.utils.uuid.typehandlers.pgsql;
|
|||||||
|
|
||||||
import com.mingliqiye.utils.uuid.UUID;
|
import com.mingliqiye.utils.uuid.UUID;
|
||||||
import com.mingliqiye.utils.uuid.typehandlers.UUIDConverter;
|
import com.mingliqiye.utils.uuid.typehandlers.UUIDConverter;
|
||||||
import org.apache.ibatis.type.BaseTypeHandler;
|
|
||||||
import org.apache.ibatis.type.JdbcType;
|
|
||||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
|
||||||
import org.apache.ibatis.type.MappedTypes;
|
|
||||||
|
|
||||||
import java.sql.CallableStatement;
|
import java.sql.CallableStatement;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import org.apache.ibatis.type.BaseTypeHandler;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||||
|
import org.apache.ibatis.type.MappedTypes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UUIDBinaryTypeHandler 类用于处理 UUID 类型与数据库 BINARY 类型之间的转换
|
* UUIDBinaryTypeHandler 类用于处理 UUID 类型与数据库 BINARY 类型之间的转换
|
||||||
|
|||||||
+1
@@ -22,3 +22,4 @@
|
|||||||
|
|
||||||
com.mingliqiye.utils.springboot.autoconfigure.AutoConfiguration
|
com.mingliqiye.utils.springboot.autoconfigure.AutoConfiguration
|
||||||
com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration
|
com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration
|
||||||
|
com.mingliqiye.utils.springboot.autoconfigure.GsonAutoConfiguration
|
||||||
|
|||||||
Reference in New Issue
Block a user