This commit is contained in:
Looly 2024-10-01 00:00:04 +08:00
parent 9a753e3fd6
commit 3a54cc9445
9 changed files with 44 additions and 39 deletions

View File

@ -18,7 +18,6 @@ package org.dromara.hutool.json;
import org.dromara.hutool.core.bean.path.BeanPath;
import org.dromara.hutool.core.lang.mutable.MutableEntry;
import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.json.writer.JSONWriter;
import java.io.Serializable;
@ -261,11 +260,7 @@ public interface JSON extends Serializable {
* @param type {@link Type}
* @return 实体类对象
*/
@SuppressWarnings("unchecked")
default <T> T toBean(final Type type) {
if(JSON.class.isAssignableFrom(TypeUtil.getClass(type))){
return (T) this;
}
return getFactory().toBean(this, type);
}
}

View File

@ -28,7 +28,6 @@ import org.dromara.hutool.json.serializer.TypeAdapter;
import org.dromara.hutool.json.support.JSONNodeBeanFactory;
import org.dromara.hutool.json.writer.JSONWriter;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Type;
import java.util.function.Consumer;
import java.util.function.Predicate;
@ -268,16 +267,12 @@ public class JSONFactory {
// region ----- parse
/**
* JSON字符串转JSONObject对象
* 对象转JSONObject对象
*
* @param obj Bean对象或者Map
* @return JSONObject
*/
public JSONObject parseObj(Object obj) {
if (obj instanceof byte[]) {
obj = new ByteArrayInputStream((byte[]) obj);
}
public JSONObject parseObj(final Object obj) {
final JSONMapper mapper = getMapper();
if (obj instanceof CharSequence) {
return (JSONObject) mapper.map((CharSequence) obj);
@ -298,9 +293,9 @@ public class JSONFactory {
*/
public JSONArray parseArray(final Object obj) {
final JSONMapper mapper = getMapper();
if (obj instanceof JSONObject) {
return mapper.mapFromJSONObject((JSONObject) obj);
}
// if (obj instanceof JSONObject) {
// return mapper.mapFromJSONObject((JSONObject) obj);
// }
if (obj instanceof CharSequence) {
return (JSONArray) mapper.map((CharSequence) obj);

View File

@ -99,7 +99,7 @@ public class JSONUtil {
* @since 6.0.0
*/
public static JSONPrimitive ofPrimitive(final Object value) {
return ofPrimitive(value, JSONConfig.of());
return JSONFactory.getInstance().ofPrimitive(value);
}
/**
@ -132,7 +132,7 @@ public class JSONUtil {
* @return JSONObject
*/
public static JSONObject parseObj(final Object obj) {
return parseObj(obj, JSONConfig.of(), null);
return JSONFactory.getInstance().parseObj(obj);
}
/**
@ -166,7 +166,7 @@ public class JSONUtil {
* @return JSONArray
*/
public static JSONArray parseArray(final Object obj) {
return parseArray(obj, null);
return JSONFactory.getInstance().parseArray(obj);
}
/**
@ -207,7 +207,7 @@ public class JSONUtil {
* @return JSON
*/
public static JSON parse(final Object obj) {
return parse(obj, null);
return JSONFactory.getInstance().parse(obj);
}
/**
@ -314,7 +314,7 @@ public class JSONUtil {
* @return JSON字符串
*/
public static String toJsonStr(final Object obj) {
return toJsonStr(obj, (JSONConfig) null);
return parse(obj).toString();
}
/**
@ -341,7 +341,7 @@ public class JSONUtil {
*/
public static void toJsonStr(final Object obj, final Appendable appendable) {
Assert.notNull(appendable);
final JSONFactory jsonFactory = JSONFactory.of(null, null);
final JSONFactory jsonFactory = JSONFactory.getInstance();
final JSON json = jsonFactory.parse(obj);
json.write(jsonFactory.ofWriter(appendable, 0));
}
@ -389,13 +389,15 @@ public class JSONUtil {
* 转为实体类对象
*
* @param <T> Bean类型
* @param json JSONObject
* @param obj 对象
* @param type 实体类对象类型
* @return 实体类对象
* @since 4.3.2
*/
public static <T> T toBean(final Object json, final Type type) {
return toBean(json, null, type);
public static <T> T toBean(final Object obj, final Type type) {
if (null == obj) {
return null;
}
return parse(obj).toBean(type);
}
/**

View File

@ -28,7 +28,8 @@ public interface JSONContext {
/**
* 获取当前JSON对象<br>
* 此对象为在Mapper时预定义的对象用于指定序列化的JSON类型
* 此对象为在Mapper时预定义的对象用于指定序列化的JSON类型<br>
* 未指定返回{@code null}
*
* @return JSON对象
*/

View File

@ -129,13 +129,6 @@ public class JSONMapper implements Serializable {
type = ((TypeReference<?>) type).getType();
}
if (null == type || Object.class == type) {
if (json instanceof JSONPrimitive) {
return (T) ((JSONPrimitive) json).getValue();
}
return (T) json;
}
JSONDeserializer<Object> deserializer = null;
// 自定义反序列化
if (null != this.typeAdapterManager) {

View File

@ -37,9 +37,12 @@ public class DefaultDeserializer implements JSONDeserializer<Object> {
@Override
public Object deserialize(final JSON json, final Type deserializeType) {
// 当目标类型不确定时返回原JSON
// 当目标类型不确定时返回原JSON或JSONPrimitive的值
final Class<?> rawType = TypeUtil.getClass(deserializeType);
if (null == rawType || Object.class == rawType || rawType.isAssignableFrom(json.getClass())) {
if (json instanceof JSONPrimitive) {
return ((JSONPrimitive) json).getValue();
}
return json;
}

View File

@ -17,22 +17,24 @@
package org.dromara.hutool.json.serializer.impl;
import org.dromara.hutool.core.convert.CompositeConverter;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.JSON;
import org.dromara.hutool.json.JSONArray;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.serializer.JSONContext;
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Map类型适配器用于将JSON对象和Map对象互转
* Map类型适配器用于将JSON对象和Map对象互转
*
* @author looly
* @since 6.0.0
@ -60,10 +62,24 @@ public class MapTypeAdapter implements MatcherJSONSerializer<Map<?, ?>>, Matcher
@Override
public JSON serialize(final Map<?, ?> bean, final JSONContext context) {
// 序列化为JSONArray
if(context.getContextJson() instanceof JSONArray){
final Iterator<?> iter;
if(bean instanceof Iterator){
iter = (Iterator<?>) bean;
} else if(bean instanceof Iterable){
iter = ((Iterable<?>) bean).iterator();
} else{
iter = bean.entrySet().iterator();
}
return IterTypeAdapter.INSTANCE.serialize(bean, context);
}
// Map to JSONObject
final JSONObject result = context.getOrCreateObj();
// 注入键值对
for (final Map.Entry<?, ?> e : bean.entrySet()) {
result.putObj(ConvertUtil.toStr(e.getKey()), e.getValue());
result.putObj(StrUtil.toStringOrNull(e.getKey()), e.getValue());
}
return result;
}

View File

@ -44,7 +44,7 @@ public class PairDeserializer implements JSONDeserializer<Pair<?, ?>> {
deserializeType = ((TypeReference<?>) deserializeType).getType();
}
final Type leftType = TypeUtil.getTypeArgument(deserializeType, 0);
final Type rightType = TypeUtil.getTypeArgument(deserializeType, 2);
final Type rightType = TypeUtil.getTypeArgument(deserializeType, 1);
final JSONObject jsonObject = json.asJSONObject();
final JSON left = jsonObject.get("left");

View File

@ -48,7 +48,7 @@ public class JSONArrayTest {
// JSONObject实现了Iterable接口可以转换为JSONArray
final JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = JSONUtil.parseArray(jsonObject, JSONConfig.of());
JSONArray jsonArray = JSONUtil.parseArray(jsonObject);
assertEquals(new JSONArray(), jsonArray);
jsonObject.putObj("key1", "value1");