Looly 5 年 前
コミット
d4ee12562b

+ 1 - 0
CHANGELOG.md

@@ -8,6 +8,7 @@
 ### 新特性
 * 【core   】     增加ISO8601日期格式(issue#904@Github)
 ### Bug修复
+* 【json   】     修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 4 - 4
hutool-json/src/main/java/cn/hutool/json/JSONObject.java

@@ -282,7 +282,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 		if (CollectionUtil.isEmpty(names)) {
 			return null;
 		}
-		final JSONArray ja = new JSONArray();
+		final JSONArray ja = new JSONArray(this.config);
 		Object value;
 		for (String name : names) {
 			value = this.get(name);
@@ -432,11 +432,11 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 		InternalJSONUtil.testValidity(value);
 		Object object = this.getObj(key);
 		if (object == null) {
-			this.put(key, value instanceof JSONArray ? new JSONArray().set(value) : value);
+			this.put(key, value instanceof JSONArray ? new JSONArray(this.config).set(value) : value);
 		} else if (object instanceof JSONArray) {
 			((JSONArray) object).set(value);
 		} else {
-			this.set(key, new JSONArray().set(object).set(value));
+			this.set(key, new JSONArray(this.config).set(object).set(value));
 		}
 		return this;
 	}
@@ -453,7 +453,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 		InternalJSONUtil.testValidity(value);
 		Object object = this.getObj(key);
 		if (object == null) {
-			this.set(key, new JSONArray().set(value));
+			this.set(key, new JSONArray(this.config).set(value));
 		} else if (object instanceof JSONArray) {
 			this.set(key, ((JSONArray) object).set(value));
 		} else {

+ 13 - 0
hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java

@@ -404,6 +404,19 @@ public class JSONObjectTest {
 		Assert.assertEquals(new Integer(35), bean.getValue2());
 	}
 
+	@Test
+	public void setDateFormatTest(){
+		JSONConfig jsonConfig = JSONConfig.create();
+		jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
+		jsonConfig.setOrder(true);
+
+		JSONObject json = new JSONObject(jsonConfig);
+		json.append("date", DateUtil.parse("2020-06-05 11:16:11"));
+		json.append("bbb", "222");
+		json.append("aaa", "123");
+		Assert.assertEquals("{\"date\":[\"2020-06-05 11:16:11\"],\"bbb\":[\"222\"],\"aaa\":[\"123\"]}", json.toString());
+	}
+
 	public enum TestEnum {
 		TYPE_A, TYPE_B
 	}