mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix toBean
This commit is contained in:
parent
ff65a8cc0c
commit
61ba892b49
@ -6,7 +6,9 @@
|
|||||||
## 5.0.1
|
## 5.0.1
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
|
* 【json】 JSONUtil.toBean支持JSONArray
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
* 【extra】 修复getSession端口判断错误
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ public class JschUtil {
|
|||||||
*/
|
*/
|
||||||
public static Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
|
public static Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
|
||||||
Assert.notEmpty(sshHost, "SSH Host must be not empty!");
|
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!");
|
Assert.notEmpty(privateKeyPath, "PrivateKey Path must be not empty!");
|
||||||
|
|
||||||
// 默认root用户
|
// 默认root用户
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.extra.ssh;
|
package cn.hutool.extra.ssh;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.JSch;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -61,4 +62,9 @@ public class JschUtilTest {
|
|||||||
|
|
||||||
IoUtil.close(sftp);
|
IoUtil.close(sftp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getSessionTest(){
|
||||||
|
JschUtil.getSession("192.168.1.134", 22, "root", "aaa", null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
package cn.hutool.json;
|
package cn.hutool.json;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanPath;
|
import cn.hutool.core.bean.BeanPath;
|
||||||
|
import cn.hutool.core.lang.TypeReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON接口
|
* JSON接口
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface JSON extends Cloneable, Serializable{
|
public interface JSON extends Cloneable, Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过表达式获取JSON中嵌套的对象<br>
|
* 通过表达式获取JSON中嵌套的对象<br>
|
||||||
@ -19,7 +21,7 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
||||||
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
||||||
* </ol>
|
* </ol>
|
||||||
*
|
* <p>
|
||||||
* 表达式栗子:
|
* 表达式栗子:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -44,7 +46,7 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
||||||
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
||||||
* </ol>
|
* </ol>
|
||||||
*
|
* <p>
|
||||||
* 表达式栗子:
|
* 表达式栗子:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -55,9 +57,9 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param expression 表达式
|
* @param expression 表达式
|
||||||
* @param value 值
|
* @param value 值
|
||||||
*/
|
*/
|
||||||
public void putByPath(String expression, Object value);
|
void putByPath(String expression, Object value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过表达式获取JSON中嵌套的对象<br>
|
* 通过表达式获取JSON中嵌套的对象<br>
|
||||||
@ -65,7 +67,7 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
||||||
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
||||||
* </ol>
|
* </ol>
|
||||||
*
|
* <p>
|
||||||
* 表达式栗子:
|
* 表达式栗子:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -74,10 +76,10 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
* persons[3]
|
* persons[3]
|
||||||
* person.friends[5].name
|
* person.friends[5].name
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
* <p>
|
||||||
* 获取表达式对应值后转换为对应类型的值
|
* 获取表达式对应值后转换为对应类型的值
|
||||||
*
|
*
|
||||||
* @param <T> 返回值类型
|
* @param <T> 返回值类型
|
||||||
* @param expression 表达式
|
* @param expression 表达式
|
||||||
* @param resultType 返回值类型
|
* @param resultType 返回值类型
|
||||||
* @return 对象
|
* @return 对象
|
||||||
@ -86,6 +88,31 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
*/
|
*/
|
||||||
<T> T getByPath(String expression, Class<T> resultType);
|
<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>
|
* 将JSON内容写入Writer,无缩进<br>
|
||||||
* Warning: This method assumes that the data structure is acyclical.
|
* Warning: This method assumes that the data structure is acyclical.
|
||||||
@ -94,35 +121,67 @@ public interface JSON extends Cloneable, Serializable{
|
|||||||
* @return Writer
|
* @return Writer
|
||||||
* @throws JSONException JSON相关异常
|
* @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>
|
* 将JSON内容写入Writer<br>
|
||||||
* Warning: This method assumes that the data structure is acyclical.
|
* Warning: This method assumes that the data structure is acyclical.
|
||||||
*
|
*
|
||||||
* @param writer writer
|
* @param writer writer
|
||||||
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
||||||
* @param indent 本级别缩进量
|
* @param indent 本级别缩进量
|
||||||
* @return Writer
|
* @return Writer
|
||||||
* @throws JSONException JSON相关异常
|
* @throws JSONException JSON相关异常
|
||||||
*/
|
*/
|
||||||
Writer write(Writer writer, int indentFactor, int indent) throws JSONException;
|
Writer write(Writer writer, int indentFactor, int indent) throws JSONException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为JSON字符串
|
* 转为实体类对象,转换异常将被抛出
|
||||||
*
|
*
|
||||||
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
* @param <T> Bean类型
|
||||||
* @return JSON字符串
|
* @param clazz 实体类
|
||||||
* @throws JSONException JSON相关异常
|
* @return 实体类对象
|
||||||
*/
|
*/
|
||||||
String toJSONString(int indentFactor) throws JSONException;
|
default <T> T toBean(Class<T> clazz) {
|
||||||
|
return toBean((Type) clazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化打印JSON,缩进为4个空格
|
* 转为实体类对象,转换异常将被抛出
|
||||||
*
|
*
|
||||||
* @return 格式化后的JSON字符串
|
* @param <T> Bean类型
|
||||||
* @throws JSONException 包含非法数抛出此异常
|
* @param reference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||||
* @since 3.0.9
|
* @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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,5 @@
|
|||||||
package cn.hutool.json;
|
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.bean.BeanPath;
|
||||||
import cn.hutool.core.collection.ArrayIter;
|
import cn.hutool.core.collection.ArrayIter;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
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.GlobalSerializeMapping;
|
||||||
import cn.hutool.json.serialize.JSONSerializer;
|
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>
|
||||||
* 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
|
@Override
|
||||||
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
||||||
try {
|
try {
|
||||||
|
@ -77,7 +77,7 @@ public class JSONConverter implements Converter<JSON> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object targetValue = null;
|
Object targetValue;
|
||||||
try {
|
try {
|
||||||
targetValue = Convert.convert(targetType, value);
|
targetValue = Convert.convert(targetType, value);
|
||||||
} catch (ConvertException e) {
|
} catch (ConvertException e) {
|
||||||
|
@ -1,24 +1,10 @@
|
|||||||
package cn.hutool.json;
|
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.BeanDesc.PropDesc;
|
||||||
import cn.hutool.core.bean.BeanPath;
|
import cn.hutool.core.bean.BeanPath;
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.lang.TypeReference;
|
|
||||||
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
|
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
|
||||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
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.JSONObjectSerializer;
|
||||||
import cn.hutool.json.serialize.JSONSerializer;
|
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>
|
* JSON对象<br>
|
||||||
* 例:<br>
|
* 例:<br>
|
||||||
@ -111,9 +109,9 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
|||||||
*/
|
*/
|
||||||
public JSONObject(int capacity, JSONConfig config) {
|
public JSONObject(int capacity, JSONConfig config) {
|
||||||
if (config.isIgnoreCase()) {
|
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 {
|
} 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;
|
this.config = config;
|
||||||
}
|
}
|
||||||
@ -288,54 +286,6 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
|||||||
return ja;
|
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
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return rawHashMap.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
|
@Override
|
||||||
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
||||||
try {
|
try {
|
||||||
|
@ -378,7 +378,7 @@ public final class JSONUtil {
|
|||||||
* @since 4.3.2
|
* @since 4.3.2
|
||||||
*/
|
*/
|
||||||
public static <T> T toBean(String jsonString, Type beanType, boolean ignoreError) {
|
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 实体类对象
|
* @return 实体类对象
|
||||||
* @since 4.6.2
|
* @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);
|
return toBean(json, typeReference.getType(), ignoreError);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -405,7 +405,7 @@ public final class JSONUtil {
|
|||||||
* @return 实体类对象
|
* @return 实体类对象
|
||||||
* @since 4.3.2
|
* @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) {
|
if (null == json) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -9,4 +9,5 @@ import cn.hutool.json.JSONArray;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
public interface JSONArraySerializer<V> extends JSONSerializer<JSONArray, V>{}
|
public interface JSONArraySerializer<V> extends JSONSerializer<JSONArray, V>{}
|
||||||
|
@ -9,6 +9,7 @@ import cn.hutool.json.JSON;
|
|||||||
*
|
*
|
||||||
* @param <T> 反序列化后的类型
|
* @param <T> 反序列化后的类型
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
public interface JSONDeserializer<T> {
|
public interface JSONDeserializer<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,4 +8,5 @@ import cn.hutool.json.JSONObject;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
public interface JSONObjectSerializer<V> extends JSONSerializer<JSONObject, V>{}
|
public interface JSONObjectSerializer<V> extends JSONSerializer<JSONObject, V>{}
|
||||||
|
@ -9,6 +9,7 @@ import cn.hutool.json.JSON;
|
|||||||
* @param <V> 对象类型
|
* @param <V> 对象类型
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
public interface JSONSerializer<T extends JSON, V> {
|
public interface JSONSerializer<T extends JSON, V> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,25 +1,17 @@
|
|||||||
package cn.hutool.json;
|
package cn.hutool.json;
|
||||||
|
|
||||||
import java.util.Date;
|
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||||
|
import lombok.ToString;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import cn.hutool.json.serialize.JSONDeserializer;
|
import java.util.Date;
|
||||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
public class CustomSerializeTest {
|
public class CustomSerializeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serializeTest() {
|
public void serializeTest() {
|
||||||
JSONUtil.putSerializer(CustomBean.class, new JSONObjectSerializer<CustomBean>() {
|
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.put("customName", bean.name));
|
||||||
|
|
||||||
@Override
|
|
||||||
public void serialize(JSONObject json, CustomBean bean) {
|
|
||||||
json.put("customName", bean.name);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
CustomBean customBean = new CustomBean();
|
CustomBean customBean = new CustomBean();
|
||||||
customBean.name = "testName";
|
customBean.name = "testName";
|
||||||
@ -30,15 +22,10 @@ public class CustomSerializeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void deserializeTest() {
|
public void deserializeTest() {
|
||||||
JSONUtil.putDeserializer(CustomBean.class, new JSONDeserializer<CustomBean>() {
|
JSONUtil.putDeserializer(CustomBean.class, json -> {
|
||||||
|
CustomBean customBean = new CustomBean();
|
||||||
@Override
|
customBean.name = ((JSONObject)json).getStr("customName");
|
||||||
public CustomBean deserialize(JSON json) {
|
return customBean;
|
||||||
CustomBean customBean = new CustomBean();
|
|
||||||
customBean.name = ((JSONObject)json).getStr("customName");
|
|
||||||
return customBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
String jsonStr = "{\"customName\":\"testName\"}";
|
String jsonStr = "{\"customName\":\"testName\"}";
|
||||||
|
@ -31,14 +31,36 @@ public class Issue488Test {
|
|||||||
Assert.assertEquals("MeetingRoom219@abc.com", adds.get(3).getAddress());
|
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
|
@Data
|
||||||
public class ResultSuccess<T> {
|
public static class ResultSuccess<T> {
|
||||||
private String context;
|
private String context;
|
||||||
private T value;
|
private T value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class EmailAddress {
|
public static class EmailAddress {
|
||||||
private String name;
|
private String name;
|
||||||
private String address;
|
private String address;
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,17 @@
|
|||||||
package cn.hutool.json;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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转换单元测试
|
* JSON转换单元测试
|
||||||
*
|
*
|
||||||
@ -46,14 +43,14 @@ public class JSONConvertTest {
|
|||||||
examInfoDict2.setExamType(1);
|
examInfoDict2.setExamType(1);
|
||||||
examInfoDict2.setAnswerIs(0);
|
examInfoDict2.setAnswerIs(0);
|
||||||
|
|
||||||
List<ExamInfoDict> examInfoDicts = new ArrayList<ExamInfoDict>();
|
List<ExamInfoDict> examInfoDicts = new ArrayList<>();
|
||||||
examInfoDicts.add(examInfoDict);
|
examInfoDicts.add(examInfoDict);
|
||||||
examInfoDicts.add(examInfoDict1);
|
examInfoDicts.add(examInfoDict1);
|
||||||
examInfoDicts.add(examInfoDict2);
|
examInfoDicts.add(examInfoDict2);
|
||||||
|
|
||||||
userInfoDict.setExamInfoDict(examInfoDicts);
|
userInfoDict.setExamInfoDict(examInfoDicts);
|
||||||
|
|
||||||
Map<String, Object> tempMap = new HashMap<String, Object>();
|
Map<String, Object> tempMap = new HashMap<>();
|
||||||
tempMap.put("userInfoDict", userInfoDict);
|
tempMap.put("userInfoDict", userInfoDict);
|
||||||
tempMap.put("toSendManIdCard", 1);
|
tempMap.put("toSendManIdCard", 1);
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ public class JSONObjectTest {
|
|||||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||||
Assert.assertEquals("null", bean.getStrValue());
|
Assert.assertEquals("null", bean.getStrValue());
|
||||||
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
|
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
|
||||||
Assert.assertEquals(null, bean.getBeanValue());
|
Assert.assertNull(bean.getBeanValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
16
hutool-json/src/test/resources/issue488Array.json
Normal file
16
hutool-json/src/test/resources/issue488Array.json
Normal 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"
|
||||||
|
}]
|
||||||
|
}]
|
Loading…
x
Reference in New Issue
Block a user