mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
bytes as array
This commit is contained in:
parent
c5ab974441
commit
2b7403162e
@ -1,6 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
@ -785,12 +784,6 @@ public class JSONUtil {
|
||||
|
||||
// JSONArray
|
||||
if (object instanceof Iterable || ArrayUtil.isArray(object)) {
|
||||
if(object instanceof byte[]){
|
||||
// issue#I59LW4
|
||||
// json内容中的bytes默认转为Base64
|
||||
return Base64.encode((byte[]) object);
|
||||
}
|
||||
|
||||
return new JSONArray(object, jsonConfig);
|
||||
}
|
||||
// JSONObject
|
||||
|
@ -144,13 +144,13 @@ public class ObjectMapper {
|
||||
} else if (source instanceof InputStream) {
|
||||
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, filter);
|
||||
} else if (source instanceof byte[]) {
|
||||
// bytes按照JSON的二进制流对待
|
||||
try{
|
||||
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonArray.getConfig()), jsonArray, filter);
|
||||
} catch (final JSONException ignore){
|
||||
final byte[] bytesSource = (byte[]) source;
|
||||
if('[' == bytesSource[0] && ']' == bytesSource[bytesSource.length - 1]){
|
||||
mapFromTokener(new JSONTokener(IoUtil.toStream(bytesSource), jsonArray.getConfig()), jsonArray, filter);
|
||||
}else{
|
||||
// https://github.com/dromara/hutool/issues/2369
|
||||
// 非标准的二进制流,则按照普通数组对待
|
||||
for(final byte b : (byte[]) source){
|
||||
for(final byte b : bytesSource){
|
||||
jsonArray.add(b);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.hutool.json.serialize;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TemporalAccessorUtil;
|
||||
@ -226,13 +225,7 @@ public class JSONWriter extends Writer {
|
||||
} else if (value instanceof Map || value instanceof Map.Entry) {
|
||||
new JSONObject(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Iterable || value instanceof Iterator || ArrayUtil.isArray(value)) {
|
||||
if(value instanceof byte[]){
|
||||
// issue#I59LW4
|
||||
// json内容中的bytes默认转为Base64
|
||||
writeStrValue(Base64.encode((byte[]) value));
|
||||
}else{
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
}
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
writeNumberValue((Number) value);
|
||||
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -9,15 +8,18 @@ import java.util.List;
|
||||
public class Issue2377Test {
|
||||
|
||||
@Test
|
||||
public void bytesTest(){
|
||||
Object[] paramArray = new Object[]{ 1,new byte[]{10,11}, "报表.xlsx"};
|
||||
String paramsStr = JSONUtil.toJsonStr(paramArray);
|
||||
public void bytesTest() {
|
||||
final Object[] paramArray = new Object[]{1, new byte[]{10, 11}, "报表.xlsx"};
|
||||
final String paramsStr = JSONUtil.toJsonStr(paramArray);
|
||||
Assert.assertEquals("[1,[10,11],\"报表.xlsx\"]", paramsStr);
|
||||
|
||||
List<Object> paramList = JSONUtil.toList(paramsStr, Object.class);
|
||||
final List<Object> paramList = JSONUtil.toList(paramsStr, Object.class);
|
||||
|
||||
String paramBytesStr = JSONUtil.toJsonStr(paramList.get(1));
|
||||
final String paramBytesStr = JSONUtil.toJsonStr(paramList.get(1));
|
||||
Assert.assertEquals("[10,11]", paramBytesStr);
|
||||
|
||||
final byte[] convert = Convert.convert(byte[].class, paramBytesStr);
|
||||
Assert.assertArrayEquals((byte[])paramArray[1], convert);
|
||||
final byte[] paramBytes = JSONUtil.toBean(paramBytesStr, byte[].class, false);
|
||||
Assert.assertArrayEquals((byte[]) paramArray[1], paramBytes);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ public class IssueI59LW4Test {
|
||||
@Test
|
||||
public void bytesTest(){
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("bytes", new byte[]{1});
|
||||
Assert.assertEquals("{\"bytes\":\"AQ==\"}", jsonObject.toString());
|
||||
Assert.assertEquals("{\"bytes\":[1]}", jsonObject.toString());
|
||||
|
||||
final byte[] bytes = jsonObject.getBytes("bytes");
|
||||
Assert.assertArrayEquals(new byte[]{1}, bytes);
|
||||
@ -17,7 +17,7 @@ public class IssueI59LW4Test {
|
||||
@Test
|
||||
public void bytesInJSONArrayTest(){
|
||||
final JSONArray jsonArray = JSONUtil.createArray().set(new byte[]{1});
|
||||
Assert.assertEquals("[\"AQ==\"]", jsonArray.toString());
|
||||
Assert.assertEquals("[[1]]", jsonArray.toString());
|
||||
|
||||
final byte[] bytes = jsonArray.getBytes(0);
|
||||
Assert.assertArrayEquals(new byte[]{1}, bytes);
|
||||
|
Loading…
Reference in New Issue
Block a user