bytes as array

This commit is contained in:
Looly
2022-06-13 21:47:05 +08:00
parent c5ab974441
commit 2b7403162e
5 changed files with 18 additions and 30 deletions

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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) {