9 changed files with 232 additions and 106 deletions
-
8app/src/main/java/com/iflytop/profilometer/api/ble/BleRoutes.kt
-
1app/src/main/java/com/iflytop/profilometer/api/ble/BleWebsocketManager.java
-
11app/src/main/java/com/iflytop/profilometer/api/record/RecordApi.java
-
12app/src/main/java/com/iflytop/profilometer/api/record/RecordRoutes.kt
-
66app/src/main/java/com/iflytop/profilometer/api/sync/SyncApi.java
-
31app/src/main/java/com/iflytop/profilometer/api/sync/SyncRoutes.kt
-
2app/src/main/java/com/iflytop/profilometer/common/result/Result.java
-
94app/src/main/java/com/iflytop/profilometer/common/utils/GsonUtil.java
-
113app/src/main/java/com/iflytop/profilometer/dao/SyncTaskDao.java
@ -1,94 +0,0 @@ |
|||||
package com.iflytop.profilometer.common.utils; |
|
||||
|
|
||||
import com.google.gson.*; |
|
||||
import com.google.gson.reflect.TypeToken; |
|
||||
|
|
||||
import java.lang.reflect.Type; |
|
||||
import java.time.LocalDateTime; |
|
||||
import java.time.format.DateTimeFormatter; |
|
||||
import java.util.List; |
|
||||
|
|
||||
public class GsonUtil { |
|
||||
// 定义日期格式 |
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; |
|
||||
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT); |
|
||||
|
|
||||
// 创建 LocalDateTime 的序列化器 |
|
||||
private static final JsonSerializer<LocalDateTime> localDateTimeSerializer = new JsonSerializer<LocalDateTime>() { |
|
||||
@Override |
|
||||
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { |
|
||||
return new JsonPrimitive(src.format(formatter)); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
// 创建 LocalDateTime 的反序列化器 |
|
||||
private static final JsonDeserializer<LocalDateTime> localDateTimeDeserializer = new JsonDeserializer<LocalDateTime>() { |
|
||||
@Override |
|
||||
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
|
||||
throws JsonParseException { |
|
||||
return LocalDateTime.parse(json.getAsString(), formatter); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
// 使用 GsonBuilder 注册 LocalDateTime 的适配器,同时设置 java.util.Date 的格式 |
|
||||
private static final Gson gson = new GsonBuilder() |
|
||||
.registerTypeAdapter(LocalDateTime.class, localDateTimeSerializer) |
|
||||
.registerTypeAdapter(LocalDateTime.class, localDateTimeDeserializer) |
|
||||
.setDateFormat(DATE_FORMAT) |
|
||||
.create(); |
|
||||
|
|
||||
/** |
|
||||
* 对象转 JSON 字符串 |
|
||||
* |
|
||||
* @param object 要转换的对象 |
|
||||
* @return 对象对应的 JSON 字符串,若对象为 null 则返回空字符串 |
|
||||
*/ |
|
||||
public static String toJson(Object object) { |
|
||||
if (object == null) { |
|
||||
return ""; |
|
||||
} |
|
||||
return gson.toJson(object); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* JSON 字符串转对象 |
|
||||
* |
|
||||
* @param json JSON 字符串 |
|
||||
* @param clazz 目标对象的 Class 类型 |
|
||||
* @param <T> 目标对象的类型 |
|
||||
* @return 转换后的对象,转换失败时返回 null |
|
||||
*/ |
|
||||
public static <T> T fromJson(String json, Class<T> clazz) { |
|
||||
if (json == null || json.isEmpty()) { |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
return gson.fromJson(json, clazz); |
|
||||
} catch (JsonSyntaxException e) { |
|
||||
e.printStackTrace(); |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* JSON 字符串转 List 对象 |
|
||||
* |
|
||||
* @param json JSON 字符串 |
|
||||
* @param clazz List 中元素的 Class 类型 |
|
||||
* @param <T> List 中元素的类型 |
|
||||
* @return 转换后的 List 对象,转换失败时返回 null |
|
||||
*/ |
|
||||
public static <T> List<T> fromJsonToList(String json, Class<T> clazz) { |
|
||||
if (json == null || json.isEmpty()) { |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
// 利用 TypeToken 获取泛型类型 |
|
||||
Type type = TypeToken.getParameterized(List.class, clazz).getType(); |
|
||||
return gson.fromJson(json, type); |
|
||||
} catch (JsonSyntaxException e) { |
|
||||
e.printStackTrace(); |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue