This commit is contained in:
Looly
2024-09-19 01:47:21 +08:00
parent c489cc7735
commit bc3f6cf1ee
35 changed files with 296 additions and 576 deletions

View File

@@ -51,7 +51,7 @@ public class Issue3274Test {
Gender(final String enum_name, final String en_Us, final String zh_CN) {
this.enum_name = enum_name;
this.display = new JSONArray("[{\"lang\": \"en-US\",\"value\": \"" + en_Us + "\"},{\"lang\": \"zh-CN\",\"value\": \"" + zh_CN + "\"}]");
this.display = JSONUtil.parseArray("[{\"lang\": \"en-US\",\"value\": \"" + en_Us + "\"},{\"lang\": \"zh-CN\",\"value\": \"" + zh_CN + "\"}]");
}
}
}

View File

@@ -17,7 +17,7 @@
package org.dromara.hutool.json;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.mapper.JSONObjectMapper;
import org.dromara.hutool.json.mapper.JSONValueMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -27,12 +27,11 @@ public class IssueI9DX5HTest {
@Test
void xmlToJSONTest() {
final String xml = "<GoodMsg>你好</GoodMsg>";
final JSONObjectMapper mapper = JSONObjectMapper.of(xml, entry -> {
final JSONValueMapper mapper = JSONValueMapper.of(JSONConfig.of(), entry -> {
entry.setKey(StrUtil.toUnderlineCase((CharSequence) entry.getKey()));
return true;
});
final JSONObject jsonObject = new JSONObject();
mapper.mapTo(jsonObject);
final JSONObject jsonObject = (JSONObject) mapper.map(xml);
Assertions.assertEquals("{\"good_msg\":\"你好\"}", jsonObject.toString());
}

View File

@@ -48,11 +48,11 @@ public class JSONArrayTest {
// JSONObject实现了Iterable接口可以转换为JSONArray
final JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray(jsonObject, JSONConfig.of());
JSONArray jsonArray = JSONUtil.parseArray(jsonObject, JSONConfig.of());
assertEquals(new JSONArray(), jsonArray);
jsonObject.set("key1", "value1");
jsonArray = new JSONArray(jsonObject, JSONConfig.of());
jsonArray = JSONUtil.parseArray(jsonObject, JSONConfig.of());
assertEquals(1, jsonArray.size());
assertEquals("[{\"key1\":\"value1\"}]", jsonArray.toString());
}
@@ -70,9 +70,9 @@ public class JSONArrayTest {
final JSONArray array = JSONUtil.ofArray();
// 方法2
// JSONArray array = new JSONArray();
array.add("value1");
array.add("value2");
array.add("value3");
array.set("value1");
array.set("value2");
array.set("value3");
assertEquals(array.get(0), "value1");
}
@@ -238,12 +238,12 @@ public class JSONArrayTest {
@Test
public void putToIndexTest() {
JSONArray jsonArray = new JSONArray();
jsonArray.set(3, "test");
jsonArray.setValue(3, "test");
// 默认忽略null值因此空位无值只有一个值
assertEquals(1, jsonArray.size());
jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullValue(false));
jsonArray.set(2, "test");
jsonArray.setValue(2, "test");
// 第三个位置插入值0~2都是null
assertEquals(3, jsonArray.size());
}
@@ -252,7 +252,7 @@ public class JSONArrayTest {
@Test
public void putTest2() {
final JSONArray jsonArray = new JSONArray();
jsonArray.put(0, 1);
jsonArray.setValue(0, 1);
assertEquals(1, jsonArray.size());
assertEquals(1, jsonArray.get(0));
}
@@ -307,7 +307,7 @@ public class JSONArrayTest {
public void parseFilterTest() {
final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> mutable.get().toString().contains("111"));
final JSONArray array = JSONUtil.parseArray(jsonArr, null, (mutable) -> mutable.get().toString().contains("111"));
assertEquals(1, array.size());
assertTrue(array.getJSONObject(0).containsKey("id"));
}
@@ -316,7 +316,7 @@ public class JSONArrayTest {
public void parseFilterEditTest() {
final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> {
final JSONArray array = JSONUtil.parseArray(jsonArr, null, (mutable) -> {
if(mutable.getKey() instanceof Integer){
final JSONObject o = (JSONObject) mutable.getValue();
if ("111".equals(o.getStr("id"))) {

View File

@@ -42,7 +42,7 @@ public class JSONNullTest {
" \"device_model\": null,\n" +
" \"device_status_date\": null,\n" +
" \"imsi\": null,\n" +
" \"act_date\": \"2021-07-23T06:23:26.000+00:00\"}", true);
" \"act_date\": \"2021-07-23T06:23:26.000+00:00\"}");
Assertions.assertFalse(bodyjson.containsKey("device_model"));
Assertions.assertFalse(bodyjson.containsKey("device_status_date"));
Assertions.assertFalse(bodyjson.containsKey("imsi"));

View File

@@ -313,7 +313,7 @@ public class JSONObjectTest {
userA.setDate(new Date());
userA.setSqs(ListUtil.of(new Seq(null), new Seq("seq2")));
final JSONObject json = JSONUtil.parseObj(userA, false);
final JSONObject json = JSONUtil.parseObj(userA, JSONConfig.of().setIgnoreNullValue(false));
Assertions.assertTrue(json.containsKey("a"));
Assertions.assertTrue(json.getJSONArray("sqs").getJSONObject(0).containsKey("seq"));
@@ -328,7 +328,8 @@ public class JSONObjectTest {
bean.setStrValue("strTest");
bean.setTestEnum(TestEnum.TYPE_B);
final JSONObject json = JSONUtil.parseObj(bean, false);
final JSONObject json = JSONUtil.parseObj(bean,
JSONConfig.of().setIgnoreNullValue(false));
// 枚举转换检查更新枚举原样保存在writer时调用toString。
Assertions.assertEquals(TestEnum.TYPE_B, json.getObj("testEnum"));
@@ -396,7 +397,8 @@ public class JSONObjectTest {
final JSONObject userAJson = JSONUtil.parseObj(userA);
Assertions.assertFalse(userAJson.containsKey("a"));
final JSONObject userAJsonWithNullValue = JSONUtil.parseObj(userA, false);
final JSONObject userAJsonWithNullValue = JSONUtil.parseObj(userA,
JSONConfig.of().setIgnoreNullValue(false));
Assertions.assertTrue(userAJsonWithNullValue.containsKey("a"));
Assertions.assertTrue(userAJsonWithNullValue.containsKey("sqs"));
}

View File

@@ -18,6 +18,7 @@ package org.dromara.hutool.json;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.json.reader.JSONTokener;
import org.junit.jupiter.api.Test;
@@ -29,6 +30,7 @@ public class JSONTokenerTest {
void parseTest() {
final JSONObject jsonObject = JSONUtil.parseObj(ResourceUtil.getUtf8Reader("issue1200.json"));
assertNotNull(jsonObject);
Console.log(jsonObject.toStringPretty());
}
@Test

View File

@@ -37,6 +37,7 @@ public class TransientTest {
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = JSONUtil.parseObj(detailBill,
JSONConfig.of().setTransientSupport(false));
Assertions.assertEquals("{\"id\":\"3243\",\"bizNo\":\"bizNo\"}", jsonObject.toString());
}

View File

@@ -16,12 +16,15 @@
package org.dromara.hutool.json.engine;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateTime;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.TimeUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
public class GsonTest {
/**
* Gson默认缩进两个空格使用\n换行符
@@ -54,4 +57,11 @@ public class GsonTest {
Assertions.assertEquals("{\"date\":\"2024-01-01 00:00:00\"}", engine.toJsonString(bean));
}
@Test
void arrayToStringTest() {
final ArrayList<Integer> integers = ListUtil.of(1, 2, 3);
final JSONEngine engine = JSONEngineFactory.createEngine("gson");
final String jsonString = engine.toJsonString(integers);
Assertions.assertEquals("[1,2,3]", jsonString);
}
}

View File

@@ -16,13 +16,16 @@
package org.dromara.hutool.json.jwt;
import lombok.Data;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.date.DatePattern;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.reflect.TypeReference;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.json.jwt.signers.AlgorithmUtil;
import org.dromara.hutool.json.jwt.signers.JWTSigner;
import org.dromara.hutool.json.jwt.signers.JWTSignerUtil;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -132,12 +135,12 @@ public class JWTTest {
final List<Integer> list = Arrays.asList(1, 2, 3);
final Integer num = 18;
final String username = "takaki";
final HashMap<String, String> map = new HashMap<>();
final Map<String, String> map = new HashMap<>();
map.put("test1", "1");
map.put("test2", "2");
final Map<String, Object> payload = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("username", username);
put("bean", bean);
@@ -155,17 +158,20 @@ public class JWTTest {
final Date dateRes = jwt.getPayload("date", Date.class);
final List<?> listRes = jwt.getPayload("list", List.class);
final Integer numRes = jwt.getPayload("number", Integer.class);
final HashMap<?, ?> mapRes = jwt.getPayload("map", HashMap.class);
final Map<String, String> mapRes = jwt.getPayload("map", new TypeReference<Map<String, String>>(){});
Assertions.assertEquals(bean, beanRes);
Assertions.assertEquals(numRes, num);
Assertions.assertEquals(username, strRes);
Assertions.assertEquals(list, listRes);
Assertions.assertEquals(
StrUtil.wrap(CollUtil.join(list, ","), "[", "]"),
listRes.toString());
final String formattedDate = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
final String formattedRes = DateUtil.format(dateRes, "yyyy-MM-dd HH:mm:ss");
Assertions.assertEquals(formattedDate, formattedRes);
Assertions.assertEquals(map, mapRes);
Assertions.assertEquals(map.get("test1"), mapRes.get("test1"));
Assertions.assertEquals(map.get("test2"), mapRes.get("test2"));
}
@Test()