fix json bug

This commit is contained in:
Looly 2020-06-05 11:23:34 +08:00
parent 9b2fc8aecf
commit d4ee12562b
3 changed files with 18 additions and 4 deletions

View File

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

View File

@ -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 {

View File

@ -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
}