mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix json bug
This commit is contained in:
parent
66fe842140
commit
55923c4af2
@ -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,16 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return Convert.toLocalDateTime(obj, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]数据
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
default byte[] getBytes(final K key) {
|
||||
return get(key, byte[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象<br>
|
||||
* 转换失败或抛出异常
|
||||
|
@ -1,15 +1,16 @@
|
||||
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;
|
||||
import cn.hutool.core.reflect.TypeReference;
|
||||
import cn.hutool.core.map.MapWrapper;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.codec.HexUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.reflect.TypeReference;
|
||||
import cn.hutool.core.reflect.TypeUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONArraySerializer;
|
||||
import cn.hutool.json.serialize.JSONDeserializer;
|
||||
@ -748,6 +749,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,14 +1,15 @@
|
||||
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;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.math.NumberUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.json.JSON;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
@ -226,7 +227,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,24 @@
|
||||
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…
x
Reference in New Issue
Block a user