This commit is contained in:
Looly 2024-09-19 21:59:10 +08:00
parent 59c168a99c
commit 91d425ba63
7 changed files with 37 additions and 36 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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>

View File

@ -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()) {

View File

@ -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);

View File

@ -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, ()->{
// 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

View File

@ -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");