fix byte[0]在json序列化时被忽略的bug;

This commit is contained in:
Zjp
2022-11-21 10:31:59 +08:00
parent fa115dffbc
commit e32129740a
2 changed files with 23 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.test.bean.Price;
import cn.hutool.json.test.bean.UserA;
import cn.hutool.json.test.bean.UserC;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
@@ -244,10 +245,29 @@ public class JSONUtilTest {
}
@Test(expected = JSONException.class)
public void duplicateKeyTrueTest(){
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());
}
/**
* 测试普通数组转JSONArray时是否异常, 尤其是byte[]数组, 可能是普通的byte[]数组, 也可能是二进制流
*/
@Test
public void testArrayEntity() {
final String jsonStr = JSONUtil.toJsonStr(new ArrayEntity());
Assert.assertEquals("{\"a\":[],\"b\":[0],\"c\":[],\"d\":[],\"e\":[]}", jsonStr);
}
@Data
static class ArrayEntity {
private byte[] a = new byte[0];
private byte[] b = new byte[1];
private int[] c = new int[0];
private Byte[] d = new Byte[0];
private Byte[] e = new Byte[1];
}
}