Browse Source

新增支持 LocalDateTime、LocalDate、LocalTime,对高版本 JDBC 驱动更友好

James 4 years ago
parent
commit
6282b84282
1 changed files with 57 additions and 0 deletions
  1. 57 0
      src/main/java/com/jfinal/json/JFinalJsonKit.java

+ 57 - 0
src/main/java/com/jfinal/json/JFinalJsonKit.java

@@ -21,6 +21,10 @@ import java.lang.reflect.Method;
 import java.sql.Time;
 import java.sql.Time;
 import java.sql.Timestamp;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.temporal.Temporal;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Collections;
@@ -34,6 +38,7 @@ import java.util.Objects;
 import java.util.function.Function;
 import java.util.function.Function;
 import com.jfinal.kit.StrKit;
 import com.jfinal.kit.StrKit;
 import com.jfinal.kit.SyncWriteMap;
 import com.jfinal.kit.SyncWriteMap;
+import com.jfinal.kit.TimeKit;
 import com.jfinal.plugin.activerecord.CPI;
 import com.jfinal.plugin.activerecord.CPI;
 import com.jfinal.plugin.activerecord.Model;
 import com.jfinal.plugin.activerecord.Model;
 import com.jfinal.plugin.activerecord.Record;
 import com.jfinal.plugin.activerecord.Record;
@@ -148,6 +153,18 @@ public class JFinalJsonKit {
 			return new DateToJson();
 			return new DateToJson();
 		}
 		}
 		
 		
+		if (value instanceof Temporal) {
+			if (value instanceof LocalDateTime) {
+				return new LocalDateTimeToJson();
+			}
+			if (value instanceof LocalDate) {
+				return new LocalDateToJson();
+			}
+			if (value instanceof LocalTime) {
+				return new LocalTimeToJson();
+			}
+		}
+		
 		// 集合、Bean 类型,需要检测 depth ---------------------------------
 		// 集合、Bean 类型,需要检测 depth ---------------------------------
 		if (! treatModelAsBean) {
 		if (! treatModelAsBean) {
 			if (value instanceof Model) {
 			if (value instanceof Model) {
@@ -281,6 +298,24 @@ public class JFinalJsonKit {
 		}
 		}
 	}
 	}
 	
 	
+	static class LocalDateTimeToJson implements ToJson<LocalDateTime> {
+		public void toJson(LocalDateTime value, int depth, JsonResult ret) {
+			ret.addLocalDateTime(value);
+		}
+	}
+	
+	static class LocalDateToJson implements ToJson<LocalDate> {
+		public void toJson(LocalDate value, int depth, JsonResult ret) {
+			ret.addLocalDate(value);
+		}
+	}
+	
+	static class LocalTimeToJson implements ToJson<LocalTime> {
+		public void toJson(LocalTime value, int depth, JsonResult ret) {
+			ret.addLocalTime(value);
+		}
+	}
+	
 	static class ModelToJson implements ToJson<Model> {
 	static class ModelToJson implements ToJson<Model> {
 		public void toJson(Model model, int depth, JsonResult ret) {
 		public void toJson(Model model, int depth, JsonResult ret) {
 			if (checkDepth(depth--, ret)) {
 			if (checkDepth(depth--, ret)) {
@@ -702,6 +737,28 @@ public class JFinalJsonKit {
 			}
 			}
 		}
 		}
 		
 		
+		public void addLocalDateTime(LocalDateTime ldt) {
+			if (timestampPattern != null) {
+				sb.append('\"').append(TimeKit.format(ldt, timestampPattern)).append('\"');
+			} else {
+				sb.append(TimeKit.toDate(ldt).getTime());
+			}
+		}
+		
+		public void addLocalDate(LocalDate ld) {
+			// LocalDate 的 pattern 不支持时分秒,暂时写死 pattern
+			// 可通过 JFinalJson.addToJson(LocalDate.class, ...) 定制自己的转换 pattern
+			String dp = "yyyy-MM-dd";
+			sb.append('\"').append(TimeKit.format(ld, dp)).append('\"');
+		}
+		
+		public void addLocalTime(LocalTime lt) {
+			// LocalTime.toString() 的结果与 Time.toString() 格式不同,暂时写死 pattern
+			// 可通过 JFinalJson.addToJson(LocalTime.class, ...) 定制自己的转换 pattern
+			String tp = "HH:mm:ss";
+			sb.append('\"').append(TimeKit.format(lt, tp)).append('\"');
+		}
+		
 		public void addMapKey(Object value) {
 		public void addMapKey(Object value) {
 			escape(String.valueOf(value), sb);
 			escape(String.valueOf(value), sb);
 		}
 		}