mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
bytes as array
This commit is contained in:
parent
a947d2171e
commit
917f6eeead
@ -1,6 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.codec.HexUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
@ -747,12 +746,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]){
|
||||
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;
|
||||
@ -238,13 +237,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);
|
||||
}
|
||||
} 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;
|
||||
|
||||
@ -11,12 +10,14 @@ public class Issue2377Test {
|
||||
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);
|
||||
|
||||
final List<Object> paramList = JSONUtil.toList(paramsStr, Object.class);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user