This commit is contained in:
Looly
2022-07-22 16:58:52 +08:00
parent f879f3084a
commit b74b7c682c
8 changed files with 261 additions and 327 deletions

View File

@@ -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());
}
}