diff --git a/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java b/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java index 3dec64565d0879b734786c3bcd89680839306c41..a28dfc73cd8abb895e5c5b3afa291afbd0141e32 100644 --- a/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java +++ b/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java @@ -1,6 +1,7 @@ package com.anwen.mongo.convert; import com.anwen.mongo.cache.global.PropertyCache; +import com.anwen.mongo.toolkit.InstantUtil; import com.anwen.mongo.toolkit.StringUtils; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCursor; @@ -8,6 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -18,11 +20,12 @@ public class Converter { /** * 将FindIterable转换为List>。 + * * @param iterable 待转换的FindIterable对象 - * @return java.util.List> 转换后的List>对象 + * @return java.util.List> 转换后的List>对象 * @author JiaChaoYang * @date 2023/6/29/029 - */ + */ public static List> convertDocumentToMap(FindIterable iterable) { List> resultList = new ArrayList<>(); try (MongoCursor cursor = iterable.iterator()) { @@ -41,24 +44,30 @@ public class Converter { return resultList; } - public static List> convertDocumentToMap(FindIterable iterable,Integer total) { + public static List> convertDocumentToMap(FindIterable iterable, Integer total) { List> resultList = new ArrayList<>(total); - for (Map map : iterable.batchSize(total)) { + for (Map map : iterable.batchSize(total)) { resultList.add(convertKeysToCamelCase(map)); } return resultList; } public static Map convertKeysToCamelCase(Map map) { - if (!PropertyCache.mapUnderscoreToCamelCase){ - return map; - } return map.entrySet().stream() .collect(Collectors.toMap( - entry -> StringUtils.convertToCamelCase(entry.getKey()), - Map.Entry::getValue) - ); + entry -> convertToCamelCaseIfNeeded(entry.getKey()), + entry -> convertValue(entry.getValue()) + )); } + private static String convertToCamelCaseIfNeeded(String key) { + return PropertyCache.mapUnderscoreToCamelCase ? StringUtils.convertToCamelCase(key) : key; + } + private static Object convertValue(Object value) { + if (value instanceof Date) { + return InstantUtil.convertTimestampToLocalDateTime(((Date) value).toInstant()); + } + return value; + } }