!720 JSONConfig增加允许重复key配置,解决不规整json序列化的问题。

Merge pull request !720 from hellozrh/v5-dev
This commit is contained in:
Looly 2022-07-22 08:09:33 +00:00 committed by Gitee
commit f879f3084a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 110 additions and 1 deletions

View File

@ -635,6 +635,57 @@ public class DateUtil extends CalendarUtil {
return CalendarUtil.formatChineseDate(CalendarUtil.calendar(date), withTime);
}
/**
* 返回可读的时间间隔如1天20小时3分48秒
* @param milliSecond 毫秒
* @author zrh 455741807@qq.com
* @return
*/
public static String formatDuring(long milliSecond) {
final int i1000=1000;
if (milliSecond <= 0) {
return "小于1毫秒";
}
if (milliSecond < i1000) {
return milliSecond + "毫秒";
}
String result = "";
long days = milliSecond / (1000 * 60 * 60 * 24);
long hours = (milliSecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (milliSecond % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (milliSecond % (1000 * 60)) / 1000;
if (days > 0) {
result += days + "";
}
if (hours > 0) {
result += hours + "小时";
}
if (minutes > 0) {
result += minutes + "";
}
if (seconds >= 0) {
result += seconds + "";
}
return result;
}
/**
* 返回两个日期之间间隔时间
* @param begin 开始日期
* @param end 结束日期
* @author zrh 455741807@qq.com
* @return
*/
public static String formatDuring(Date begin, Date end) {
if(begin == null || end == null) {
return "";
}
return formatDuring(end.getTime()-begin.getTime());
}
// ------------------------------------ Format end ----------------------------------------------
// ------------------------------------ Parse start ----------------------------------------------

View File

@ -97,6 +97,29 @@ public class DateUtilTest {
Assert.assertEquals(date, parse);
}
@Test
public void formatDuring() {
long d = 56123456;
String s = DateUtil.formatDuring(d);
Assert.assertEquals("15小时35分23秒", s);
}
@Test
public void testFormatDuring() {
Calendar c = Calendar.getInstance();
Date s = c.getTime();
c.add(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.MINUTE, 2);
c.add(Calendar.SECOND, 3);
Date e = c.getTime();
String str = DateUtil.formatDuring(s, e);
Assert.assertEquals("1天2分3秒", str);
}
@Test
public void beginAndEndTest() {
String dateStr = "2017-03-01 00:33:23";

View File

@ -44,6 +44,11 @@ public class JSONConfig implements Serializable {
*/
private boolean stripTrailingZeros = true;
/**
* 是否忽略多个相同的key
*/
private boolean ignoreDuplicateKey = false;
/**
* 创建默认的配置项
*
@ -235,4 +240,22 @@ public class JSONConfig implements Serializable {
this.stripTrailingZeros = stripTrailingZeros;
return this;
}
/**
* 是否忽略多个相同的key
* @return
*/
public boolean isIgnoreDuplicateKey() {
return ignoreDuplicateKey;
}
/**
* 是否忽略多个相同的key
* @param ignoreDuplicateKey
* @return
*/
public JSONConfig setIgnoreDuplicateKey(boolean ignoreDuplicateKey) {
this.ignoreDuplicateKey = ignoreDuplicateKey;
return this;
}
}

View File

@ -392,7 +392,9 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
// 忽略值模式下如果值为空清除key
this.remove(key);
} else {
if (checkDuplicate && containsKey(key)) {
/*如果允许多个key就不抛出异常使用后面的值覆盖前面的值*/
boolean ignoreDuplicateKey = this.config.isIgnoreDuplicateKey();
if (checkDuplicate && containsKey(key) && false == ignoreDuplicateKey) {
throw new JSONException("Duplicate key \"{}\"", key);
}

View File

@ -234,4 +234,14 @@ public class JSONUtilTest {
final String xmlStr = JSONUtil.toXmlStr(obj);
Assert.assertEquals("<key1>v1</key1><key2>a</key2><key2>b</key2><key2>c</key2>", xmlStr);
}
@Test
public void testDuplicateKey(){
String str = "{id:123, name:\"张三\", name:\"李四\"}";
JSONObject jsonObject = JSONUtil.parseObj(str, JSONConfig.create().setIgnoreDuplicateKey(true));
System.out.println(jsonObject.toString());
Assert.assertNotNull(jsonObject);
}
}