This commit is contained in:
Looly
2022-06-22 22:18:24 +08:00
parent 21bfa0ebff
commit a4f0f46ab1
21 changed files with 571 additions and 471 deletions

View File

@@ -3,16 +3,20 @@ package cn.hutool.json;
import cn.hutool.json.serialize.JSONObjectSerializer;
import lombok.ToString;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Date;
public class CustomSerializeTest {
@Before
public void init(){
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.set("customName", bean.name));
}
@Test
public void serializeTest() {
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.set("customName", bean.name));
final CustomBean customBean = new CustomBean();
customBean.name = "testName";
@@ -20,6 +24,15 @@ public class CustomSerializeTest {
Assert.assertEquals("testName", obj.getStr("customName"));
}
@Test
public void putTest() {
final CustomBean customBean = new CustomBean();
customBean.name = "testName";
final JSONObject obj = JSONUtil.createObj().set("customBean", customBean);
Assert.assertEquals("testName", obj.getJSONObject("customBean").getStr("customName"));
}
@Test
public void deserializeTest() {
JSONUtil.putDeserializer(CustomBean.class, json -> {

View File

@@ -1,5 +1,6 @@
package cn.hutool.json;
import cn.hutool.core.lang.Console;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
@@ -10,7 +11,7 @@ import java.time.LocalTime;
import java.time.Month;
/**
* https://github.com/dromara/hutool/issues/2090
* <a href="https://github.com/dromara/hutool/issues/2090">https://github.com/dromara/hutool/issues/2090</a>
*/
public class Issue2090Test {
@@ -20,6 +21,7 @@ public class Issue2090Test {
test.setLocalDate(LocalDate.now());
final JSONObject json = JSONUtil.parseObj(test);
Console.log(json);
final TestBean test1 = json.toBean(TestBean.class);
Assert.assertEquals(test, test1);
}

View File

@@ -15,7 +15,7 @@ public class IssuesI44E4HTest {
@Test
public void deserializerTest(){
GlobalSerializeMapping.put(TestDto.class, (JSONDeserializer<TestDto>) json -> {
GlobalSerializeMapping.putDeserializer(TestDto.class, (JSONDeserializer<TestDto>) json -> {
final TestDto testDto = new TestDto();
testDto.setMd(new AcBizModuleMd("name1", ((JSONObject)json).getStr("md")));
return testDto;

View File

@@ -5,9 +5,7 @@ import cn.hutool.core.annotation.PropIgnore;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ObjUtil;
@@ -24,7 +22,6 @@ import cn.hutool.json.test.bean.report.StepReport;
import cn.hutool.json.test.bean.report.SuiteReport;
import lombok.Data;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayInputStream;
@@ -47,13 +44,13 @@ import java.util.Set;
public class JSONObjectTest {
@Test
@Ignore
public void toStringTest() {
final String str = "{\"code\": 500, \"data\":null}";
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(str);
Console.log(jsonObject);
Assert.assertEquals("{\"code\":500,\"data\":null}", jsonObject.toString());
jsonObject.getConfig().setIgnoreNullValue(true);
Console.log(jsonObject.toStringPretty());
Assert.assertEquals("{\"code\":500}", jsonObject.toString());
}
@Test
@@ -176,16 +173,6 @@ public class JSONObjectTest {
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
}
@Test
@Ignore
public void parseStringWithBomTest() {
final String jsonStr = FileUtil.readUtf8String("f:/test/jsontest.txt");
final JSONObject json = new JSONObject(jsonStr);
final JSONObject json2 = JSONUtil.parseObj(json);
Console.log(json);
Console.log(json2);
}
@Test
public void parseStringWithSlashTest() {
//在5.3.2之前,</div>中的/会被转义修复此bug的单元测试

View File

@@ -2,9 +2,9 @@ package cn.hutool.json;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.math.NumberUtil;
import cn.hutool.json.serialize.JSONString;
import cn.hutool.json.test.bean.Price;
import cn.hutool.json.test.bean.UserA;
import cn.hutool.json.test.bean.UserC;
@@ -25,8 +25,7 @@ public class JSONUtilTest {
*/
@Test(expected = JSONException.class)
public void parseTest() {
final JSONArray jsonArray = JSONUtil.parseArray("[{\"a\":\"a\\x]");
Console.log(jsonArray);
JSONUtil.parseArray("[{\"a\":\"a\\x]");
}
/**
@@ -107,6 +106,7 @@ public class JSONUtilTest {
@Test
public void toJsonStrFromSortedTest() {
//noinspection SerializableInnerClassWithNonSerializableOuterClass
final SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
private static final long serialVersionUID = 1L;
@@ -236,14 +236,14 @@ public class JSONUtilTest {
@Test
public void toJsonStrOfStringTest(){
String a = "a";
final String a = "a";
final String s = JSONUtil.toJsonStr(a);
Assert.assertEquals(a, s);
}
@Test
public void toJsonStrOfNumberTest(){
int a = 1;
final int a = 1;
final String s = JSONUtil.toJsonStr(a);
Assert.assertEquals("1", s);
}