mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix json bug
This commit is contained in:
parent
969a017f02
commit
bfa130b564
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.8.2.M1 (2022-05-26)
|
||||
# 5.8.2.M1 (2022-05-27)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 BeanUtil拷贝对象增加空检查(issue#I58CJ3@Gitee)
|
||||
@ -18,6 +18,7 @@
|
||||
* 【core 】 修复NumberUtil除法空指针问题(issue#I58XKE@Gitee)
|
||||
* 【core 】 修复CAR_VIN正则(pr#624@Gitee)
|
||||
* 【db 】 修复count查询别名问题(issue#I590YB@Gitee)
|
||||
* 【json 】 修复json中byte[]无法转换问题(issue#I59LW4@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.convert.Converter;
|
||||
@ -85,6 +86,9 @@ public class JSONConverter implements Converter<JSON> {
|
||||
}
|
||||
target.parse(value);
|
||||
return (T) target;
|
||||
} else if(targetType == byte[].class && value instanceof CharSequence){
|
||||
// issue#I59LW4
|
||||
return (T) Base64.decode((CharSequence) value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,6 +190,17 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return Convert.toLocalDateTime(obj, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]数据
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
* @since 5.8.2
|
||||
*/
|
||||
default byte[] getBytes(K key) {
|
||||
return get(key, byte[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象<br>
|
||||
* 转换失败或抛出异常
|
||||
|
@ -1,5 +1,6 @@
|
||||
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;
|
||||
@ -784,6 +785,12 @@ 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,6 +144,7 @@ public class ObjectMapper {
|
||||
} else if (source instanceof InputStream) {
|
||||
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);
|
||||
} else if (source instanceof JSONTokener) {
|
||||
mapFromTokener((JSONTokener) source, jsonArray, filter);
|
||||
|
@ -1,5 +1,6 @@
|
||||
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;
|
||||
@ -167,7 +168,6 @@ public class JSONWriter extends Writer {
|
||||
if(JSONUtil.isNull(value) && config.isIgnoreNullValue()){
|
||||
return this;
|
||||
}
|
||||
|
||||
return writeKey(key).writeValueDirect(value);
|
||||
}
|
||||
|
||||
@ -226,7 +226,13 @@ 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)) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
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) {
|
||||
|
@ -0,0 +1,25 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IssueI59LW4Test {
|
||||
|
||||
@Test
|
||||
public void bytesTest(){
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("bytes", new byte[]{1});
|
||||
Assert.assertEquals("{\"bytes\":\"AQ==\"}", jsonObject.toString());
|
||||
|
||||
final byte[] bytes = jsonObject.getBytes("bytes");
|
||||
Assert.assertArrayEquals(new byte[]{1}, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bytesInJSONArrayTest(){
|
||||
final JSONArray jsonArray = JSONUtil.createArray().set(new byte[]{1});
|
||||
Assert.assertEquals("[\"AQ==\"]", jsonArray.toString());
|
||||
|
||||
final byte[] bytes = jsonArray.getBytes(0);
|
||||
Assert.assertArrayEquals(new byte[]{1}, bytes);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user