generated from mingliqiye/lib-tem
refactor(import-order): 重新排序和整理导入语句
All checks were successful
Gitea Actions Build / Build (push) Successful in 1m3s
All checks were successful
Gitea Actions Build / Build (push) Successful in 1m3s
在多个文件中调整了导入语句的顺序,并移除了重复的导入语句。 - `AutoConfiguration.java` 中调整了导入语句的顺序。 - `DateTimeToStringConverter.java` 和 `StringToDateTimeConverter.java` 中调整了静态导入的位置。- `JsonStringConverter.java` 中移除了多余的空行。 - `UUIDBinaryTypeHandler.java` 和 `UUIDStringTypeHandler.java` 中调整了导入语句的顺序。 feat(json): 添加新的 GsonJsonApi 类和 JsonApiAutoConfiguration 配置类 - 新增 `GsonJsonApi` 类,实现了 `JsonApi` 接口,提供了 JSON 处理功能。 - 新增 `JsonApiAutoConfiguration` 类,自动配置 `JsonApi` bean,支持 Jackson 和 Gson。 - 更新 `gradle.properties` 文件中的版本号从 3.1.2 到 3.1.3
This commit is contained in:
parent
b1dc8c0ac8
commit
972ed6d338
@ -22,4 +22,4 @@
|
||||
JDKVERSIONS=1.8
|
||||
GROUPSID=com.mingliqiye.utils
|
||||
ARTIFACTID=mingli-utils
|
||||
VERSIONS=3.1.2
|
||||
VERSIONS=3.1.3
|
||||
|
||||
187
src/main/java/com/mingliqiye/utils/json/GsonJsonApi.java
Normal file
187
src/main/java/com/mingliqiye/utils/json/GsonJsonApi.java
Normal file
@ -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;
|
||||
|
||||
import com.mingliqiye.utils.collection.Maps;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@ -34,8 +33,6 @@ import java.util.Map;
|
||||
* JSON处理接口,提供JSON字符串与Java对象之间的相互转换功能
|
||||
*/
|
||||
public interface JsonApi {
|
||||
final Map<String, String> UNICODE_BIND = Maps.of("1", "");
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为指定类型的对象
|
||||
*
|
||||
|
||||
@ -11,7 +11,6 @@ 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;
|
||||
|
||||
|
||||
@ -25,14 +25,13 @@ package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
import com.mingliqiye.utils.collection.ForEach;
|
||||
import com.mingliqiye.utils.time.DateTime;
|
||||
import com.mingliqiye.utils.time.Formatter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@org.springframework.boot.autoconfigure.AutoConfiguration
|
||||
@EnableConfigurationProperties(AutoConfiguration.class)
|
||||
@ComponentScan(
|
||||
|
||||
@ -0,0 +1,50 @@
|
||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.mingliqiye.utils.json.GsonJsonApi;
|
||||
import com.mingliqiye.utils.json.JacksonJsonApi;
|
||||
import com.mingliqiye.utils.json.JsonApi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
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;
|
||||
|
||||
@AutoConfiguration
|
||||
@AutoConfigureAfter(
|
||||
name = {
|
||||
"com.mingliqiye.utils.springboot.autoconfigure.GsonAutoConfiguration",
|
||||
"com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration",
|
||||
}
|
||||
)
|
||||
public class JsonApiAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(
|
||||
"MingliUtils-JsonApiAutoConfiguration"
|
||||
);
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(ObjectMapper.class)
|
||||
public JsonApi jacksonJsonApi(ObjectMapper objectMapper) {
|
||||
log.info(
|
||||
"MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created."
|
||||
);
|
||||
return new JacksonJsonApi(objectMapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(Gson.class)
|
||||
public JsonApi gsonJsonApi(Gson gson) {
|
||||
log.info(
|
||||
"MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created."
|
||||
);
|
||||
return new GsonJsonApi(gson);
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
package com.mingliqiye.utils.springboot.converters;
|
||||
|
||||
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME;
|
||||
|
||||
import com.mingliqiye.utils.time.DateTime;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME;
|
||||
|
||||
/**
|
||||
* spring boot DateTime到字符串转换器
|
||||
*
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package com.mingliqiye.utils.springboot.converters;
|
||||
|
||||
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME_MILLISECOUND7;
|
||||
|
||||
import com.mingliqiye.utils.time.DateTime;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.mingliqiye.utils.time.Formatter.STANDARD_DATETIME_MILLISECOUND7;
|
||||
|
||||
/**
|
||||
* spring boot 字符串到DateTime转换器
|
||||
*
|
||||
|
||||
@ -24,15 +24,14 @@ package com.mingliqiye.utils.uuid.typehandlers.mysql;
|
||||
|
||||
import com.mingliqiye.utils.uuid.UUID;
|
||||
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.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
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 类型之间的转换
|
||||
|
||||
@ -24,15 +24,14 @@ package com.mingliqiye.utils.uuid.typehandlers.pgsql;
|
||||
|
||||
import com.mingliqiye.utils.uuid.UUID;
|
||||
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.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
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 类型之间的转换
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user