Browse Source

重构 MixedJson

James 5 years ago
parent
commit
fea32701dd

+ 64 - 0
src/main/java/com/jfinal/json/MixedJson.java

@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2011-2020, James Zhan 詹波 (jfinal@126.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.jfinal.json;
+
+/**
+ * JFinalJson 与 FastJson 混合做 json 转换
+ * toJson 用 JFinalJson,parse 用 FastJson
+ * 
+ * 注意:
+ * 1:需要添加 fastjson 相关 jar 包
+ * 2:parse 方法转对象依赖于 setter 方法
+ */
+public class MixedJson extends Json {
+	
+	private JFinalJson jFinalJson;
+	private FastJson fastJson;
+	
+	public String toJson(Object object) {
+		return getJFinalJson().toJson(object);
+	}
+	
+	public <T> T parse(String jsonString, Class<T> type) {
+		return getFastJson().parse(jsonString, type);
+	}
+	
+	private JFinalJson getJFinalJson() {
+		if (jFinalJson == null) {
+			jFinalJson = JFinalJson.getJson();
+		}
+		return jFinalJson;
+	}
+	
+	private FastJson getFastJson() {
+		if (fastJson == null) {
+			fastJson = FastJson.getJson();
+		}
+		return fastJson;
+	}
+	
+	public Json setDatePattern(String datePattern) {
+		getJFinalJson().setDatePattern(datePattern);
+		getFastJson().setDatePattern(datePattern);
+		return this;
+	}
+	
+	public String getDatePattern() {
+		return getJFinalJson().getDatePattern();
+	}
+}
+

+ 22 - 26
src/main/java/com/jfinal/json/MixedJsonFactory.java

@@ -1,18 +1,30 @@
+/**
+ * Copyright (c) 2011-2020, James Zhan 詹波 (jfinal@126.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.jfinal.json;
 
-import com.jfinal.json.FastJson;
 import com.jfinal.json.IJsonFactory;
-import com.jfinal.json.JFinalJson;
 import com.jfinal.json.Json;
 
 /**
- * JFinalJson 与 FastJson 混合做 json 转换
- * toJson 用 JFinalJson,parse 用 FastJson
+ * IJsonFactory 的 jfinal + fastjson 组合实现
  * 
- * 注意:
- * 1:需要添加 fastjson 相关 jar 包
- * 2:parse 方法转对象依赖于 setter 方法
- * 3:MixedJson 内部使用了 static 共享变量,在使用时不要改变其内部属性值,以免影响其它线程
+ * 1:toJson 用 JFinalJson,parse 用 FastJson
+ * 2:需要添加 fastjson 相关 jar 包
+ * 3:parse 方法转对象依赖于 setter 方法
  */
 public class MixedJsonFactory implements IJsonFactory {
 	
@@ -21,24 +33,8 @@ public class MixedJsonFactory implements IJsonFactory {
 	public static MixedJsonFactory me() {
 		return me;
 	}
-
-	private static MixedJson mixedJson =  new MixedJson();
-
+	
 	public Json getJson() {
-		return mixedJson;
-	}
-
-	private static class MixedJson extends Json {
-
-		private static JFinalJson jFinalJson = JFinalJson.getJson();
-		private static FastJson fastJson = FastJson.getJson();
-
-		public String toJson(Object object) {
-			return jFinalJson.toJson(object);
-		}
-
-		public <T> T parse(String jsonString, Class<T> type) {
-			return fastJson.parse(jsonString, type);
-		}
+		return new MixedJson();
 	}
 }