From d0ff3ab07bedf205a1ee327a7f3b4021bd468256 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 10 Jun 2022 22:57:15 +0800 Subject: [PATCH] fix bu --- CHANGELOG.md | 1 + .../java/cn/hutool/json/ObjectMapper.java | 10 +++++++++- .../{ => cn/hutool/json}/Issue2365Test.java | 2 ++ .../java/cn/hutool/json/Issue2369Test.java | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) rename hutool-json/src/test/java/{ => cn/hutool/json}/Issue2365Test.java (95%) create mode 100644 hutool-json/src/test/java/cn/hutool/json/Issue2369Test.java diff --git a/CHANGELOG.md b/CHANGELOG.md index cc743043a..6307cc303 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * 【core 】 ArrayUtil.setOrAppend()传入空数组时,抛出异常(issue#I5APJE@Gitee) * 【extra 】 JschSessionPool修复空指针检查问题(issue#I5BK4D@Gitee) * 【core 】 修复使用ValueProvider中setFieldMapping无效问题(issue#I5B4R7@Gitee) +* 【json 】 修复byte[]作为JSONArray构造问题(issue#2369@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java b/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java index c8eeea2a1..11d53a837 100755 --- a/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java +++ b/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java @@ -145,7 +145,15 @@ public class ObjectMapper { mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, filter); } else if (source instanceof byte[]) { // bytes按照JSON的二进制流对待 - mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonArray.getConfig()), jsonArray, filter); + try{ + 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) { mapFromTokener((JSONTokener) source, jsonArray, filter); } else { diff --git a/hutool-json/src/test/java/Issue2365Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue2365Test.java similarity index 95% rename from hutool-json/src/test/java/Issue2365Test.java rename to hutool-json/src/test/java/cn/hutool/json/Issue2365Test.java index f3d0aafaa..b4a55dee1 100644 --- a/hutool-json/src/test/java/Issue2365Test.java +++ b/hutool-json/src/test/java/cn/hutool/json/Issue2365Test.java @@ -1,3 +1,5 @@ +package cn.hutool.json; + import cn.hutool.json.JSONUtil; import lombok.Data; import org.junit.Assert; diff --git a/hutool-json/src/test/java/cn/hutool/json/Issue2369Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue2369Test.java new file mode 100644 index 000000000..be9c1f8bd --- /dev/null +++ b/hutool-json/src/test/java/cn/hutool/json/Issue2369Test.java @@ -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); + } +}