This commit is contained in:
Looly 2022-11-24 12:40:07 +08:00
parent e85bd4ada4
commit 7ea286cb29

View File

@ -8,6 +8,7 @@ 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;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
@ -247,4 +248,23 @@ public class JSONUtilTest {
final String s = JSONUtil.toJsonStr(a);
Assert.assertEquals("1", s);
}
/**
* 测试普通数组转JSONArray时是否异常, 尤其是byte[]数组, 可能是普通的byte[]数组, 也可能是二进制流
*/
@Test
public void testArrayEntity() {
final String jsonStr = JSONUtil.toJsonStr(new ArrayEntity());
// a为空的bytes数组按照空的流对待
Assert.assertEquals("{\"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];
}
}