fix toBean

This commit is contained in:
Looly 2019-10-17 17:37:20 +08:00
parent ff65a8cc0c
commit 61ba892b49
17 changed files with 190 additions and 210 deletions

View File

@ -6,7 +6,9 @@
## 5.0.1
### 新特性
* 【json】 JSONUtil.toBean支持JSONArray
### Bug修复
* 【extra】 修复getSession端口判断错误
-------------------------------------------------------------------------------------------------------------

View File

@ -157,7 +157,7 @@ public class JschUtil {
*/
public static Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
Assert.notEmpty(sshHost, "SSH Host must be not empty!");
Assert.isTrue(sshPort < 0, "SSH Host must be not empty!");
Assert.isTrue(sshPort > 0, "SSH port must be > 0");
Assert.notEmpty(privateKeyPath, "PrivateKey Path must be not empty!");
// 默认root用户

View File

@ -1,5 +1,6 @@
package cn.hutool.extra.ssh;
import com.jcraft.jsch.JSch;
import org.junit.Ignore;
import org.junit.Test;
@ -61,4 +62,9 @@ public class JschUtilTest {
IoUtil.close(sftp);
}
@Test
public void getSessionTest(){
JschUtil.getSession("192.168.1.134", 22, "root", "aaa", null);
}
}

View File

@ -1,17 +1,19 @@
package cn.hutool.json;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.lang.TypeReference;
/**
* JSON接口
*
* @author Looly
*
* @author Looly
*/
public interface JSON extends Cloneable, Serializable{
public interface JSON extends Cloneable, Serializable {
/**
* 通过表达式获取JSON中嵌套的对象<br>
@ -19,16 +21,16 @@ public interface JSON extends Cloneable, Serializable{
* <li>.表达式可以获取Bean对象中的属性字段值或者Map中key对应的值</li>
* <li>[]表达式可以获取集合等对象中对应index的值</li>
* </ol>
*
* <p>
* 表达式栗子
*
*
* <pre>
* persion
* persion.name
* persons[3]
* person.friends[5].name
* </pre>
*
*
* @param expression 表达式
* @return 对象
* @see BeanPath#get(Object)
@ -44,20 +46,20 @@ public interface JSON extends Cloneable, Serializable{
* <li>.表达式可以获取Bean对象中的属性字段值或者Map中key对应的值</li>
* <li>[]表达式可以获取集合等对象中对应index的值</li>
* </ol>
*
* <p>
* 表达式栗子
*
*
* <pre>
* persion
* persion.name
* persons[3]
* person.friends[5].name
* </pre>
*
*
* @param expression 表达式
* @param value
* @param value
*/
public void putByPath(String expression, Object value);
void putByPath(String expression, Object value);
/**
* 通过表达式获取JSON中嵌套的对象<br>
@ -65,19 +67,19 @@ public interface JSON extends Cloneable, Serializable{
* <li>.表达式可以获取Bean对象中的属性字段值或者Map中key对应的值</li>
* <li>[]表达式可以获取集合等对象中对应index的值</li>
* </ol>
*
* <p>
* 表达式栗子
*
*
* <pre>
* persion
* persion.name
* persons[3]
* person.friends[5].name
* </pre>
*
* <p>
* 获取表达式对应值后转换为对应类型的值
*
* @param <T> 返回值类型
*
* @param <T> 返回值类型
* @param expression 表达式
* @param resultType 返回值类型
* @return 对象
@ -86,43 +88,100 @@ public interface JSON extends Cloneable, Serializable{
*/
<T> T getByPath(String expression, Class<T> resultType);
/**
* 格式化打印JSON缩进为4个空格
*
* @return 格式化后的JSON字符串
* @throws JSONException 包含非法数抛出此异常
* @since 3.0.9
*/
default String toStringPretty() throws JSONException {
return this.toJSONString(4);
}
/**
* 格式化输出JSON字符串
*
* @param indentFactor 每层缩进空格数
* @return JSON字符串
* @throws JSONException 包含非法数抛出此异常
*/
default String toJSONString(int indentFactor) throws JSONException {
final StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
}
/**
* 将JSON内容写入Writer无缩进<br>
* Warning: This method assumes that the data structure is acyclical.
*
*
* @param writer Writer
* @return Writer
* @throws JSONException JSON相关异常
*/
Writer write(Writer writer) throws JSONException;
default Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}
/**
* 将JSON内容写入Writer<br>
* Warning: This method assumes that the data structure is acyclical.
*
* @param writer writer
*
* @param writer writer
* @param indentFactor 缩进因子定义每一级别增加的缩进量
* @param indent 本级别缩进量
* @param indent 本级别缩进量
* @return Writer
* @throws JSONException JSON相关异常
*/
Writer write(Writer writer, int indentFactor, int indent) throws JSONException;
/**
* 转换为JSON字符串
*
* @param indentFactor 缩进因子定义每一级别增加的缩进量
* @return JSON字符串
* @throws JSONException JSON相关异常
* 为实体类对象转换异常将被抛出
*
* @param <T> Bean类型
* @param clazz 实体类
* @return 实体类对象
*/
String toJSONString(int indentFactor) throws JSONException;
default <T> T toBean(Class<T> clazz) {
return toBean((Type) clazz);
}
/**
* 格式化打印JSON缩进为4个空格
*
* @return 格式化后的JSON字符串
* @throws JSONException 包含非法数抛出此异常
* @since 3.0.9
* 转为实体类对象转换异常将被抛出
*
* @param <T> Bean类型
* @param reference {@link TypeReference}类型参考子类可以获取其泛型参数中的Type类型
* @return 实体类对象
* @since 4.2.2
*/
String toStringPretty() throws JSONException;
default <T> T toBean(TypeReference<T> reference) {
return toBean(reference.getType());
}
/**
* 转为实体类对象
*
* @param <T> Bean类型
* @param type {@link Type}
* @return 实体类对象
* @since 3.0.8
*/
default <T> T toBean(Type type) {
return toBean(type, false);
}
/**
* 转为实体类对象
*
* @param <T> Bean类型
* @param type {@link Type}
* @param ignoreError 是否忽略转换错误
* @return 实体类对象
* @since 4.3.2
*/
default <T> T toBean(Type type, boolean ignoreError) {
return JSONConverter.jsonConvert(type, this, ignoreError);
}
}

View File

@ -1,15 +1,5 @@
package cn.hutool.json;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.ArrayIter;
import cn.hutool.core.collection.CollUtil;
@ -20,6 +10,15 @@ import cn.hutool.core.util.TypeUtil;
import cn.hutool.json.serialize.GlobalSerializeMapping;
import cn.hutool.json.serialize.JSONSerializer;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
/**
* JSON数组<br>
* JSON数组是表示中括号括住的数据表现形式<br>
@ -498,38 +497,6 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
}
}
/**
* 格式化打印JSON缩进为4个空格
*
* @return 格式化后的JSON字符串
* @throws JSONException 包含非法数抛出此异常
* @since 3.0.9
*/
@Override
public String toStringPretty() throws JSONException {
return this.toJSONString(4);
}
/**
* 转为JSON字符串指定缩进值
*
* @param indentFactor 缩进值即缩进空格数
* @return JSON字符串
* @throws JSONException JSON写入异常
*/
@Override
public String toJSONString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
}
@Override
public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}
@Override
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
try {

View File

@ -77,7 +77,7 @@ public class JSONConverter implements Converter<JSON> {
}
}
Object targetValue = null;
Object targetValue;
try {
targetValue = Convert.convert(targetType, value);
} catch (ConvertException e) {

View File

@ -1,24 +1,10 @@
package cn.hutool.json;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import cn.hutool.core.bean.BeanDesc.PropDesc;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.util.ArrayUtil;
@ -30,6 +16,18 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
import cn.hutool.json.serialize.JSONObjectSerializer;
import cn.hutool.json.serialize.JSONSerializer;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* JSON对象<br>
* <br>
@ -111,9 +109,9 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
*/
public JSONObject(int capacity, JSONConfig config) {
if (config.isIgnoreCase()) {
this.rawHashMap = config.isOrder() ? new CaseInsensitiveLinkedMap<String, Object>(capacity) : new CaseInsensitiveMap<String, Object>(capacity);
this.rawHashMap = config.isOrder() ? new CaseInsensitiveLinkedMap<>(capacity) : new CaseInsensitiveMap<>(capacity);
} else {
this.rawHashMap = config.isOrder() ? new LinkedHashMap<String, Object>(capacity) : new HashMap<String, Object>(capacity);
this.rawHashMap = config.isOrder() ? new LinkedHashMap<>(capacity) : new HashMap<>(capacity);
}
this.config = config;
}
@ -288,54 +286,6 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
return ja;
}
/**
* 转为实体类对象转换异常将被抛出
*
* @param <T> Bean类型
* @param clazz 实体类
* @return 实体类对象
*/
public <T> T toBean(Class<T> clazz) {
return toBean((Type) clazz);
}
/**
* 转为实体类对象转换异常将被抛出
*
* @param <T> Bean类型
* @param reference {@link TypeReference}类型参考子类可以获取其泛型参数中的Type类型
* @return 实体类对象
* @since 4.2.2
*/
public <T> T toBean(TypeReference<T> reference) {
return toBean(reference.getType());
}
/**
* 转为实体类对象
*
* @param <T> Bean类型
* @param type {@link Type}
* @return 实体类对象
* @since 3.0.8
*/
public <T> T toBean(Type type) {
return toBean(type, false);
}
/**
* 转为实体类对象
*
* @param <T> Bean类型
* @param type {@link Type}
* @param ignoreError 是否忽略转换错误
* @return 实体类对象
* @since 4.3.2
*/
public <T> T toBean(Type type, boolean ignoreError) {
return JSONConverter.jsonConvert(type, this, ignoreError);
}
@Override
public int size() {
return rawHashMap.size();
@ -586,36 +536,6 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
}
}
/**
* 格式化打印JSON缩进为4个空格
*
* @return 格式化后的JSON字符串
* @throws JSONException 包含非法数抛出此异常
* @since 3.0.9
*/
@Override
public String toStringPretty() throws JSONException {
return this.toJSONString(4);
}
/**
* 格式化输出JSON字符串
*
* @param indentFactor 每层缩进空格数
* @return JSON字符串
* @throws JSONException 包含非法数抛出此异常
*/
@Override
public String toJSONString(int indentFactor) throws JSONException {
final StringWriter w = new StringWriter();
return this.write(w, indentFactor, 0).toString();
}
@Override
public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}
@Override
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
try {

View File

@ -378,7 +378,7 @@ public final class JSONUtil {
* @since 4.3.2
*/
public static <T> T toBean(String jsonString, Type beanType, boolean ignoreError) {
return toBean(parseObj(jsonString), beanType, ignoreError);
return toBean(parse(jsonString), beanType, ignoreError);
}
/**
@ -391,7 +391,7 @@ public final class JSONUtil {
* @return 实体类对象
* @since 4.6.2
*/
public static <T> T toBean(JSONObject json, TypeReference<T> typeReference, boolean ignoreError) {
public static <T> T toBean(JSON json, TypeReference<T> typeReference, boolean ignoreError) {
return toBean(json, typeReference.getType(), ignoreError);
}
@ -405,7 +405,7 @@ public final class JSONUtil {
* @return 实体类对象
* @since 4.3.2
*/
public static <T> T toBean(JSONObject json, Type beanType, boolean ignoreError) {
public static <T> T toBean(JSON json, Type beanType, boolean ignoreError) {
if (null == json) {
return null;
}

View File

@ -9,4 +9,5 @@ import cn.hutool.json.JSONArray;
*
* @author Looly
*/
@FunctionalInterface
public interface JSONArraySerializer<V> extends JSONSerializer<JSONArray, V>{}

View File

@ -9,6 +9,7 @@ import cn.hutool.json.JSON;
*
* @param <T> 反序列化后的类型
*/
@FunctionalInterface
public interface JSONDeserializer<T> {
/**

View File

@ -8,4 +8,5 @@ import cn.hutool.json.JSONObject;
*
* @author Looly
*/
@FunctionalInterface
public interface JSONObjectSerializer<V> extends JSONSerializer<JSONObject, V>{}

View File

@ -9,6 +9,7 @@ import cn.hutool.json.JSON;
* @param <V> 对象类型
* @author Looly
*/
@FunctionalInterface
public interface JSONSerializer<T extends JSON, V> {
/**

View File

@ -1,25 +1,17 @@
package cn.hutool.json;
import java.util.Date;
import cn.hutool.json.serialize.JSONObjectSerializer;
import lombok.ToString;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.json.serialize.JSONDeserializer;
import cn.hutool.json.serialize.JSONObjectSerializer;
import lombok.ToString;
import java.util.Date;
public class CustomSerializeTest {
@Test
public void serializeTest() {
JSONUtil.putSerializer(CustomBean.class, new JSONObjectSerializer<CustomBean>() {
@Override
public void serialize(JSONObject json, CustomBean bean) {
json.put("customName", bean.name);
}
});
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.put("customName", bean.name));
CustomBean customBean = new CustomBean();
customBean.name = "testName";
@ -30,15 +22,10 @@ public class CustomSerializeTest {
@Test
public void deserializeTest() {
JSONUtil.putDeserializer(CustomBean.class, new JSONDeserializer<CustomBean>() {
@Override
public CustomBean deserialize(JSON json) {
CustomBean customBean = new CustomBean();
customBean.name = ((JSONObject)json).getStr("customName");
return customBean;
}
JSONUtil.putDeserializer(CustomBean.class, json -> {
CustomBean customBean = new CustomBean();
customBean.name = ((JSONObject)json).getStr("customName");
return customBean;
});
String jsonStr = "{\"customName\":\"testName\"}";

View File

@ -31,14 +31,36 @@ public class Issue488Test {
Assert.assertEquals("MeetingRoom219@abc.com", adds.get(3).getAddress());
}
@Test
public void toCollctionBeanTest() {
String jsonStr = ResourceUtil.readUtf8Str("issue488Array.json");
List<ResultSuccess<List<EmailAddress>>> resultList = JSONUtil.toBean(jsonStr,
new TypeReference<List<ResultSuccess<List<EmailAddress>>>>() {}, false);
ResultSuccess<List<EmailAddress>> result = resultList.get(0);
Assert.assertEquals("https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)", result.getContext());
List<EmailAddress> adds = result.getValue();
Assert.assertEquals("会议室101", adds.get(0).getName());
Assert.assertEquals("MeetingRoom101@abc.com", adds.get(0).getAddress());
Assert.assertEquals("会议室102", adds.get(1).getName());
Assert.assertEquals("MeetingRoom102@abc.com", adds.get(1).getAddress());
Assert.assertEquals("会议室103", adds.get(2).getName());
Assert.assertEquals("MeetingRoom103@abc.com", adds.get(2).getAddress());
Assert.assertEquals("会议室219", adds.get(3).getName());
Assert.assertEquals("MeetingRoom219@abc.com", adds.get(3).getAddress());
}
@Data
public class ResultSuccess<T> {
public static class ResultSuccess<T> {
private String context;
private T value;
}
@Data
public class EmailAddress {
public static class EmailAddress {
private String name;
private String address;
}

View File

@ -1,20 +1,17 @@
package cn.hutool.json;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.test.bean.ExamInfoDict;
import cn.hutool.json.test.bean.PerfectEvaluationProductResVo;
import cn.hutool.json.test.bean.UserInfoDict;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.test.bean.ExamInfoDict;
import cn.hutool.json.test.bean.PerfectEvaluationProductResVo;
import cn.hutool.json.test.bean.UserInfoDict;
/**
* JSON转换单元测试
*
@ -46,14 +43,14 @@ public class JSONConvertTest {
examInfoDict2.setExamType(1);
examInfoDict2.setAnswerIs(0);
List<ExamInfoDict> examInfoDicts = new ArrayList<ExamInfoDict>();
List<ExamInfoDict> examInfoDicts = new ArrayList<>();
examInfoDicts.add(examInfoDict);
examInfoDicts.add(examInfoDict1);
examInfoDicts.add(examInfoDict2);
userInfoDict.setExamInfoDict(examInfoDicts);
Map<String, Object> tempMap = new HashMap<String, Object>();
Map<String, Object> tempMap = new HashMap<>();
tempMap.put("userInfoDict", userInfoDict);
tempMap.put("toSendManIdCard", 1);

View File

@ -161,7 +161,7 @@ public class JSONObjectTest {
// 当JSON中为字符串"null"时应被当作字符串处理
Assert.assertEquals("null", bean.getStrValue());
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
Assert.assertEquals(null, bean.getBeanValue());
Assert.assertNull(bean.getBeanValue());
}
@Test

View File

@ -0,0 +1,16 @@
[{
"context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)",
"value": [{
"name": "\u4f1a\u8bae\u5ba4101",
"address": "MeetingRoom101@abc.com"
}, {
"name": "\u4f1a\u8bae\u5ba4102",
"address": "MeetingRoom102@abc.com"
}, {
"name": "\u4f1a\u8bae\u5ba4103",
"address": "MeetingRoom103@abc.com"
}, {
"name": "\u4f1a\u8bae\u5ba4219",
"address": "MeetingRoom219@abc.com"
}]
}]