mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix
This commit is contained in:
parent
90f9298370
commit
a4db1e0e52
@ -145,7 +145,15 @@ public class ObjectMapper {
|
|||||||
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, filter);
|
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, filter);
|
||||||
} else if (source instanceof byte[]) {
|
} else if (source instanceof byte[]) {
|
||||||
// bytes按照JSON的二进制流对待
|
// bytes按照JSON的二进制流对待
|
||||||
|
try{
|
||||||
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonArray.getConfig()), jsonArray, filter);
|
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonArray.getConfig()), jsonArray, filter);
|
||||||
|
} catch (final JSONException ignore){
|
||||||
|
// https://github.com/dromara/hutool/issues/2369
|
||||||
|
// 非标准的二进制流,则按照普通数组对待
|
||||||
|
for(final byte b : (byte[]) source){
|
||||||
|
jsonArray.add(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (source instanceof JSONTokener) {
|
} else if (source instanceof JSONTokener) {
|
||||||
mapFromTokener((JSONTokener) source, jsonArray, filter);
|
mapFromTokener((JSONTokener) source, jsonArray, filter);
|
||||||
} else {
|
} else {
|
||||||
|
19
hutool-json/src/test/java/cn/hutool/json/Issue2369Test.java
Normal file
19
hutool-json/src/test/java/cn/hutool/json/Issue2369Test.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package cn.hutool.json;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Issue2369Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toJsonStrTest(){
|
||||||
|
//https://github.com/dromara/hutool/issues/2369
|
||||||
|
// byte[]数组对于JSONArray来说,即可能是一个JSON字符串的二进制流,也可能是普通数组,因此需要做双向兼容
|
||||||
|
final byte[] bytes = {10, 11};
|
||||||
|
final String s = JSONUtil.toJsonStr(bytes);
|
||||||
|
Assert.assertEquals("[10,11]", s);
|
||||||
|
|
||||||
|
final Object o = JSONUtil.toBean(s, byte[].class, false);
|
||||||
|
Assert.assertArrayEquals(bytes, (byte[])o);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user