This commit is contained in:
Looly
2024-09-24 03:52:10 +08:00
parent 1b92f58bae
commit d8a7b9b440
23 changed files with 477 additions and 134 deletions

View File

@@ -22,14 +22,25 @@ import org.junit.jupiter.api.Test;
import java.util.List;
public class Issue2377Test {
@Test
void toListTest() {
final String jsonStr = "[1,[10,11], \"报表.xlsx\"]";
final JSONArray array = JSONUtil.parseArray(jsonStr);
final List<Object> paramList = JSONUtil.toList(array, Object.class);
Assertions.assertEquals(JSONArray.class, paramList.get(1).getClass());
}
@Test
public void bytesTest() {
final Object[] paramArray = new Object[]{1, new byte[]{10, 11}, "报表.xlsx"};
final String paramsStr = JSONUtil.toJsonStr(paramArray);
Assertions.assertEquals("[1,[10,11],\"报表.xlsx\"]", paramsStr);
final List<Object> paramList = JSONUtil.toList(paramsStr, Object.class);
final JSONArray array = JSONUtil.parseArray(paramsStr);
final List<Object> paramList = JSONUtil.toList(array, Object.class);
Assertions.assertEquals(JSONArray.class, paramList.get(1).getClass());
final String paramBytesStr = JSONUtil.toJsonStr(paramList.get(1));
Assertions.assertEquals("[10,11]", paramBytesStr);

View File

@@ -24,6 +24,8 @@ public class Issue3051Test {
@Test
public void parseTest() {
// 空Bean按照Bean对待转为空对象
// 逻辑见BeanTypeAdapter
final JSONObject jsonObject = JSONUtil.parseObj(new EmptyBean(),
JSONConfig.of().setIgnoreError(true));
@@ -32,10 +34,8 @@ public class Issue3051Test {
@Test
public void parseTest2() {
Assertions.assertThrows(JSONException.class, ()->{
final JSONObject jsonObject = JSONUtil.parseObj(new EmptyBean());
Assertions.assertEquals("{}", jsonObject.toString());
});
final JSONObject jsonObject = JSONUtil.parseObj(new EmptyBean());
Assertions.assertEquals("{}", jsonObject.toString());
}
@Data

View File

@@ -17,7 +17,9 @@
package org.dromara.hutool.json;
import lombok.Data;
import org.dromara.hutool.core.xml.XmlUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.TypeAdapterManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -33,8 +35,21 @@ public class Issue3139Test {
" </c>\n" +
"</r>";
final JSONObject jsonObject = XmlUtil.xmlToBean(XmlUtil.parseXml(xml).getDocumentElement(), JSONObject.class);
final R bean = jsonObject.toBean(R.class);
final JSONObject jsonObject = JSONUtil.parseObj(xml);
Assertions.assertEquals("{\"r\":{\"c\":{\"s\":1,\"p\":\"str\"}}}", jsonObject.toString());
final JSONObject r = jsonObject.getJSONObject("r");
Assertions.assertEquals("{\"c\":{\"s\":1,\"p\":\"str\"}}", r.toString());
// R中的c为List但是JSON中为JSONObject默认会遍历键值对此处需要自定义序列化
TypeAdapterManager.getInstance().register(R.class, (JSONDeserializer<R>) (json, deserializeType) -> {
final R r1 = new R();
// c作为一个独立对象放入List中
r1.setC(ListUtil.of((C) json.asJSONObject().get("c", C.class)));
return r1;
});
final R bean = r.toBean(R.class);
Assertions.assertNotNull(bean);
final List<C> c = bean.getC();

View File

@@ -20,13 +20,25 @@ import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class Issue3649Test {
@Test
void toEmptyBeanTest() {
//issue#3649对于空对象转目标对象直接实例化一个空对象
// 逻辑见BeanTypeAdapter
final Object bean = JSONUtil.toBean("{}", JSONConfig.of().setIgnoreError(false), EmptyBean.class);
Assertions.assertEquals(new EmptyBean(), bean);
}
@Test
void toEmptyListTest() {
final List<?> bean = JSONUtil.toBean("[]", JSONConfig.of().setIgnoreError(false), List.class);
Assertions.assertNotNull(bean);
Assertions.assertTrue(bean.isEmpty());
}
@Data
public static class EmptyBean {}
public static class EmptyBean {
}
}

View File

@@ -172,7 +172,11 @@ public class JSONArrayTest {
final JSONArray array = JSONUtil.parseArray(jsonStr);
//noinspection SuspiciousToArrayCall
final Exam[] list = array.toArray(new Exam[0]);
Exam[] list = array.toArray(new Exam[0]);
Assertions.assertNotEquals(0, list.length);
Assertions.assertSame(Exam.class, list[0].getClass());
list = (Exam[]) array.toArray(Exam[].class);
Assertions.assertNotEquals(0, list.length);
Assertions.assertSame(Exam.class, list[0].getClass());
}
@@ -233,8 +237,8 @@ public class JSONArrayTest {
public void getByPathTest() {
final String jsonStr = "[{\"id\": \"1\",\"name\": \"a\"},{\"id\": \"2\",\"name\": \"b\"}]";
final JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
assertEquals("b", jsonArray.getByPath("[1].name"));
assertEquals("b", JSONUtil.getByPath(jsonArray, "[1].name"));
assertEquals("b", jsonArray.getByPath("[1].name", Object.class));
assertEquals("b", JSONUtil.getObjByPath(jsonArray, "[1].name"));
}
@Test
@@ -256,7 +260,7 @@ public class JSONArrayTest {
final JSONArray jsonArray = new JSONArray();
jsonArray.setValue(0, 1);
assertEquals(1, jsonArray.size());
assertEquals(1, jsonArray.get(0));
assertEquals(1, jsonArray.getObj(0));
}
private static Map<String, String> buildMap(final String id, final String parentId, final String name) {

View File

@@ -619,7 +619,7 @@ public class JSONObjectTest {
@Test
public void createJSONObjectTest() {
Assertions.assertThrows(ClassCastException.class, ()->{
Assertions.assertThrows(JSONException.class, ()->{
// 集合类不支持转为JSONObject
JSONUtil.parseObj(new JSONArray(), JSONConfig.of());
});

View File

@@ -30,16 +30,16 @@ public class JSONPathTest {
@Test
public void getByPathTest() {
final String json = "[{\"id\":\"1\",\"name\":\"xingming\"},{\"id\":\"2\",\"name\":\"mingzi\"}]";
Object value = JSONUtil.parseArray(json).getByPath("[0].name");
Object value = JSONUtil.parseArray(json).getByPath("[0].name", Object.class);
Assertions.assertEquals("xingming", value);
value = JSONUtil.parseArray(json).getByPath("[1].name");
value = JSONUtil.parseArray(json).getByPath("[1].name", Object.class);
Assertions.assertEquals("mingzi", value);
}
@Test
public void getByPathTest2(){
final String str = "{'accountId':111}";
final JSON json = (JSON) JSONUtil.parse(str);
final JSON json = JSONUtil.parse(str);
final Long accountId = JSONUtil.getByPath(json, "$.accountId", 0L);
Assertions.assertEquals(111L, accountId.longValue());
}

View File

@@ -102,20 +102,25 @@ public class JSONUtilTest {
*/
@Test
public void parseNumberToJSONArrayTest() {
assertThrows(ClassCastException.class, () -> {
assertThrows(JSONException.class, () -> {
final JSONArray json = JSONUtil.parseArray(123L);
Assertions.assertNotNull(json);
});
}
/**
* 数字解析为JSONArray报错
* 数字解析为JSONArray报错忽略错误则返回null
*/
@Test
public void parseNumberToJSONArrayTest2() {
Assertions.assertThrows(JSONException.class, ()->{
JSONUtil.parseArray(123L,
JSONConfig.of().setIgnoreError(false));
});
final JSONArray json = JSONUtil.parseArray(123L,
JSONConfig.of().setIgnoreError(true));
Assertions.assertNotNull(json);
Assertions.assertNull(json);
}
/**
@@ -135,7 +140,7 @@ public class JSONUtilTest {
@Test
public void parseNumberToJSONObjectTest2() {
final JSONObject json = JSONUtil.parseObj(123L, JSONConfig.of().setIgnoreError(true));
assertEquals(new JSONObject(), json);
assertNull(json);
}
@Test
@@ -341,7 +346,7 @@ public class JSONUtilTest {
public void testArrayEntity() {
final String jsonStr = JSONUtil.toJsonStr(new ArrayEntity());
// a为空的bytes数组按照空的流对待
assertEquals("{\"b\":[0],\"c\":[],\"d\":[],\"e\":[]}", jsonStr);
assertEquals("{\"a\":[],\"b\":[0],\"c\":[],\"d\":[],\"e\":[]}", jsonStr);
}
@Data

View File

@@ -26,9 +26,37 @@ import java.util.List;
public class Pr3067Test {
final String jsonStr = "[{\"username\":\"a\",\"password\":\"a-password\"}, {\"username\":\"b\",\"password\":\"b-password\"}]";
@Test
void toListTest() {
final JSONArray array = JSONUtil.parseArray(jsonStr);
final List<TestUser> resultList = array.toList(TestUser.class);
Assertions.assertNotNull(resultList);
Assertions.assertEquals(2, resultList.size());
Assertions.assertEquals("a", resultList.get(0).getUsername());
Assertions.assertEquals("a-password", resultList.get(0).getPassword());
Assertions.assertEquals("b", resultList.get(1).getUsername());
Assertions.assertEquals("b-password", resultList.get(1).getPassword());
}
@Test
void toTypeReferenceTest() {
final JSONArray array = JSONUtil.parseArray(jsonStr);
final List<TestUser> resultList = array.toBean(new TypeReference<List<TestUser>>() {});
Assertions.assertNotNull(resultList);
Assertions.assertEquals(2, resultList.size());
Assertions.assertEquals("a", resultList.get(0).getUsername());
Assertions.assertEquals("a-password", resultList.get(0).getPassword());
Assertions.assertEquals("b", resultList.get(1).getUsername());
Assertions.assertEquals("b-password", resultList.get(1).getPassword());
}
@Test
public void getListByPathTest1() {
final JSONObject json = JSONUtil.parseObj(ResourceUtil.readUtf8Str("test_json_path_001.json"));
final List<TestUser> resultList = json.getByPath("testUserList[1].testArray",
new TypeReference<List<TestUser>>() {});

View File

@@ -16,6 +16,7 @@
package org.dromara.hutool.json.engine;
import lombok.Data;
import org.dromara.hutool.core.date.DateTime;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.TimeUtil;
@@ -49,6 +50,11 @@ public class JSONEngineTest {
Arrays.stream(engineNames).forEach(this::assertWriteTimeZone);
}
@Test
void writeEmptyBeanTest() {
Arrays.stream(engineNames).forEach(this::assertEmptyBeanToJson);
}
private void assertWriteDateFormat(final String engineName) {
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
@@ -96,4 +102,15 @@ public class JSONEngineTest {
jsonString = engine.toJsonString(timeZone);
Assertions.assertEquals("\"GMT+08:00\"", jsonString);
}
private void assertEmptyBeanToJson(final String engineName){
final JSONEngine engine = JSONEngineFactory.createEngine(engineName);
final String jsonString = engine.toJsonString(new EmptyBean());
Assertions.assertEquals("{}", jsonString);
}
@Data
private static class EmptyBean{
}
}