mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug and add method
This commit is contained in:
@@ -206,8 +206,13 @@ final class InternalJSONUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认情况下是否忽略null值的策略选择<br>
|
||||
* JavaBean默认忽略null值,其它对象不忽略
|
||||
* 默认情况下是否忽略null值的策略选择,以下对象不忽略null值,其它对象忽略:
|
||||
*
|
||||
* <pre>
|
||||
* 1. CharSequence
|
||||
* 2. JSONTokener
|
||||
* 3. Map
|
||||
* </pre>
|
||||
*
|
||||
* @param obj 需要检查的对象
|
||||
* @return 是否忽略null值
|
||||
@@ -234,13 +239,13 @@ final class InternalJSONUtil {
|
||||
|
||||
//默认使用时间戳
|
||||
long timeMillis;
|
||||
if(dateObj instanceof TemporalAccessor){
|
||||
timeMillis = DateUtil.toInstant((TemporalAccessor)dateObj).toEpochMilli();
|
||||
} else if(dateObj instanceof Date){
|
||||
if (dateObj instanceof TemporalAccessor) {
|
||||
timeMillis = DateUtil.toInstant((TemporalAccessor) dateObj).toEpochMilli();
|
||||
} else if (dateObj instanceof Date) {
|
||||
timeMillis = ((Date) dateObj).getTime();
|
||||
} else if(dateObj instanceof Calendar){
|
||||
} else if (dateObj instanceof Calendar) {
|
||||
timeMillis = ((Calendar) dateObj).getTimeInMillis();
|
||||
} else{
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported Date type: " + dateObj.getClass());
|
||||
}
|
||||
return String.valueOf(timeMillis);
|
||||
|
@@ -20,7 +20,7 @@ import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.RandomAccess;
|
||||
|
||||
import static cn.hutool.json.JSONConverter.*;
|
||||
import static cn.hutool.json.JSONConverter.jsonConvert;
|
||||
|
||||
/**
|
||||
* JSON数组<br>
|
||||
|
@@ -124,7 +124,8 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
* <li>value为Map,将键值对加入JSON对象</li>
|
||||
* <li>value为JSON字符串(CharSequence),使用JSONTokener解析</li>
|
||||
* <li>value为JSONTokener,直接解析</li>
|
||||
* <li>value为普通JavaBean,如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||
* <li>value为普通JavaBean,如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。
|
||||
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
|
@@ -45,7 +45,7 @@ public class JSONTokener {
|
||||
/**
|
||||
* JSON配置
|
||||
*/
|
||||
private JSONConfig config;
|
||||
private final JSONConfig config;
|
||||
|
||||
// ------------------------------------------------------------------------------------ Constructor start
|
||||
|
||||
@@ -63,6 +63,7 @@ public class JSONTokener {
|
||||
this.index = 0;
|
||||
this.character = 1;
|
||||
this.line = 1;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -45,6 +45,17 @@ public class JSONArrayTest {
|
||||
Assert.assertEquals(array.get(0), "value1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNullTest() {
|
||||
String jsonStr = "[{\"grep\":\"4.8\",\"result\":\"右\"},{\"grep\":\"4.8\",\"result\":null}]";
|
||||
JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
|
||||
Assert.assertFalse(jsonArray.getJSONObject(1).containsKey("result"));
|
||||
|
||||
// 不忽略null,则null的键值对被保留
|
||||
jsonArray = new JSONArray(jsonStr, false);
|
||||
Assert.assertTrue(jsonArray.getJSONObject(1).containsKey("result"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFileTest() {
|
||||
JSONArray array = JSONUtil.readJSONArray(FileUtil.file("exam_test.json"), CharsetUtil.CHARSET_UTF_8);
|
||||
|
@@ -186,11 +186,11 @@ public class JSONObjectTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanTest3() {
|
||||
public void toBeanWithNullTest() {
|
||||
String jsonStr = "{'data':{'userName':'ak','password': null}}";
|
||||
Console.log(JSONUtil.parseObj(jsonStr));
|
||||
UserWithMap user = JSONUtil.toBean(JSONUtil.parseObj(jsonStr), UserWithMap.class);
|
||||
// Bean默认忽略null
|
||||
Assert.assertFalse(user.getData().containsKey("password"));
|
||||
Assert.assertTrue(user.getData().containsKey("password"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user