mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix json bug
This commit is contained in:
parent
6fcb3cd666
commit
1d32b0862d
@ -1,13 +1,14 @@
|
||||
package cn.hutool.core.convert.impl;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.collection.iter.IterUtil;
|
||||
import cn.hutool.core.convert.AbstractConverter;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ByteUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Array;
|
||||
@ -124,6 +125,16 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
||||
return convertArrayToArray(value.toString().toCharArray());
|
||||
}
|
||||
|
||||
//issue#2365
|
||||
// 字符串转bytes,首先判断是否为Base64,是则转换,否则按照默认getBytes方法。
|
||||
if(targetComponentType == byte.class){
|
||||
final String str = value.toString();
|
||||
if(Base64.isBase64(str)){
|
||||
return Base64.decode(value.toString());
|
||||
}
|
||||
return str.getBytes();
|
||||
}
|
||||
|
||||
// 单纯字符串情况下按照逗号分隔后劈开
|
||||
final String[] strings = StrUtil.splitToArray(value.toString(), CharUtil.COMMA);
|
||||
return convertArrayToArray(strings);
|
||||
|
20
hutool-json/src/test/java/Issue2365Test.java
Normal file
20
hutool-json/src/test/java/Issue2365Test.java
Normal file
@ -0,0 +1,20 @@
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Issue2365Test {
|
||||
@Test
|
||||
public void toBeanTest(){
|
||||
String jsonStr = "{\"fileName\":\"aaa\",\"fileBytes\":\"AQ==\"}";
|
||||
final FileInfo fileInfo = JSONUtil.toBean(jsonStr, FileInfo.class);
|
||||
Assert.assertEquals("aaa", fileInfo.getFileName());
|
||||
Assert.assertArrayEquals(new byte[]{1}, fileInfo.getFileBytes());
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class FileInfo {
|
||||
private String fileName;
|
||||
private byte[] fileBytes;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user