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