mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -133,7 +133,7 @@ public final class InternalJSONUtil {
|
||||
/**
|
||||
* 将Property的键转化为JSON形式<br>
|
||||
* 用于识别类似于:com.luxiaolei.package.hutool这类用点隔开的键<br>
|
||||
* 注意:不允许重复键
|
||||
* 注意:是否允许重复键,取决于JSONObject配置
|
||||
*
|
||||
* @param jsonObject JSONObject
|
||||
* @param key 键
|
||||
@@ -142,18 +142,18 @@ public final class InternalJSONUtil {
|
||||
*/
|
||||
static JSONObject propertyPut(JSONObject jsonObject, Object key, Object value, Filter<MutablePair<String, Object>> filter) {
|
||||
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
||||
int last = path.length - 1;
|
||||
final int last = path.length - 1;
|
||||
JSONObject target = jsonObject;
|
||||
for (int i = 0; i < last; i += 1) {
|
||||
String segment = path[i];
|
||||
final String segment = path[i];
|
||||
JSONObject nextTarget = target.getJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new JSONObject(target.getConfig());
|
||||
target.setOnce(segment, nextTarget, filter);
|
||||
target.set(segment, nextTarget, filter, target.getConfig().isCheckDuplicate());
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.setOnce(path[last], value, filter);
|
||||
target.set(path[last], value, filter, target.getConfig().isCheckDuplicate());
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
@@ -45,9 +45,9 @@ public class JSONConfig implements Serializable {
|
||||
private boolean stripTrailingZeros = true;
|
||||
|
||||
/**
|
||||
* 是否忽略多个相同的key
|
||||
* 是否检查重复key
|
||||
*/
|
||||
private boolean ignoreDuplicateKey = false;
|
||||
private boolean checkDuplicate;
|
||||
|
||||
/**
|
||||
* 创建默认的配置项
|
||||
@@ -242,20 +242,24 @@ public class JSONConfig implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略多个相同的key
|
||||
* @return
|
||||
* 是否检查多个相同的key
|
||||
*
|
||||
* @return 是否检查多个相同的key
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public boolean isIgnoreDuplicateKey() {
|
||||
return ignoreDuplicateKey;
|
||||
public boolean isCheckDuplicate() {
|
||||
return checkDuplicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略多个相同的key
|
||||
* @param ignoreDuplicateKey
|
||||
* @return
|
||||
* 是否检查多个相同的key
|
||||
*
|
||||
* @param checkDuplicate 是否检查多个相同的key
|
||||
* @return this
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public JSONConfig setIgnoreDuplicateKey(boolean ignoreDuplicateKey) {
|
||||
this.ignoreDuplicateKey = ignoreDuplicateKey;
|
||||
public JSONConfig setCheckDuplicate(boolean checkDuplicate) {
|
||||
this.checkDuplicate = checkDuplicate;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@@ -236,7 +236,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
Object value;
|
||||
for (String name : names) {
|
||||
value = ((Map<?, ?>) source).get(name);
|
||||
this.putOnce(name, value);
|
||||
this.set(name, value, null, getConfig().isCheckDuplicate());
|
||||
}
|
||||
} else {
|
||||
for (String name : names) {
|
||||
@@ -392,9 +392,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
// 忽略值模式下如果值为空清除key
|
||||
this.remove(key);
|
||||
} else {
|
||||
/*如果允许多个key,就不抛出异常,使用后面的值覆盖前面的值*/
|
||||
boolean ignoreDuplicateKey = this.config.isIgnoreDuplicateKey();
|
||||
if (checkDuplicate && containsKey(key) && false == ignoreDuplicateKey) {
|
||||
if (checkDuplicate && containsKey(key)) {
|
||||
throw new JSONException("Duplicate key \"{}\"", key);
|
||||
}
|
||||
|
||||
|
@@ -69,7 +69,7 @@ public class JSONParser {
|
||||
throw tokener.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
|
||||
jsonObject.setOnce(key, tokener.nextValue(), filter);
|
||||
jsonObject.set(key, tokener.nextValue(), filter, jsonObject.getConfig().isCheckDuplicate());
|
||||
|
||||
// Pairs are separated by ','.
|
||||
|
||||
|
@@ -88,11 +88,11 @@ public class ObjectMapper {
|
||||
if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Map.Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), filter, false);
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), filter, jsonObject.getConfig().isCheckDuplicate());
|
||||
}
|
||||
} else if (source instanceof Map.Entry) {
|
||||
final Map.Entry entry = (Map.Entry) source;
|
||||
jsonObject.set(Convert.toStr(entry.getKey()), entry.getValue(), filter, false);
|
||||
jsonObject.set(Convert.toStr(entry.getKey()), entry.getValue(), filter, jsonObject.getConfig().isCheckDuplicate());
|
||||
} else if (source instanceof CharSequence) {
|
||||
// 可能为JSON字符串
|
||||
mapFromStr((CharSequence) source, jsonObject, filter);
|
||||
|
@@ -26,7 +26,7 @@ public class JSONUtilTest {
|
||||
*/
|
||||
@Test(expected = JSONException.class)
|
||||
public void parseTest() {
|
||||
JSONArray jsonArray = JSONUtil.parseArray("[{\"a\":\"a\\x]");
|
||||
final JSONArray jsonArray = JSONUtil.parseArray("[{\"a\":\"a\\x]");
|
||||
Console.log(jsonArray);
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class JSONUtilTest {
|
||||
*/
|
||||
@Test(expected = JSONException.class)
|
||||
public void parseNumberTest() {
|
||||
JSONArray json = JSONUtil.parseArray(123L);
|
||||
final JSONArray json = JSONUtil.parseArray(123L);
|
||||
Assert.assertNotNull(json);
|
||||
}
|
||||
|
||||
@@ -44,42 +44,42 @@ public class JSONUtilTest {
|
||||
*/
|
||||
@Test
|
||||
public void parseNumberTest2() {
|
||||
JSONObject json = JSONUtil.parseObj(123L);
|
||||
final JSONObject json = JSONUtil.parseObj(123L);
|
||||
Assert.assertEquals(new JSONObject(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJsonStrTest() {
|
||||
UserA a1 = new UserA();
|
||||
final UserA a1 = new UserA();
|
||||
a1.setA("aaaa");
|
||||
a1.setDate(DateUtil.date());
|
||||
a1.setName("AAAAName");
|
||||
UserA a2 = new UserA();
|
||||
final UserA a2 = new UserA();
|
||||
a2.setA("aaaa222");
|
||||
a2.setDate(DateUtil.date());
|
||||
a2.setName("AAAA222Name");
|
||||
|
||||
ArrayList<UserA> list = CollectionUtil.newArrayList(a1, a2);
|
||||
HashMap<String, Object> map = MapUtil.newHashMap();
|
||||
final ArrayList<UserA> list = CollectionUtil.newArrayList(a1, a2);
|
||||
final HashMap<String, Object> map = MapUtil.newHashMap();
|
||||
map.put("total", 13);
|
||||
map.put("rows", list);
|
||||
|
||||
String str = JSONUtil.toJsonPrettyStr(map);
|
||||
final String str = JSONUtil.toJsonPrettyStr(map);
|
||||
JSONUtil.parse(str);
|
||||
Assert.assertNotNull(str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJsonStrTest2() {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
final Map<String, Object> model = new HashMap<>();
|
||||
model.put("mobile", "17610836523");
|
||||
model.put("type", 1);
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
final Map<String, Object> data = new HashMap<>();
|
||||
data.put("model", model);
|
||||
data.put("model2", model);
|
||||
|
||||
JSONObject jsonObject = JSONUtil.parseObj(data);
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(data);
|
||||
|
||||
Assert.assertTrue(jsonObject.containsKey("model"));
|
||||
Assert.assertEquals(1, jsonObject.getJSONObject("model").getInt("type").intValue());
|
||||
@@ -90,25 +90,25 @@ public class JSONUtilTest {
|
||||
@Test
|
||||
public void toJsonStrTest3() {
|
||||
// 验证某个字段为JSON字符串时转义是否规范
|
||||
JSONObject object = new JSONObject(true);
|
||||
final JSONObject object = new JSONObject(true);
|
||||
object.set("name", "123123");
|
||||
object.set("value", "\\");
|
||||
object.set("value2", "</");
|
||||
|
||||
HashMap<String, String> map = MapUtil.newHashMap();
|
||||
final HashMap<String, String> map = MapUtil.newHashMap();
|
||||
map.put("user", object.toString());
|
||||
|
||||
JSONObject json = JSONUtil.parseObj(map);
|
||||
final JSONObject json = JSONUtil.parseObj(map);
|
||||
Assert.assertEquals("{\"name\":\"123123\",\"value\":\"\\\\\",\"value2\":\"</\"}", json.get("user"));
|
||||
Assert.assertEquals("{\"user\":\"{\\\"name\\\":\\\"123123\\\",\\\"value\\\":\\\"\\\\\\\\\\\",\\\"value2\\\":\\\"</\\\"}\"}", json.toString());
|
||||
|
||||
JSONObject json2 = JSONUtil.parseObj(json.toString());
|
||||
final JSONObject json2 = JSONUtil.parseObj(json.toString());
|
||||
Assert.assertEquals("{\"name\":\"123123\",\"value\":\"\\\\\",\"value2\":\"</\"}", json2.get("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJsonStrFromSortedTest() {
|
||||
SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
|
||||
final SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
@@ -125,20 +125,20 @@ public class JSONUtilTest {
|
||||
*/
|
||||
@Test
|
||||
public void toBeanTest() {
|
||||
String json = "{\"ADT\":[[{\"BookingCode\":[\"N\",\"N\"]}]]}";
|
||||
final String json = "{\"ADT\":[[{\"BookingCode\":[\"N\",\"N\"]}]]}";
|
||||
|
||||
Price price = JSONUtil.toBean(json, Price.class);
|
||||
final Price price = JSONUtil.toBean(json, Price.class);
|
||||
Assert.assertEquals("N", price.getADT().get(0).get(0).getBookingCode().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanTest2() {
|
||||
// 测试JSONObject转为Bean中字符串字段的情况
|
||||
String json = "{\"id\":123,\"name\":\"张三\",\"prop\":{\"gender\":\"男\", \"age\":18}}";
|
||||
UserC user = JSONUtil.toBean(json, UserC.class);
|
||||
final String json = "{\"id\":123,\"name\":\"张三\",\"prop\":{\"gender\":\"男\", \"age\":18}}";
|
||||
final UserC user = JSONUtil.toBean(json, UserC.class);
|
||||
Assert.assertNotNull(user.getProp());
|
||||
String prop = user.getProp();
|
||||
JSONObject propJson = JSONUtil.parseObj(prop);
|
||||
final String prop = user.getProp();
|
||||
final JSONObject propJson = JSONUtil.parseObj(prop);
|
||||
Assert.assertEquals("男", propJson.getStr("gender"));
|
||||
Assert.assertEquals(18, propJson.getInt("age").intValue());
|
||||
// Assert.assertEquals("{\"age\":18,\"gender\":\"男\"}", user.getProp());
|
||||
@@ -146,22 +146,22 @@ public class JSONUtilTest {
|
||||
|
||||
@Test
|
||||
public void getStrTest() {
|
||||
String html = "{\"name\":\"Something must have been changed since you leave\"}";
|
||||
JSONObject jsonObject = JSONUtil.parseObj(html);
|
||||
final String html = "{\"name\":\"Something must have been changed since you leave\"}";
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(html);
|
||||
Assert.assertEquals("Something must have been changed since you leave", jsonObject.getStr("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStrTest2() {
|
||||
String html = "{\"name\":\"Something\\u00a0must have been changed since you leave\"}";
|
||||
JSONObject jsonObject = JSONUtil.parseObj(html);
|
||||
final String html = "{\"name\":\"Something\\u00a0must have been changed since you leave\"}";
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(html);
|
||||
Assert.assertEquals("Something\\u00a0must\\u00a0have\\u00a0been\\u00a0changed\\u00a0since\\u00a0you\\u00a0leave", jsonObject.getStrEscaped("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFromXmlTest() {
|
||||
String s = "<sfzh>640102197312070614</sfzh><sfz>640102197312070614X</sfz><name>aa</name><gender>1</gender>";
|
||||
JSONObject json = JSONUtil.parseFromXml(s);
|
||||
final String s = "<sfzh>640102197312070614</sfzh><sfz>640102197312070614X</sfz><name>aa</name><gender>1</gender>";
|
||||
final JSONObject json = JSONUtil.parseFromXml(s);
|
||||
Assert.assertEquals(640102197312070614L, json.get("sfzh"));
|
||||
Assert.assertEquals("640102197312070614X", json.get("sfz"));
|
||||
Assert.assertEquals("aa", json.get("name"));
|
||||
@@ -170,7 +170,7 @@ public class JSONUtilTest {
|
||||
|
||||
@Test
|
||||
public void doubleTest() {
|
||||
String json = "{\"test\": 12.00}";
|
||||
final String json = "{\"test\": 12.00}";
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(json);
|
||||
//noinspection BigDecimalMethodWithoutRoundingCalled
|
||||
Assert.assertEquals("12.00", jsonObject.getBigDecimal("test").setScale(2).toString());
|
||||
@@ -222,7 +222,7 @@ public class JSONUtilTest {
|
||||
@Test
|
||||
public void parseBigNumberTest(){
|
||||
// 科学计数法使用BigDecimal处理,默认输出非科学计数形式
|
||||
String str = "{\"test\":100000054128897953e4}";
|
||||
final String str = "{\"test\":100000054128897953e4}";
|
||||
Assert.assertEquals("{\"test\":1000000541288979530000}", JSONUtil.parseObj(str).toString());
|
||||
}
|
||||
|
||||
@@ -236,12 +236,18 @@ public class JSONUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuplicateKey(){
|
||||
String str = "{id:123, name:\"张三\", name:\"李四\"}";
|
||||
public void duplicateKeyFalseTest(){
|
||||
final String str = "{id:123, name:\"张三\", name:\"李四\"}";
|
||||
|
||||
JSONObject jsonObject = JSONUtil.parseObj(str, JSONConfig.create().setIgnoreDuplicateKey(true));
|
||||
System.out.println(jsonObject.toString());
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(str, JSONConfig.create().setCheckDuplicate(false));
|
||||
Assert.assertEquals("{\"id\":123,\"name\":\"李四\"}", jsonObject.toString());
|
||||
}
|
||||
|
||||
Assert.assertNotNull(jsonObject);
|
||||
@Test(expected = JSONException.class)
|
||||
public void duplicateKeyTrueTest(){
|
||||
final String str = "{id:123, name:\"张三\", name:\"李四\"}";
|
||||
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(str, JSONConfig.create().setCheckDuplicate(true));
|
||||
Assert.assertEquals("{\"id\":123,\"name\":\"李四\"}", jsonObject.toString());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user