mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
59c168a99c
commit
91d425ba63
@ -158,6 +158,10 @@ public class JSONUtil {
|
||||
* @since 5.3.1
|
||||
*/
|
||||
public static JSONArray parseArray(final Object arrayOrCollection, final JSONConfig config, final Predicate<MutableEntry<Object, Object>> predicate) {
|
||||
if(arrayOrCollection instanceof JSONObject){
|
||||
final JSONValueMapper jsonValueMapper = JSONValueMapper.of(config, predicate);
|
||||
return jsonValueMapper.mapFromJSONObject((JSONObject) arrayOrCollection);
|
||||
}
|
||||
return (JSONArray) parse(arrayOrCollection, config, predicate);
|
||||
}
|
||||
|
||||
|
@ -188,7 +188,7 @@ public class JSONConverter implements Converter, Serializable {
|
||||
|
||||
// 当目标类型不确定时,返回原JSON
|
||||
final Class<T> rawType = (Class<T>) TypeUtil.getClass(targetType);
|
||||
if (null == rawType || rawType.isInstance(json)) {
|
||||
if (null == rawType || JSON.class.isAssignableFrom(rawType)) {
|
||||
return (T) json;
|
||||
//throw new JSONException("Can not get class from type: {}", targetType);
|
||||
}
|
||||
@ -238,14 +238,14 @@ public class JSONConverter implements Converter, Serializable {
|
||||
json.getClass().getName(), json, targetType.getTypeName());
|
||||
}
|
||||
|
||||
private Object toDateWithFormat(final Class<?> targetClass, final Object value) {
|
||||
private Object toDateWithFormat(final Class<?> targetDateClass, final Object value) {
|
||||
// 日期转换,支持自定义日期格式
|
||||
final String format = config.getDateFormat();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
if (Date.class.isAssignableFrom(targetClass)) {
|
||||
return new DateConverter(format).convert(targetClass, value);
|
||||
if (Date.class.isAssignableFrom(targetDateClass)) {
|
||||
return new DateConverter(format).convert(targetDateClass, value);
|
||||
} else {
|
||||
return new TemporalAccessorConverter(format).convert(targetClass, value);
|
||||
return new TemporalAccessorConverter(format).convert(targetDateClass, value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -26,17 +26,12 @@ import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 对象和JSONArray映射器,用于转换对象为JSONArray,支持:
|
||||
* <ul>
|
||||
* <li>CharSequence 转 JSONArray,使用JSONTokener解析</li>
|
||||
* <li>{@link Reader} 转 JSONArray,使用JSONTokener解析</li>
|
||||
* <li>{@link InputStream} 转 JSONArray,使用JSONTokener解析</li>
|
||||
* <li>JSONTokener 转 JSONArray,直接解析</li>
|
||||
* <li>Iterable 转 JSONArray</li>
|
||||
* <li>Iterator 转 JSONArray</li>
|
||||
|
@ -24,12 +24,13 @@ import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.reflect.method.MethodUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.InternalJSONUtil;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
@ -41,9 +42,6 @@ import java.util.function.Predicate;
|
||||
* <ul>
|
||||
* <li>Map 转 JSONObject,将键值对加入JSON对象</li>
|
||||
* <li>Map.Entry 转 JSONObject</li>
|
||||
* <li>CharSequence 转 JSONObject,使用JSONTokener解析</li>
|
||||
* <li>{@link Reader} 转 JSONObject,使用JSONTokener解析</li>
|
||||
* <li>{@link InputStream} 转 JSONObject,使用JSONTokener解析</li>
|
||||
* <li>JSONTokener 转 JSONObject,直接解析</li>
|
||||
* <li>ResourceBundle 转 JSONObject</li>
|
||||
* <li>Bean 转 JSONObject,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||
@ -90,11 +88,6 @@ class JSONObjectMapper {
|
||||
return;
|
||||
}
|
||||
|
||||
if (source instanceof JSONArray) {
|
||||
// 不支持集合类型转换为JSONObject
|
||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||
}
|
||||
|
||||
if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Map.Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
|
@ -82,6 +82,19 @@ public class JSONValueMapper implements Serializable {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSONObject转换为JSONArray
|
||||
*
|
||||
* @param jsonObject JSONObject
|
||||
* @return JSONArray
|
||||
*/
|
||||
public JSONArray mapFromJSONObject(final JSONObject jsonObject){
|
||||
final JSONArray array = new JSONArray(jsonConfig);
|
||||
JSONArrayMapper.of(jsonObject, this.predicate)
|
||||
.mapTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析JSON字符串或XML字符串为JSON结构
|
||||
*
|
||||
@ -146,8 +159,6 @@ public class JSONValueMapper implements Serializable {
|
||||
return serializer.serialize(obj, new SimpleJSONContext(null, this.jsonConfig));
|
||||
}
|
||||
|
||||
// read
|
||||
|
||||
// 原始类型
|
||||
if (null != ValueWriterManager.getInstance().get(obj)) {
|
||||
return new JSONPrimitive(obj, jsonConfig);
|
||||
|
@ -19,6 +19,7 @@ package org.dromara.hutool.json;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
@ -74,14 +75,14 @@ public class JSONArrayTest {
|
||||
array.set("value2");
|
||||
array.set("value3");
|
||||
|
||||
assertEquals(array.get(0), "value1");
|
||||
assertEquals(array.getObj(0), "value1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
final String jsonStr = "[\"value1\", \"value2\", \"value3\"]";
|
||||
final JSONArray array = JSONUtil.parseArray(jsonStr);
|
||||
assertEquals(array.get(0), "value1");
|
||||
assertEquals(array.getObj(0), "value1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -192,13 +193,14 @@ public class JSONArrayTest {
|
||||
|
||||
@Test
|
||||
public void toListWithErrorTest() {
|
||||
Assertions.assertThrows(JSONException.class, ()->{
|
||||
final String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
|
||||
final JSONArray ja = JSONUtil.parseArray(json);
|
||||
// Assertions.assertThrows(JSONException.class, ()->{
|
||||
// });
|
||||
final String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
|
||||
final JSONArray ja = JSONUtil.parseArray(json);
|
||||
|
||||
ja.toBean(new TypeReference<List<List<KeyBean>>>() {
|
||||
});
|
||||
});
|
||||
final Object bean = ja.toBean(new TypeReference<List<List<KeyBean>>>() {});
|
||||
|
||||
Console.log(bean);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -17,11 +17,9 @@
|
||||
package org.dromara.hutool.json.jwt;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.date.DatePattern;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ByteUtil;
|
||||
import org.dromara.hutool.json.jwt.signers.AlgorithmUtil;
|
||||
import org.dromara.hutool.json.jwt.signers.JWTSigner;
|
||||
@ -163,9 +161,7 @@ public class JWTTest {
|
||||
Assertions.assertEquals(bean, beanRes);
|
||||
Assertions.assertEquals(numRes, num);
|
||||
Assertions.assertEquals(username, strRes);
|
||||
Assertions.assertEquals(
|
||||
StrUtil.wrap(CollUtil.join(list, ","), "[", "]"),
|
||||
listRes.toString());
|
||||
Assertions.assertEquals(list, listRes);
|
||||
|
||||
final String formattedDate = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
|
||||
final String formattedRes = DateUtil.format(dateRes, "yyyy-MM-dd HH:mm:ss");
|
||||
|
Loading…
x
Reference in New Issue
Block a user