mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -29,7 +29,7 @@ public class CustomSerializeTest {
|
||||
final CustomBean customBean = new CustomBean();
|
||||
customBean.name = "testName";
|
||||
|
||||
final JSONObject obj = JSONUtil.createObj().set("customBean", customBean);
|
||||
final JSONObject obj = JSONUtil.ofObj().set("customBean", customBean);
|
||||
Assert.assertEquals("testName", obj.getJSONObject("customBean").getStr("customName"));
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import org.junit.Test;
|
||||
public class IssueI59LW4Test {
|
||||
@Test
|
||||
public void bytesTest(){
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("bytes", new byte[]{1});
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("bytes", new byte[]{1});
|
||||
Assert.assertEquals("{\"bytes\":[1]}", jsonObject.toString());
|
||||
|
||||
final byte[] bytes = jsonObject.getBytes("bytes");
|
||||
@@ -15,7 +15,7 @@ public class IssueI59LW4Test {
|
||||
|
||||
@Test
|
||||
public void bytesInJSONArrayTest(){
|
||||
final JSONArray jsonArray = JSONUtil.createArray().set(new byte[]{1});
|
||||
final JSONArray jsonArray = JSONUtil.ofArray().set(new byte[]{1});
|
||||
Assert.assertEquals("[[1]]", jsonArray.toString());
|
||||
|
||||
final byte[] bytes = jsonArray.getBytes(0);
|
||||
|
@@ -22,12 +22,11 @@ import java.util.Map;
|
||||
* JSONArray单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class JSONArrayTest {
|
||||
|
||||
@Test()
|
||||
public void createJSONArrayFromJSONObjectTest(){
|
||||
public void createJSONArrayFromJSONObjectTest() {
|
||||
// JSONObject实现了Iterable接口,可以转换为JSONArray
|
||||
final JSONObject jsonObject = new JSONObject();
|
||||
|
||||
@@ -41,7 +40,7 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addNullTest(){
|
||||
public void addNullTest() {
|
||||
final List<String> aaa = ListUtil.view("aaa", null);
|
||||
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa,
|
||||
JSONConfig.of().setIgnoreNullValue(false)));
|
||||
@@ -51,7 +50,7 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void addTest() {
|
||||
// 方法1
|
||||
final JSONArray array = JSONUtil.createArray();
|
||||
final JSONArray array = JSONUtil.ofArray();
|
||||
// 方法2
|
||||
// JSONArray array = new JSONArray();
|
||||
array.add("value1");
|
||||
@@ -75,12 +74,12 @@ public class JSONArrayTest {
|
||||
Assert.assertFalse(jsonArray.getJSONObject(1).containsKey("result"));
|
||||
|
||||
// 不忽略null,则null的键值对被保留
|
||||
jsonArray = JSONUtil.parseArray(jsonStr, false);
|
||||
jsonArray = JSONUtil.parseArray(jsonStr, JSONConfig.of().setIgnoreNullValue(false));
|
||||
Assert.assertTrue(jsonArray.getJSONObject(1).containsKey("result"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFileTest() {
|
||||
public void readJSONArrayFromFileTest() {
|
||||
final JSONArray array = JSONUtil.readJSONArray(FileUtil.file("exam_test.json"), CharsetUtil.UTF_8);
|
||||
|
||||
final JSONObject obj0 = array.getJSONObject(0);
|
||||
@@ -175,11 +174,12 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test(expected = ConvertException.class)
|
||||
public void toListWithErrorTest(){
|
||||
public void toListWithErrorTest() {
|
||||
final String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
|
||||
final JSONArray ja = JSONUtil.parseArray(json);
|
||||
|
||||
ja.toBean(new TypeReference<List<List<KeyBean>>>() {});
|
||||
ja.toBean(new TypeReference<List<List<KeyBean>>>() {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -209,7 +209,7 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByPathTest(){
|
||||
public void getByPathTest() {
|
||||
final String jsonStr = "[{\"id\": \"1\",\"name\": \"a\"},{\"id\": \"2\",\"name\": \"b\"}]";
|
||||
final JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
|
||||
Assert.assertEquals("b", jsonArray.getByPath("[1].name"));
|
||||
@@ -217,7 +217,7 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putToIndexTest(){
|
||||
public void putToIndexTest() {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.set(3, "test");
|
||||
// 默认忽略null值,因此空位无值,只有一个值
|
||||
@@ -231,7 +231,7 @@ public class JSONArrayTest {
|
||||
|
||||
// https://github.com/dromara/hutool/issues/1858
|
||||
@Test
|
||||
public void putTest2(){
|
||||
public void putTest2() {
|
||||
final JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(0, 1);
|
||||
Assert.assertEquals(1, jsonArray.size());
|
||||
@@ -253,8 +253,8 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterIncludeTest(){
|
||||
final JSONArray json1 = JSONUtil.createArray()
|
||||
public void filterIncludeTest() {
|
||||
final JSONArray json1 = JSONUtil.ofArray()
|
||||
.set("value1")
|
||||
.set("value2")
|
||||
.set("value3")
|
||||
@@ -265,8 +265,8 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterExcludeTest(){
|
||||
final JSONArray json1 = JSONUtil.createArray()
|
||||
public void filterExcludeTest() {
|
||||
final JSONArray json1 = JSONUtil.ofArray()
|
||||
.set("value1")
|
||||
.set("value2")
|
||||
.set("value3")
|
||||
@@ -277,8 +277,8 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putNullTest(){
|
||||
final JSONArray array = JSONUtil.createArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
public void putNullTest() {
|
||||
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
array.set(null);
|
||||
|
||||
Assert.assertEquals("[null]", array.toString());
|
||||
@@ -299,7 +299,7 @@ public class JSONArrayTest {
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> {
|
||||
final JSONObject o = new JSONObject(mutable.get());
|
||||
if("111".equals(o.getStr("id"))){
|
||||
if ("111".equals(o.getStr("id"))) {
|
||||
o.set("name", "test1_edit");
|
||||
}
|
||||
mutable.set(o);
|
||||
|
@@ -35,22 +35,22 @@ public class JSONNullTest {
|
||||
@Test
|
||||
public void setNullTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.createObj().set("key1", null).toString();
|
||||
String json1 = JSONUtil.ofObj().set("key1", null).toString();
|
||||
Assert.assertEquals("{}", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.createObj(JSONConfig.of().setIgnoreNullValue(false)).set("key1", null).toString();
|
||||
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).set("key1", null).toString();
|
||||
Assert.assertEquals("{\"key1\":null}", json1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNullOfJSONArrayTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.createArray().set(null).toString();
|
||||
String json1 = JSONUtil.ofArray().set(null).toString();
|
||||
Assert.assertEquals("[]", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.createArray(JSONConfig.of().setIgnoreNullValue(false)).set(null).toString();
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).set(null).toString();
|
||||
Assert.assertEquals("[null]", json1);
|
||||
}
|
||||
}
|
||||
|
@@ -66,7 +66,7 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
public void toStringTest3() {
|
||||
final JSONObject json = Objects.requireNonNull(JSONUtil.createObj()//
|
||||
final JSONObject json = Objects.requireNonNull(JSONUtil.ofObj()//
|
||||
.set("dateTime", DateUtil.parse("2019-05-02 22:12:01")))//
|
||||
.setDateFormat(DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("{\"dateTime\":\"2019-05-02\"}", json.toString());
|
||||
@@ -74,24 +74,24 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toStringWithDateTest() {
|
||||
JSONObject json = JSONUtil.createObj().set("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
JSONObject json = JSONUtil.ofObj().set("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
assert json != null;
|
||||
Assert.assertEquals("{\"date\":1557314301000}", json.toString());
|
||||
|
||||
json = Objects.requireNonNull(JSONUtil.createObj().set("date", DateUtil.parse("2019-05-08 19:18:21"))).setDateFormat(DatePattern.NORM_DATE_PATTERN);
|
||||
json = Objects.requireNonNull(JSONUtil.ofObj().set("date", DateUtil.parse("2019-05-08 19:18:21"))).setDateFormat(DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("{\"date\":\"2019-05-08\"}", json.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void putAllTest() {
|
||||
final JSONObject json1 = JSONUtil.createObj()
|
||||
final JSONObject json1 = JSONUtil.ofObj()
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
.set("d", true);
|
||||
|
||||
final JSONObject json2 = JSONUtil.createObj()
|
||||
final JSONObject json2 = JSONUtil.ofObj()
|
||||
.set("a", "value21")
|
||||
.set("b", "value22");
|
||||
|
||||
@@ -185,12 +185,12 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toBeanTest() {
|
||||
final JSONObject subJson = JSONUtil.createObj().set("value1", "strValue1").set("value2", "234");
|
||||
final JSONObject json = JSONUtil.createObj(JSONConfig.of().setIgnoreError(true)).set("strValue", "strTest").set("intValue", 123)
|
||||
final JSONObject subJson = JSONUtil.ofObj().set("value1", "strValue1").set("value2", "234");
|
||||
final JSONObject json = JSONUtil.ofObj(JSONConfig.of().setIgnoreError(true)).set("strValue", "strTest").set("intValue", 123)
|
||||
// 测试空字符串转对象
|
||||
.set("doubleValue", "")
|
||||
.set("beanValue", subJson)
|
||||
.set("list", JSONUtil.createArray().set("a").set("b")).set("testEnum", "TYPE_A");
|
||||
.set("list", JSONUtil.ofArray().set("a").set("b")).set("testEnum", "TYPE_A");
|
||||
|
||||
final TestBean bean = json.toBean(TestBean.class);
|
||||
Assert.assertEquals("a", bean.getList().get(0));
|
||||
@@ -205,12 +205,12 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toBeanNullStrTest() {
|
||||
final JSONObject json = JSONUtil.createObj(JSONConfig.of().setIgnoreError(true))//
|
||||
final JSONObject json = JSONUtil.ofObj(JSONConfig.of().setIgnoreError(true))//
|
||||
.set("strValue", "null")//
|
||||
.set("intValue", 123)//
|
||||
// 子对象对应"null"字符串,如果忽略错误,跳过,否则抛出转换异常
|
||||
.set("beanValue", "null")//
|
||||
.set("list", JSONUtil.createArray().set("a").set("b"));
|
||||
.set("list", JSONUtil.ofArray().set("a").set("b"));
|
||||
|
||||
final TestBean bean = json.toBean(TestBean.class);
|
||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||
@@ -272,10 +272,10 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
public void toBeanTest6() {
|
||||
final JSONObject json = JSONUtil.createObj()
|
||||
final JSONObject json = JSONUtil.ofObj()
|
||||
.set("targetUrl", "http://test.com")
|
||||
.set("success", "true")
|
||||
.set("result", JSONUtil.createObj()
|
||||
.set("result", JSONUtil.ofObj()
|
||||
.set("token", "tokenTest")
|
||||
.set("userId", "测试用户1"));
|
||||
|
||||
@@ -334,7 +334,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void parseBeanTest3() {
|
||||
final JSONObject json = JSONUtil.createObj()
|
||||
final JSONObject json = JSONUtil.ofObj()
|
||||
.set("code", 22)
|
||||
.set("data", "{\"jobId\": \"abc\", \"videoUrl\": \"http://a.com/a.mp4\"}");
|
||||
|
||||
@@ -375,7 +375,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void beanTransTest3() {
|
||||
final JSONObject userAJson = JSONUtil.createObj()
|
||||
final JSONObject userAJson = JSONUtil.ofObj()
|
||||
.set("a", "AValue")
|
||||
.set("name", "nameValue")
|
||||
.set("date", "08:00:00");
|
||||
@@ -430,7 +430,7 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals("张三", jsonObject.getStr("name"));
|
||||
Assert.assertEquals(new Integer(35), jsonObject.getInt("age"));
|
||||
|
||||
final JSONObject json = JSONUtil.createObj()
|
||||
final JSONObject json = JSONUtil.ofObj()
|
||||
.set("name", "张三")
|
||||
.set("age", 35);
|
||||
final BeanWithAlias bean = JSONUtil.toBean(Objects.requireNonNull(json).toString(), BeanWithAlias.class);
|
||||
@@ -509,7 +509,7 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void getTimestampTest() {
|
||||
final String timeStr = "1970-01-01 00:00:00";
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("time", timeStr);
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("time", timeStr);
|
||||
final Timestamp time = jsonObject.get("time", Timestamp.class);
|
||||
Assert.assertEquals("1970-01-01 00:00:00.0", time.toString());
|
||||
}
|
||||
@@ -616,7 +616,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void appendTest() {
|
||||
final JSONObject jsonObject = JSONUtil.createObj().append("key1", "value1");
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().append("key1", "value1");
|
||||
Assert.assertEquals("{\"key1\":\"value1\"}", jsonObject.toString());
|
||||
|
||||
jsonObject.append("key1", "value2");
|
||||
@@ -649,7 +649,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void filterIncludeTest() {
|
||||
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
@@ -661,7 +661,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void filterExcludeTest() {
|
||||
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
@@ -673,7 +673,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void editTest() {
|
||||
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
@@ -693,7 +693,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toUnderLineCaseTest() {
|
||||
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("aKey", "value1")
|
||||
.set("bJob", "value2")
|
||||
.set("cGood", "value3")
|
||||
@@ -708,7 +708,7 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void nullToEmptyTest() {
|
||||
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of().setIgnoreNullValue(false))
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false))
|
||||
.set("a", null)
|
||||
.set("b", "value2");
|
||||
|
||||
|
@@ -177,7 +177,7 @@ public class JSONUtilTest {
|
||||
|
||||
@Test
|
||||
public void customValueTest() {
|
||||
final JSONObject jsonObject = JSONUtil.createObj()
|
||||
final JSONObject jsonObject = JSONUtil.ofObj()
|
||||
.set("test2", (JSONString) () -> NumberUtil.format("#.0", 12.00D));
|
||||
|
||||
Assert.assertEquals("{\"test2\":12.0}", jsonObject.toString());
|
||||
@@ -186,12 +186,12 @@ public class JSONUtilTest {
|
||||
@Test
|
||||
public void setStripTrailingZerosTest() {
|
||||
// 默认去除多余的0
|
||||
final JSONObject jsonObjectDefault = JSONUtil.createObj()
|
||||
final JSONObject jsonObjectDefault = JSONUtil.ofObj()
|
||||
.set("test2", 12.00D);
|
||||
Assert.assertEquals("{\"test2\":12}", jsonObjectDefault.toString());
|
||||
|
||||
// 不去除多余的0
|
||||
final JSONObject jsonObject = JSONUtil.createObj(JSONConfig.of().setStripTrailingZeros(false))
|
||||
final JSONObject jsonObject = JSONUtil.ofObj(JSONConfig.of().setStripTrailingZeros(false))
|
||||
.set("test2", 12.00D);
|
||||
Assert.assertEquals("{\"test2\":12.0}", jsonObject.toString());
|
||||
|
||||
@@ -214,7 +214,7 @@ public class JSONUtilTest {
|
||||
public void sqlExceptionTest(){
|
||||
//https://github.com/dromara/hutool/issues/1399
|
||||
// SQLException实现了Iterable接口,默认是遍历之,会栈溢出,修正后只返回string
|
||||
final JSONObject set = JSONUtil.createObj().set("test", new SQLException("test"));
|
||||
final JSONObject set = JSONUtil.ofObj().set("test", new SQLException("test"));
|
||||
Assert.assertEquals("{\"test\":\"java.sql.SQLException: test\"}", set.toString());
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ public class JSONUtilTest {
|
||||
|
||||
@Test
|
||||
public void toXmlTest(){
|
||||
final JSONObject obj = JSONUtil.createObj();
|
||||
final JSONObject obj = JSONUtil.ofObj();
|
||||
obj.set("key1", "v1")
|
||||
.set("key2", ListUtil.view("a", "b", "c"));
|
||||
final String xmlStr = JSONUtil.toXmlStr(obj);
|
||||
|
@@ -11,7 +11,7 @@ public class XMLTest {
|
||||
|
||||
@Test
|
||||
public void toXmlTest(){
|
||||
final JSONObject put = JSONUtil.createObj()
|
||||
final JSONObject put = JSONUtil.ofObj()
|
||||
.set("aaa", "你好")
|
||||
.set("键2", "test");
|
||||
final String s = JSONUtil.toXmlStr(put);
|
||||
@@ -31,7 +31,7 @@ public class XMLTest {
|
||||
|
||||
@Test
|
||||
public void xmlContentTest(){
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("content","123456");
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("content","123456");
|
||||
|
||||
String xml = JSONXMLUtil.toXml(jsonObject);
|
||||
Assert.assertEquals("123456", xml);
|
||||
|
Reference in New Issue
Block a user